From: Fred Morcos Date: Mon, 13 Feb 2023 08:53:06 +0000 (+0100) Subject: Introduce DNSCryptoKeyEngine::convertToPEMString X-Git-Tag: dnsdist-1.8.0-rc1~18^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=d0fa1838ce067743db2d7e2d10ed9612111e6ff1;p=thirdparty%2Fpdns.git Introduce DNSCryptoKeyEngine::convertToPEMString --- 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); } }