From b5cd5eac82b6bbd8a00fb39c045d473d6517b5f4 Mon Sep 17 00:00:00 2001 From: Saturneric Date: Sat, 25 Dec 2021 09:54:57 +0800 Subject: (core, test): core improved and test gpg alone mode 1. let modules known their channels. 2. let factory create a channel. 3. reduce dumplicate code. 4. add type check for function object. 5. test gpg alone mode. 6. remove some asserts. 7. rename importexportor to importexporter. 8. move args in gpg context constructor to a struct. --- src/gpg/function/GpgKeyOpera.cpp | 65 ++++++++++++++++++++++++++++++++-------- 1 file changed, 53 insertions(+), 12 deletions(-) (limited to 'src/gpg/function/GpgKeyOpera.cpp') diff --git a/src/gpg/function/GpgKeyOpera.cpp b/src/gpg/function/GpgKeyOpera.cpp index 4bad303d..4df06875 100644 --- a/src/gpg/function/GpgKeyOpera.cpp +++ b/src/gpg/function/GpgKeyOpera.cpp @@ -26,6 +26,7 @@ #include #include +#include #include #include #include @@ -50,7 +51,7 @@ void GpgFrontend::GpgKeyOpera::DeleteKeys( 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"; + LOG(WARNING) << "GpgKeyOpera DeleteKeys get key failed" << key.fpr(); } } @@ -146,7 +147,7 @@ void GpgFrontend::GpgKeyOpera::GenerateRevokeCert( * @return error information */ GpgFrontend::GpgError GpgFrontend::GpgKeyOpera::GenerateKey( - const std::unique_ptr& params) { + const std::unique_ptr& params, GpgGenKeyResult& result) { auto userid_utf8 = params->getUserid(); const char* userid = userid_utf8.c_str(); auto algo_utf8 = params->getAlgo() + params->getKeySizeStr(); @@ -163,19 +164,53 @@ GpgFrontend::GpgError GpgFrontend::GpgKeyOpera::GenerateKey( system_clock::to_time_t(system_clock::now()); } - unsigned int flags = 0; + GpgError err; - if (!params->isSubKey()) flags |= GPGME_CREATE_CERT; - if (params->isAllowEncryption()) flags |= GPGME_CREATE_ENCR; - if (params->isAllowSigning()) flags |= GPGME_CREATE_SIGN; - if (params->isAllowAuthentication()) flags |= GPGME_CREATE_AUTH; - if (params->isNonExpired()) flags |= GPGME_CREATE_NOEXPIRE; - if (params->isNoPassPhrase()) flags |= GPGME_CREATE_NOPASSWD; + if (ctx.GetInfo().GnupgVersion >= "2.1.0") { + unsigned int flags = 0; + + if (!params->isSubKey()) flags |= GPGME_CREATE_CERT; + if (params->isAllowEncryption()) flags |= GPGME_CREATE_ENCR; + if (params->isAllowSigning()) flags |= GPGME_CREATE_SIGN; + if (params->isAllowAuthentication()) flags |= GPGME_CREATE_AUTH; + if (params->isNonExpired()) flags |= GPGME_CREATE_NOEXPIRE; + if (params->isNoPassPhrase()) flags |= GPGME_CREATE_NOPASSWD; + + LOG(INFO) << "GpgFrontend::GpgKeyOpera::GenerateKey Args: " << userid + << algo << expires << flags; + + err = gpgme_op_createkey(ctx, userid, algo, 0, expires, nullptr, flags); - LOG(INFO) << "GpgFrontend::GpgKeyOpera::GenerateKey Args: " << userid << algo - << expires << flags; + } else { + std::stringstream ss; + + auto param_format = + boost::format{ + "\n" + "Key-Type: %1%\n" + "Subkey-Type: %2%\n" + "Name-Real: %3%\n" + "Name-Comment: %4%\n" + "Name-Email: %5%\n"} % + params->getAlgo() % params->getKeySize() % params->getName() % + params->getComment() % params->getEmail(); + ss << param_format; + + if (!params->isNonExpired()) + ss << boost::format{"Expire-Date: %1%\n"} % expires; + if (!params->isNoPassPhrase()) + ss << boost::format{"Passphrase: %1%\n"} % params->getPassPhrase(); + + ss << ""; + + err = gpgme_op_genkey(ctx, ss.str().c_str(), nullptr, nullptr); + } + + if (check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR) { + auto temp_result = _new_result(gpgme_op_genkey_result(ctx)); + std::swap(temp_result, result); + } - auto err = gpgme_op_createkey(ctx, userid, algo, 0, expires, nullptr, flags); return check_gpg_error(err); } @@ -234,3 +269,9 @@ GpgFrontend::GpgError GpgFrontend::GpgKeyOpera::ModifyTOFUPolicy( auto err = gpgme_op_tofu_policy(ctx, gpgme_key_t(key), tofu_policy); return check_gpg_error(err); } + +void GpgFrontend::GpgKeyOpera::DeleteKey(const GpgFrontend::KeyId& key_id) { + auto keys = std::make_unique(); + keys->push_back(key_id); + DeleteKeys(std::move(keys)); +} -- cgit v1.2.3 From cb10cffa20702ec3dac50adb5aeeebd6138279b0 Mon Sep 17 00:00:00 2001 From: Saturneric Date: Fri, 31 Dec 2021 09:50:20 +0800 Subject: (core, test): core improved and test gpg key generation 1. Refactor the initialization code of Context. 2. Improve some callback function support. 3. Improve the key deletion function. 4. Modify the key output of some functions. 5. Add test for key generation. 6. Delete all imported keys at the end of the test case. 7. Add setup mode initialization parameters for Context. --- src/gpg/function/GpgKeyOpera.cpp | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'src/gpg/function/GpgKeyOpera.cpp') diff --git a/src/gpg/function/GpgKeyOpera.cpp b/src/gpg/function/GpgKeyOpera.cpp index 4df06875..4cbc1d0a 100644 --- a/src/gpg/function/GpgKeyOpera.cpp +++ b/src/gpg/function/GpgKeyOpera.cpp @@ -47,11 +47,13 @@ void GpgFrontend::GpgKeyOpera::DeleteKeys( for (const auto& tmp : *key_ids) { 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)); + err = check_gpg_error( + gpgme_op_delete_ext(ctx, gpgme_key_t(key), + GPGME_DELETE_ALLOW_SECRET | GPGME_DELETE_FORCE)); assert(gpg_err_code(err) == GPG_ERR_NO_ERROR); - } else - LOG(WARNING) << "GpgKeyOpera DeleteKeys get key failed" << key.fpr(); + } else { + LOG(WARNING) << "GpgKeyOpera DeleteKeys get key failed" << tmp; + } } } @@ -152,8 +154,7 @@ GpgFrontend::GpgError GpgFrontend::GpgKeyOpera::GenerateKey( const char* userid = userid_utf8.c_str(); auto algo_utf8 = params->getAlgo() + params->getKeySizeStr(); - LOG(INFO) << "GpgFrontend::GpgKeyOpera::GenerateKey Params" - << params->getAlgo() << params->getKeySizeStr(); + LOG(INFO) << "params" << params->getAlgo() << params->getKeySizeStr(); const char* algo = algo_utf8.c_str(); unsigned long expires = 0; @@ -176,14 +177,12 @@ GpgFrontend::GpgError GpgFrontend::GpgKeyOpera::GenerateKey( if (params->isNonExpired()) flags |= GPGME_CREATE_NOEXPIRE; if (params->isNoPassPhrase()) flags |= GPGME_CREATE_NOPASSWD; - LOG(INFO) << "GpgFrontend::GpgKeyOpera::GenerateKey Args: " << userid - << algo << expires << flags; + LOG(INFO) << "args: " << userid << algo << expires << flags; err = gpgme_op_createkey(ctx, userid, algo, 0, expires, nullptr, flags); } else { std::stringstream ss; - auto param_format = boost::format{ "\n" @@ -203,6 +202,8 @@ GpgFrontend::GpgError GpgFrontend::GpgKeyOpera::GenerateKey( ss << ""; + DLOG(INFO) << "params" << std::endl << ss.str(); + err = gpgme_op_genkey(ctx, ss.str().c_str(), nullptr, nullptr); } -- cgit v1.2.3 From 5e0e6494c76154b1da5002bda05e23258678c403 Mon Sep 17 00:00:00 2001 From: Saturneric Date: Sat, 1 Jan 2022 02:55:29 +0800 Subject: (core, test): test key generation for standalone mode 1. add key generation test for standalone mode 2. add supported algos in standalone mode 3. changed support algo vectors from static value to static getter 4. modified name of some variables. --- src/gpg/function/GpgKeyOpera.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'src/gpg/function/GpgKeyOpera.cpp') diff --git a/src/gpg/function/GpgKeyOpera.cpp b/src/gpg/function/GpgKeyOpera.cpp index 4cbc1d0a..df89c6f5 100644 --- a/src/gpg/function/GpgKeyOpera.cpp +++ b/src/gpg/function/GpgKeyOpera.cpp @@ -187,7 +187,8 @@ GpgFrontend::GpgError GpgFrontend::GpgKeyOpera::GenerateKey( boost::format{ "\n" "Key-Type: %1%\n" - "Subkey-Type: %2%\n" + "Key-Usage: sign\n" + "Key-Length: %2%\n" "Name-Real: %3%\n" "Name-Comment: %4%\n" "Name-Email: %5%\n"} % @@ -195,8 +196,11 @@ GpgFrontend::GpgError GpgFrontend::GpgKeyOpera::GenerateKey( params->getComment() % params->getEmail(); ss << param_format; - if (!params->isNonExpired()) - ss << boost::format{"Expire-Date: %1%\n"} % expires; + if (!params->isNonExpired()) { + auto date = params->getExpired().date(); + ss << boost::format{"Expire-Date: %1%\n"} % to_iso_string(date); + } else + ss << boost::format{"Expire-Date: 0\n"}; if (!params->isNoPassPhrase()) ss << boost::format{"Passphrase: %1%\n"} % params->getPassPhrase(); -- cgit v1.2.3 From 0f464081971569d9ec6f621cfecdb39a5b8ee2b9 Mon Sep 17 00:00:00 2001 From: Saturneric Date: Sun, 2 Jan 2022 15:07:19 +0800 Subject: (core, ui): add & modify file operations 1. add non ascii mode for file operations. 2. the suffix of normalized file encryption. 3. refactor general settings. --- src/gpg/function/GpgKeyOpera.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src/gpg/function/GpgKeyOpera.cpp') diff --git a/src/gpg/function/GpgKeyOpera.cpp b/src/gpg/function/GpgKeyOpera.cpp index df89c6f5..e9fcbc1d 100644 --- a/src/gpg/function/GpgKeyOpera.cpp +++ b/src/gpg/function/GpgKeyOpera.cpp @@ -58,9 +58,10 @@ void GpgFrontend::GpgKeyOpera::DeleteKeys( } /** - * Set the expire date and time of a key pair(actually the master key) or subkey + * Set the expire date and time of a key pair(actually the primary key) or + * subkey * @param key target key pair - * @param subkey null if master key + * @param subkey null if primary key * @param expires date and time * @return if successful */ -- cgit v1.2.3