*
* \return An ED25519 key engine populated with the contents of the PEM file.
*/
- void createFromPEMFile(DNSKEYRecordContent& drc, std::FILE& inputFile, const std::string& filename) override;
+ void createFromPEMFile(DNSKEYRecordContent& drc, std::FILE& inputFile, std::optional<std::reference_wrapper<const std::string>> filename = std::nullopt) override;
/**
* \brief Writes this key's contents to a file.
}
#if defined(HAVE_LIBCRYPTO_ED25519)
-void DecafED25519DNSCryptoKeyEngine::createFromPEMFile(DNSKEYRecordContent& drc, std::FILE& inputFile, const string& filename)
+void DecafED25519DNSCryptoKeyEngine::createFromPEMFile(DNSKEYRecordContent& drc, std::FILE& inputFile, std::optional<std::reference_wrapper<const std::string>> filename)
{
drc.d_algorithm = d_algorithm;
auto key = std::unique_ptr<EVP_PKEY, decltype(&EVP_PKEY_free)>(PEM_read_PrivateKey(&inputFile, nullptr, nullptr, nullptr), &EVP_PKEY_free);
if (key == nullptr) {
- throw runtime_error(getName() + ": Failed to read private key from PEM file `" + filename + "`");
+ if (filename.has_value()) {
+ throw runtime_error(getName() + ": Failed to read private key from PEM file `" + filename->get() + "`");
+ }
+
+ throw runtime_error(getName() + ": Failed to read private key from PEM contents");
}
std::size_t keylen = DECAF_EDDSA_25519_PRIVATE_BYTES;
int ret = EVP_PKEY_get_raw_private_key(key.get(), d_seckey, &keylen);
if (ret == 0) {
- throw runtime_error(getName() + ": Failed to get private key from PEM file contents `" + filename + "`");
+ if (filename.has_value()) {
+ throw runtime_error(getName() + ": Failed to get private key from PEM file contents `" + filename->get() + "`");
+ }
+
+ throw runtime_error(getName() + ": Failed to get private key from PEM contents");
}
keylen = DECAF_EDDSA_25519_PUBLIC_BYTES;
ret = EVP_PKEY_get_raw_public_key(key.get(), d_pubkey, &keylen);
if (ret == 0) {
- throw runtime_error(getName() + ": Failed to get public key from PEM file contents `" + filename + "`");
+ if (filename.has_value()) {
+ throw runtime_error(getName() + ": Failed to get public key from PEM file contents `" + filename->get() + "`");
+ }
+
+ throw runtime_error(getName() + ": Failed to get public key from PEM contents");
}
}
*
* \return An ED448 key engine populated with the contents of the PEM file.
*/
- void createFromPEMFile(DNSKEYRecordContent& drc, std::FILE& inputFile, const std::string& filename) override;
+ void createFromPEMFile(DNSKEYRecordContent& drc, std::FILE& inputFile, std::optional<std::reference_wrapper<const std::string>> filename = std::nullopt) override;
/**
* \brief Writes this key's contents to a file.
}
#if defined(HAVE_LIBCRYPTO_ED448)
-void DecafED448DNSCryptoKeyEngine::createFromPEMFile(DNSKEYRecordContent& drc, std::FILE& inputFile, const string& filename)
+void DecafED448DNSCryptoKeyEngine::createFromPEMFile(DNSKEYRecordContent& drc, std::FILE& inputFile, std::optional<std::reference_wrapper<const std::string>> filename)
{
drc.d_algorithm = d_algorithm;
auto key = std::unique_ptr<EVP_PKEY, decltype(&EVP_PKEY_free)>(PEM_read_PrivateKey(&inputFile, nullptr, nullptr, nullptr), &EVP_PKEY_free);
if (key == nullptr) {
- throw runtime_error(getName() + ": Failed to read private key from PEM file `" + filename + "`");
+ if (filename.has_value()) {
+ throw runtime_error(getName() + ": Failed to read private key from PEM file `" + filename->get() + "`");
+ }
+
+ throw runtime_error(getName() + ": Failed to read private key from PEM contents");
}
std::size_t keylen = DECAF_EDDSA_448_PRIVATE_BYTES;
int ret = EVP_PKEY_get_raw_private_key(key.get(), d_seckey, &keylen);
if (ret == 0) {
- throw runtime_error(getName() + ": Failed to get private key from PEM file contents `" + filename + "`");
+ if (filename.has_value()) {
+ throw runtime_error(getName() + ": Failed to get private key from PEM file contents `" + filename->get() + "`");
+ }
+
+ throw runtime_error(getName() + ": Failed to get private key from PEM contents");
}
keylen = DECAF_EDDSA_448_PUBLIC_BYTES;
ret = EVP_PKEY_get_raw_public_key(key.get(), d_pubkey, &keylen);
if (ret == 0) {
- throw runtime_error(getName() + ": Failed to get public key from PEM file contents `" + filename + "`");
+ if (filename.has_value()) {
+ throw runtime_error(getName() + ": Failed to get public key from PEM file contents `" + filename->get() + "`");
+ }
+
+ throw runtime_error(getName() + ": Failed to get public key from PEM contents");
}
}
using storvector_t = std::vector<std::pair<std::string, std::string>>;
virtual void create(unsigned int bits)=0;
- virtual void createFromPEMFile(DNSKEYRecordContent& /* drc */, std::FILE& /* inputFile */, const std::string& /* filename */)
+ virtual void createFromPEMFile(DNSKEYRecordContent& /* drc */, std::FILE& /* inputFile */, const std::optional<std::reference_wrapper<const std::string>> filename = std::nullopt)
{
- throw std::runtime_error("Can't create key from PEM file");
+ if (filename.has_value()) {
+ throw std::runtime_error("Can't create key from PEM file `" + filename->get() + "`");
+ }
+
+ throw std::runtime_error("Can't create key from PEM contents");
}
[[nodiscard]] virtual storvector_t convertToISCVector() const =0;
*
* \return An RSA key engine populated with the contents of the PEM file.
*/
- void createFromPEMFile(DNSKEYRecordContent& drc, std::FILE& inputFile, const std::string& filename) override;
+ void createFromPEMFile(DNSKEYRecordContent& drc, std::FILE& inputFile, std::optional<std::reference_wrapper<const std::string>> filename = std::nullopt) override;
/**
* \brief Writes this key's contents to a file.
#endif
}
-void OpenSSLRSADNSCryptoKeyEngine::createFromPEMFile(DNSKEYRecordContent& drc, std::FILE& inputFile, const std::string& filename)
+void OpenSSLRSADNSCryptoKeyEngine::createFromPEMFile(DNSKEYRecordContent& drc, std::FILE& inputFile, const std::optional<std::reference_wrapper<const std::string>> filename)
{
drc.d_algorithm = d_algorithm;
#if OPENSSL_VERSION_MAJOR >= 3
EVP_PKEY* key = nullptr;
if (PEM_read_PrivateKey(&inputFile, &key, nullptr, nullptr) == nullptr) {
- throw pdns::OpenSSL::error(getName(), "Could not read private key from PEM file `" + filename + "`");
+ if (filename.has_value()) {
+ throw pdns::OpenSSL::error(getName(), "Could not read private key from PEM file `" + filename->get() + "`");
+ }
+
+ throw pdns::OpenSSL::error(getName(), "Could not read private key from PEM contents");
}
d_key.reset(key);
#else
d_key = Key(PEM_read_RSAPrivateKey(&inputFile, nullptr, nullptr, nullptr), &RSA_free);
if (d_key == nullptr) {
- throw runtime_error(getName() + ": Failed to read private key from PEM file `" + filename + "`");
+ if (filename.has_value()) {
+ throw runtime_error(getName() + ": Failed to read private key from PEM file `" + filename->get() + "`");
+ }
+
+ throw runtime_error(getName() + ": Failed to read private key from PEM contents");
}
#endif
}
*
* \return An ECDSA key engine populated with the contents of the PEM file.
*/
- void createFromPEMFile(DNSKEYRecordContent& drc, std::FILE& inputFile, const std::string& filename) override;
+ void createFromPEMFile(DNSKEYRecordContent& drc, std::FILE& inputFile, std::optional<std::reference_wrapper<const std::string>> filename = std::nullopt) override;
/**
* \brief Writes this key's contents to a file.
#endif
}
-void OpenSSLECDSADNSCryptoKeyEngine::createFromPEMFile(DNSKEYRecordContent& drc, std::FILE& inputFile, const string& filename)
+void OpenSSLECDSADNSCryptoKeyEngine::createFromPEMFile(DNSKEYRecordContent& drc, std::FILE& inputFile, std::optional<std::reference_wrapper<const std::string>> filename)
{
drc.d_algorithm = d_algorithm;
#if OPENSSL_VERSION_MAJOR >= 3
EVP_PKEY* key = nullptr;
if (PEM_read_PrivateKey(&inputFile, &key, nullptr, nullptr) == nullptr) {
- throw pdns::OpenSSL::error(getName(), "Failed to read private key from PEM file `" + filename + "`");
+ if (filename.has_value()) {
+ throw pdns::OpenSSL::error(getName(), "Failed to read private key from PEM file `" + filename->get() + "`");
+ }
+
+ throw pdns::OpenSSL::error(getName(), "Failed to read private key from PEM contents");
}
d_eckey.reset(key);
#else
d_eckey = Key(PEM_read_ECPrivateKey(&inputFile, nullptr, nullptr, nullptr), &EC_KEY_free);
if (d_eckey == nullptr) {
- throw runtime_error(getName() + ": Failed to read private key from PEM file `" + filename + "`");
+ if (filename.has_value()) {
+ throw runtime_error(getName() + ": Failed to read private key from PEM file `" + filename->get() + "`");
+ }
+
+ throw runtime_error(getName() + ": Failed to read private key from PEM contents");
}
int ret = EC_KEY_set_group(d_eckey.get(), d_group.get());
*
* \return An EDDSA key engine populated with the contents of the PEM file.
*/
- void createFromPEMFile(DNSKEYRecordContent& drc, std::FILE& inputFile, const std::string& filename) override;
+ void createFromPEMFile(DNSKEYRecordContent& drc, std::FILE& inputFile, std::optional<std::reference_wrapper<const std::string>> filename = std::nullopt) override;
/**
* \brief Writes this key's contents to a file.
d_edkey.reset(newKey);
}
-void OpenSSLEDDSADNSCryptoKeyEngine::createFromPEMFile(DNSKEYRecordContent& drc, std::FILE& inputFile, const string& filename)
+void OpenSSLEDDSADNSCryptoKeyEngine::createFromPEMFile(DNSKEYRecordContent& drc, std::FILE& inputFile, std::optional<std::reference_wrapper<const std::string>> filename)
{
drc.d_algorithm = d_algorithm;
d_edkey = Key(PEM_read_PrivateKey(&inputFile, nullptr, nullptr, nullptr), &EVP_PKEY_free);
if (d_edkey == nullptr) {
- throw pdns::OpenSSL::error(getName(), "Failed to read private key from PEM file `" + filename + "`");
+ if (filename.has_value()) {
+ throw pdns::OpenSSL::error(getName(), "Failed to read private key from PEM file `" + filename->get() + "`");
+ }
+
+ throw pdns::OpenSSL::error(getName(), "Failed to read private key from PEM contents");
}
}
*
* \return An ED25519 key engine populated with the contents of the PEM file.
*/
- void createFromPEMFile(DNSKEYRecordContent& drc, std::FILE& inputFile, const std::string& filename) override;
+ void createFromPEMFile(DNSKEYRecordContent& drc, std::FILE& inputFile, std::optional<std::reference_wrapper<const std::string>> filename = std::nullopt) override;
/**
* \brief Writes this key's contents to a file.
}
#if defined(HAVE_LIBCRYPTO_ED25519)
-void SodiumED25519DNSCryptoKeyEngine::createFromPEMFile(DNSKEYRecordContent& drc, std::FILE& inputFile, const string& filename)
+void SodiumED25519DNSCryptoKeyEngine::createFromPEMFile(DNSKEYRecordContent& drc, std::FILE& inputFile, std::optional<std::reference_wrapper<const std::string>> filename)
{
drc.d_algorithm = d_algorithm;
auto key = std::unique_ptr<EVP_PKEY, decltype(&EVP_PKEY_free)>(PEM_read_PrivateKey(&inputFile, nullptr, nullptr, nullptr), &EVP_PKEY_free);
if (key == nullptr) {
- throw runtime_error(getName() + ": Failed to read private key from PEM file `" + filename + "`");
+ if (filename.has_value()) {
+ throw runtime_error(getName() + ": Failed to read private key from PEM file `" + filename->get() + "`");
+ }
+
+ throw runtime_error(getName() + ": Failed to read private key from PEM contents");
}
// The secret key is 64 bytes according to libsodium. But OpenSSL returns 32 in
std::size_t secKeyLen = crypto_sign_ed25519_SECRETKEYBYTES;
int ret = EVP_PKEY_get_raw_private_key(key.get(), d_seckey, &secKeyLen);
if (ret == 0) {
- throw runtime_error(getName() + ": Failed to get private key from PEM file contents `" + filename + "`");
+ if (filename.has_value()) {
+ throw runtime_error(getName() + ": Failed to get private key from PEM file contents `" + filename->get() + "`");
+ }
+
+ throw runtime_error(getName() + ": Failed to get private key from PEM contents");
}
std::size_t pubKeyLen = crypto_sign_ed25519_PUBLICKEYBYTES;
ret = EVP_PKEY_get_raw_public_key(key.get(), d_pubkey, &pubKeyLen);
if (ret == 0) {
- throw runtime_error(getName() + ": Failed to get public key from PEM file contents `" + filename + "`");
+ if (filename.has_value()) {
+ throw runtime_error(getName() + ": Failed to get public key from PEM file contents `" + filename->get() + "`");
+ }
+
+ throw runtime_error(getName() + ": Failed to get public key from PEM contents");
}
// It looks like libsodium expects the public key to be appended to the private key,