From d0fa1838ce067743db2d7e2d10ed9612111e6ff1 Mon Sep 17 00:00:00 2001 From: Fred Morcos Date: Mon, 13 Feb 2023 09:53:06 +0100 Subject: [PATCH] Introduce DNSCryptoKeyEngine::convertToPEMString --- pdns/dnssecinfra.hh | 19 +++++++++++++++++++ pdns/test-signers.cc | 18 ++---------------- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/pdns/dnssecinfra.hh b/pdns/dnssecinfra.hh index b0f80af246..f2d00f28f8 100644 --- a/pdns/dnssecinfra.hh +++ b/pdns/dnssecinfra.hh @@ -78,6 +78,25 @@ class DNSCryptoKeyEngine throw std::runtime_error(getName() + ": Conversion to PEM not supported"); }; + /** + * \brief Converts the key into a PEM string. + * + * \return A string containing the key's PEM contents. + */ + [[nodiscard]] auto convertToPEMString() const -> std::string + { + const size_t buflen = 4096; + + std::string output{}; + output.resize(buflen); + unique_ptr outputFile{fmemopen(output.data(), output.length() - 1, "w"), &std::fclose}; + convertToPEMFile(*outputFile); + std::fflush(outputFile.get()); + output.resize(std::ftell(outputFile.get())); + + return output; + }; + [[nodiscard]] virtual std::string sign(const std::string& msg) const =0; [[nodiscard]] virtual std::string hash(const std::string& msg) const diff --git a/pdns/test-signers.cc b/pdns/test-signers.cc index 1abb7d960f..0a1dbd8e7c 100644 --- a/pdns/test-signers.cc +++ b/pdns/test-signers.cc @@ -465,24 +465,10 @@ BOOST_FIXTURE_TEST_CASE(test_generic_signers, Fixture) test_generic_signer(pemKey, pemDRC, signer, message); - const size_t buflen = 4096; - - std::string dckePEMOutput{}; - dckePEMOutput.resize(buflen); - unique_ptr dckePEMOutputFp{fmemopen(static_cast(dckePEMOutput.data()), dckePEMOutput.length() - 1, "w"), &std::fclose}; - dcke->convertToPEMFile(*dckePEMOutputFp); - std::fflush(dckePEMOutputFp.get()); - dckePEMOutput.resize(std::ftell(dckePEMOutputFp.get())); - + auto dckePEMOutput = dcke->convertToPEMString(); BOOST_CHECK_EQUAL(dckePEMOutput, signer.pem); - std::string pemKeyOutput{}; - pemKeyOutput.resize(buflen); - unique_ptr pemKeyOutputFp{fmemopen(static_cast(pemKeyOutput.data()), pemKeyOutput.length() - 1, "w"), &std::fclose}; - pemKey->convertToPEMFile(*pemKeyOutputFp); - std::fflush(pemKeyOutputFp.get()); - pemKeyOutput.resize(std::ftell(pemKeyOutputFp.get())); - + auto pemKeyOutput = pemKey->convertToPEMString(); BOOST_CHECK_EQUAL(pemKeyOutput, signer.pem); } } -- 2.47.2