From 4c4121f164adac9dc9212372a02efd66d65cae97 Mon Sep 17 00:00:00 2001 From: Alexander Diemand Date: Mon, 27 Nov 2023 00:32:20 +0100 Subject: [PATCH] add: Gpg::decrypt_from_buffer --- build/test/CMakeLists.txt | 4 ++-- src/cpp/gpg.hpp.md | 2 ++ src/cpp/gpg_ctor.cpp.md | 26 ++++++++++++++++++++++++++ src/cpp/gpg_functions.cpp.md | 5 +++++ test/cpp/utGpg.cpp.md | 11 ++++++++--- 5 files changed, 43 insertions(+), 5 deletions(-) diff --git a/build/test/CMakeLists.txt b/build/test/CMakeLists.txt index 2213857..a566fd9 100644 --- a/build/test/CMakeLists.txt +++ b/build/test/CMakeLists.txt @@ -11,8 +11,8 @@ set(Boost_DEBUG OFF) set(Boost_USE_STATIC_LIBS OFF) set(Boost_USE_STATIC_RUNTIME OFF) set(Boost_USE_MULTITHREADED ON) -find_package( Boost 1.74.0 REQUIRED COMPONENTS system date_time unit_test_framework contract) -#include_directories(${Boost_INCLUDE_DIRS}) +find_package( Boost 1.83.0 REQUIRED COMPONENTS unit_test_framework) +include_directories(${Boost_INCLUDE_DIRS}) # mine include_directories(../src) diff --git a/src/cpp/gpg.hpp.md b/src/cpp/gpg.hpp.md index 730d204..247ee25 100644 --- a/src/cpp/gpg.hpp.md +++ b/src/cpp/gpg.hpp.md @@ -43,6 +43,8 @@ namespace [lxr](namespace.list) { >bool [decrypt_from_file](gpg_functions.cpp.md)(std::string const & fpath); +>std::optional<std::string> [decrypt_from_buffer](gpg_functions.cpp.md)(std::string const & buffer); + >private: >Gpg(Gpg const &) = delete; diff --git a/src/cpp/gpg_ctor.cpp.md b/src/cpp/gpg_ctor.cpp.md index 2613827..e28329e 100644 --- a/src/cpp/gpg_ctor.cpp.md +++ b/src/cpp/gpg_ctor.cpp.md @@ -13,6 +13,7 @@ struct Gpg::pimpl { std::ostream& ostream() { oss.reset(new std::ostringstream()); return *oss; } std::istream& istream() { return *iss; } bool decrypt_from_file(std::string const & fpath); + std::optional decrypt_from_buffer(std::string const & buffer); private: gpgme_ctx_t ctx{nullptr}; @@ -181,6 +182,31 @@ bool Gpg::pimpl::decrypt_from_file(std::string const &p_fp) return true; } +std::optional Gpg::pimpl::decrypt_from_buffer(std::string const &p_buffer) +{ + gpgme_error_t err; + gpgme_data_t plain, cipher; + err = gpgme_data_new_from_mem(&cipher, p_buffer.c_str(), p_buffer.size(), 0); + if (err != GPG_ERR_NO_ERROR) { OUTPUT_GPGME_ERROR(err); return {}; } + err = gpgme_data_new(&plain); + if (err != GPG_ERR_NO_ERROR) { OUTPUT_GPGME_ERROR(err); gpgme_data_release(cipher); return {}; } + auto cleanup_data = [&](void) { + gpgme_data_release(plain); + gpgme_data_release(cipher); + }; + + err = gpgme_new(&ctx); + if (err != GPG_ERR_NO_ERROR) { OUTPUT_GPGME_ERROR(err); cleanup_data(); return {}; } + + err = gpgme_op_decrypt(ctx, cipher, plain); + if (err != GPG_ERR_NO_ERROR) { OUTPUT_GPGME_ERROR(err); cleanup_data(); gpgme_release(ctx); return {}; } + + gpgme_data_release(cipher); + size_t mlen; + const char *mptr = gpgme_data_release_and_get_mem(plain, &mlen); + return std::string(mptr, mlen); +} + Gpg::Gpg() : _pimpl(std::make_unique()) { diff --git a/src/cpp/gpg_functions.cpp.md b/src/cpp/gpg_functions.cpp.md index 579576f..7f6d2c6 100644 --- a/src/cpp/gpg_functions.cpp.md +++ b/src/cpp/gpg_functions.cpp.md @@ -37,4 +37,9 @@ bool Gpg::decrypt_from_file(std::string const &fp) return _pimpl->decrypt_from_file(fp); } +std::optional Gpg::decrypt_from_buffer(std::string const &b) +{ + return _pimpl->decrypt_from_buffer(b); +} + ``` \ No newline at end of file diff --git a/test/cpp/utGpg.cpp.md b/test/cpp/utGpg.cpp.md index 3cdfcc0..2654727 100644 --- a/test/cpp/utGpg.cpp.md +++ b/test/cpp/utGpg.cpp.md @@ -52,11 +52,16 @@ BOOST_AUTO_TEST_CASE( gpg_check_non_private_key ) BOOST_TEST_WARN(!gpg.has_private_key(addr), "the archiving process should not have access to this private key."); } -BOOST_AUTO_TEST_CASE( gpg_encrypt_string ) +BOOST_AUTO_TEST_CASE( gpg_encrypt_decrypt_string ) { lxr::Gpg gpg; - auto s = gpg.encrypt_to_key(addr, "hello world."); - BOOST_CHECK(s); + std::optional cipher = gpg.encrypt_to_key(addr, "hello world."); + BOOST_CHECK(cipher); + if (cipher) { + std::optional plain = gpg.decrypt_from_buffer(cipher.value()); + BOOST_CHECK(plain); + BOOST_CHECK_EQUAL("hello world.", plain.value()); + } } BOOST_AUTO_TEST_CASE( gpg_encrypt_stream )