19#include <openssl/ssl.h>
20#include <openssl/bio.h>
56 other.pkey_ =
nullptr;
76 other.pkey_ =
nullptr;
84 return pkey_ !=
nullptr;
86 ::EVP_PKEY *
obj()
const
93 switch (::EVP_PKEY_id(
pkey_))
115 int ret = ::i2d_PrivateKey(
pkey_, NULL);
130 BIO *bio = ::BIO_new_mem_buf(
const_cast<char *
>(pkey_txt.c_str()), numeric_cast<int>(pkey_txt.length()));
137 throw OpenSSLException(std::string(
"PKey::parse_pem: error in ") + title + std::string(
":"));
147 BIO *bio = ::BIO_new(BIO_s_mem());
148 const int ret = ::PEM_write_bio_PrivateKey(bio,
pkey_,
nullptr,
nullptr, 0,
nullptr,
nullptr);
157 const size_t buf_len = ::BIO_get_mem_data(bio, &temp);
158 std::string
ret = std::string(temp, buf_len);
176 const PKey *self = (
PKey *)userdata;
180 auto len = std::strlen(buf);
181 if (is_safe_conversion<int>(len))
182 return static_cast<int>(len);
190 ::EVP_PKEY_free(
pkey_);
193#if OPENSSL_VERSION_NUMBER < 0x30000000L
194 static ::EVP_PKEY *
dup(const ::EVP_PKEY *pkey)
199 ::EVP_PKEY *pDupKey = ::EVP_PKEY_new();
200 ::RSA *pRSA = ::EVP_PKEY_get1_RSA(
const_cast<::EVP_PKEY *
>(pkey));
201 ::RSA *pRSADupKey = ::RSAPrivateKey_dup(pRSA);
203 ::EVP_PKEY_set1_RSA(pDupKey, pRSADupKey);
204 ::RSA_free(pRSADupKey);
211 static ::EVP_PKEY *
dup(const ::EVP_PKEY *pkey)
214 return EVP_PKEY_dup(
const_cast<EVP_PKEY *
>(pkey));
PKey(PKey &&other) noexcept
static int pem_password_callback(char *buf, int size, int rwflag, void *userdata)
size_t key_length() const
PKey & operator=(const PKey &other)
void set_private_key_password(const std::string &pwd)
PKey & operator=(PKey &&other) noexcept
void parse_pem(const std::string &pkey_txt, const std::string &title, SSLLib::Ctx libctx)
PKType::Type key_type() const
PKey(const std::string &pkey_txt, const std::string &title, SSLLib::Ctx ctx)
std::string render_pem() const
::EVP_PKEY * dup(const ::EVP_PKEY *pkey)
static EVP_PKEY * PEM_read_bio_PrivateKey_ex(BIO *bp, EVP_PKEY **x, pem_password_cb *cb, void *u, void *libctx, const char *propq)
void strncpynt(char *dest, const char *src, size_t maxlen)