]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Introduce DNSCryptoKeyEngine::convertToPEMString 12540/head
authorFred Morcos <fred.morcos@open-xchange.com>
Mon, 13 Feb 2023 08:53:06 +0000 (09:53 +0100)
committerFred Morcos <fred.morcos@open-xchange.com>
Tue, 14 Feb 2023 14:19:33 +0000 (15:19 +0100)
pdns/dnssecinfra.hh
pdns/test-signers.cc

index b0f80af2464b18cf890e46e1878d0e94724a21a3..f2d00f28f84cc18a0250757f4bf945586b2c4f74 100644 (file)
@@ -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<std::FILE, decltype(&std::fclose)> 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
index 1abb7d960f81f80623c88377d7394a4f28fb79d6..0a1dbd8e7c15c3ab6018bb937f0567102d9e509a 100644 (file)
@@ -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<std::FILE, decltype(&std::fclose)> dckePEMOutputFp{fmemopen(static_cast<void*>(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<std::FILE, decltype(&std::fclose)> pemKeyOutputFp{fmemopen(static_cast<void*>(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);
   }
 }