diff options
Diffstat (limited to 'src/gpg/function')
-rw-r--r-- | src/gpg/function/GpgKeyImportExportor.cpp | 27 | ||||
-rw-r--r-- | src/gpg/function/GpgKeyImportExportor.h | 2 | ||||
-rw-r--r-- | src/gpg/function/GpgKeyManager.cpp | 6 | ||||
-rw-r--r-- | src/gpg/function/GpgKeyManager.h | 4 | ||||
-rw-r--r-- | src/gpg/function/GpgKeyOpera.cpp | 33 | ||||
-rw-r--r-- | src/gpg/function/GpgKeyOpera.h | 7 |
6 files changed, 61 insertions, 18 deletions
diff --git a/src/gpg/function/GpgKeyImportExportor.cpp b/src/gpg/function/GpgKeyImportExportor.cpp index f4b88c60..d8812839 100644 --- a/src/gpg/function/GpgKeyImportExportor.cpp +++ b/src/gpg/function/GpgKeyImportExportor.cpp @@ -33,11 +33,12 @@ */ GpgFrontend::GpgImportInformation GpgFrontend::GpgKeyImportExportor::ImportKey( StdBypeArrayPtr in_buffer) { - if (in_buffer->empty()) return GpgImportInformation(); + if (in_buffer->empty()) return {}; GpgData data_in(in_buffer->data(), in_buffer->size()); auto err = check_gpg_error(gpgme_op_import(ctx, data_in)); - assert(gpgme_err_code(err) == GPG_ERR_NO_ERROR); + if (gpgme_err_code(err) != GPG_ERR_NO_ERROR) return {}; + gpgme_import_result_t result; result = gpgme_op_import_result(ctx); gpgme_import_status_t status = result->imports; @@ -49,6 +50,7 @@ GpgFrontend::GpgImportInformation GpgFrontend::GpgKeyImportExportor::ImportKey( import_info->importedKeys.emplace_back(key); status = status->next; } + return *import_info; } @@ -64,11 +66,11 @@ bool GpgFrontend::GpgKeyImportExportor::ExportKeys( // Alleviate another crash problem caused by an unknown array out-of-bounds // access + auto all_success = true; for (size_t i = 0; i < uid_list->size(); i++) { GpgData data_out; auto err = gpgme_op_export(ctx, (*uid_list)[i].c_str(), 0, data_out); - assert(gpgme_err_code(err) == GPG_ERR_NO_ERROR); - + if (gpgme_err_code(err) != GPG_ERR_NO_ERROR) all_success = false; DLOG(INFO) << "exportKeys read_bytes" << gpgme_data_seek(data_out, 0, SEEK_END); @@ -76,7 +78,7 @@ bool GpgFrontend::GpgKeyImportExportor::ExportKeys( std::swap(out_buffer, temp_out_buffer); } - return true; + return all_success; } /** @@ -105,7 +107,6 @@ bool GpgFrontend::GpgKeyImportExportor::ExportSecretKey( gpgme_key_t target_key[2] = {gpgme_key_t(key), nullptr}; GpgData data_out; - // export private key to outBuffer gpgme_error_t err = gpgme_op_export_keys(ctx, target_key, GPGME_EXPORT_MODE_SECRET, data_out); @@ -115,3 +116,17 @@ bool GpgFrontend::GpgKeyImportExportor::ExportSecretKey( return check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR; } + +bool GpgFrontend::GpgKeyImportExportor::ExportKey( + const GpgFrontend::GpgKey& key, + GpgFrontend::ByteArrayPtr& out_buffer) const { + GpgData data_out; + auto err = gpgme_op_export(ctx, key.id().c_str(), 0, data_out); + + DLOG(INFO) << "exportKeys read_bytes" + << gpgme_data_seek(data_out, 0, SEEK_END); + + auto temp_out_buffer = data_out.Read2Buffer(); + std::swap(out_buffer, temp_out_buffer); + return check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR; +} diff --git a/src/gpg/function/GpgKeyImportExportor.h b/src/gpg/function/GpgKeyImportExportor.h index bceb87ef..35a237ba 100644 --- a/src/gpg/function/GpgKeyImportExportor.h +++ b/src/gpg/function/GpgKeyImportExportor.h @@ -88,6 +88,8 @@ class GpgKeyImportExportor bool ExportKeys(const KeyArgsList& keys, ByteArrayPtr& outBuffer) const; + bool ExportKey(const GpgKey& key, ByteArrayPtr& out_buffer) const; + bool ExportSecretKey(const GpgKey& key, ByteArrayPtr& outBuffer) const; private: diff --git a/src/gpg/function/GpgKeyManager.cpp b/src/gpg/function/GpgKeyManager.cpp index 9e24b3d6..998c27a7 100644 --- a/src/gpg/function/GpgKeyManager.cpp +++ b/src/gpg/function/GpgKeyManager.cpp @@ -33,7 +33,7 @@ bool GpgFrontend::GpgKeyManager::signKey( const GpgFrontend::GpgKey& target, GpgFrontend::KeyArgsList& keys, const std::string& uid, - const std::unique_ptr<boost::gregorian::date>& expires) { + const std::unique_ptr<boost::posix_time::ptime>& expires) { using namespace boost::posix_time; BasicOperator::GetInstance().SetSigners(keys); @@ -44,7 +44,7 @@ bool GpgFrontend::GpgKeyManager::signKey( if (expires == nullptr) flags |= GPGME_KEYSIGN_NOEXPIRE; else - expires_time_t = to_time_t(ptime(*expires)); + expires_time_t = to_time_t(*expires); auto err = check_gpg_error(gpgme_op_keysign( ctx, gpgme_key_t(target), uid.c_str(), expires_time_t, flags)); @@ -70,7 +70,7 @@ bool GpgFrontend::GpgKeyManager::revSign( bool GpgFrontend::GpgKeyManager::setExpire( const GpgFrontend::GpgKey& key, std::unique_ptr<GpgSubKey>& subkey, - std::unique_ptr<boost::gregorian::date>& expires) { + std::unique_ptr<boost::posix_time::ptime>& expires) { using namespace boost::posix_time; unsigned long expires_time = 0; diff --git a/src/gpg/function/GpgKeyManager.h b/src/gpg/function/GpgKeyManager.h index 2b07425c..01254962 100644 --- a/src/gpg/function/GpgKeyManager.h +++ b/src/gpg/function/GpgKeyManager.h @@ -41,13 +41,13 @@ class GpgKeyManager : public SingletonFunctionObject<GpgKeyManager> { * @return if successful */ bool signKey(const GpgKey& target, KeyArgsList& keys, const std::string& uid, - const std::unique_ptr<boost::gregorian::date>& expires); + const std::unique_ptr<boost::posix_time::ptime>& expires); bool revSign(const GpgFrontend::GpgKey& key, const GpgFrontend::SignIdArgsListPtr& signature_id); bool setExpire(const GpgKey& key, std::unique_ptr<GpgSubKey>& subkey, - std::unique_ptr<boost::gregorian::date>& expires); + std::unique_ptr<boost::posix_time::ptime>& expires); private: GpgContext& ctx = GpgContext::GetInstance(); diff --git a/src/gpg/function/GpgKeyOpera.cpp b/src/gpg/function/GpgKeyOpera.cpp index c60f9157..cdf5ab24 100644 --- a/src/gpg/function/GpgKeyOpera.cpp +++ b/src/gpg/function/GpgKeyOpera.cpp @@ -63,17 +63,19 @@ void GpgFrontend::GpgKeyOpera::DeleteKeys( */ GpgFrontend::GpgError GpgFrontend::GpgKeyOpera::SetExpire( const GpgKey& key, const SubkeyId& subkey_fpr, - std::unique_ptr<boost::gregorian::date>& expires) { + std::unique_ptr<boost::posix_time::ptime>& expires) { unsigned long expires_time = 0; + + LOG(INFO) << "expires" << *expires; + if (expires != nullptr) { using namespace boost::posix_time; using namespace std::chrono; - expires_time = to_time_t(ptime(*expires)) - - system_clock::to_time_t(system_clock::now()); + expires_time = + to_time_t(*expires) - system_clock::to_time_t(system_clock::now()); } - LOG(INFO) << "GpgFrontend::GpgKeyOpera::SetExpire" << key.id() << subkey_fpr - << expires_time; + LOG(INFO) << key.id() << subkey_fpr << expires_time; GpgError err; if (subkey_fpr.empty()) @@ -214,4 +216,23 @@ GpgFrontend::GpgError GpgFrontend::GpgKeyOpera::GenerateSubkey( auto err = gpgme_op_createsubkey(ctx, gpgme_key_t(key), algo, 0, expires, flags); return check_gpg_error(err); -}
\ No newline at end of file +} + +GpgFrontend::GpgError GpgFrontend::GpgKeyOpera::ModifyPassword( + const GpgFrontend::GpgKey& key) { + if (ctx.GetInfo().GnupgVersion < "2.0.15") { + LOG(ERROR) << _("operator not support"); + return GPG_ERR_NOT_SUPPORTED; + } + auto err = gpgme_op_passwd(ctx, gpgme_key_t(key), 0); + return check_gpg_error(err); +} +GpgFrontend::GpgError GpgFrontend::GpgKeyOpera::ModifyTOFUPolicy( + const GpgFrontend::GpgKey& key, gpgme_tofu_policy_t tofu_policy) { + if (ctx.GetInfo().GnupgVersion < "2.1.10") { + LOG(ERROR) << _("operator not support"); + return GPG_ERR_NOT_SUPPORTED; + } + auto err = gpgme_op_tofu_policy(ctx, gpgme_key_t(key), tofu_policy); + return check_gpg_error(err); +} diff --git a/src/gpg/function/GpgKeyOpera.h b/src/gpg/function/GpgKeyOpera.h index 71e2de8b..7decfd79 100644 --- a/src/gpg/function/GpgKeyOpera.h +++ b/src/gpg/function/GpgKeyOpera.h @@ -36,11 +36,16 @@ class GpgKeyOpera : public SingletonFunctionObject<GpgKeyOpera> { void DeleteKeys(KeyIdArgsListPtr key_ids); GpgError SetExpire(const GpgKey& key, const SubkeyId& subkey_fpr, - std::unique_ptr<boost::gregorian::date>& expires); + std::unique_ptr<boost::posix_time::ptime>& expires); static void GenerateRevokeCert(const GpgKey& key, const std::string& output_file_name); + GpgFrontend::GpgError ModifyPassword(const GpgKey& key); + + GpgFrontend::GpgError ModifyTOFUPolicy(const GpgKey& key, + gpgme_tofu_policy_t tofu_policy); + GpgFrontend::GpgError GenerateKey(const std::unique_ptr<GenKeyInfo>& params); GpgFrontend::GpgError GenerateSubkey( |