31#ifndef ASYNC_SSL_KEYPAIR_INCLUDED
32#define ASYNC_SSL_KEYPAIR_INCLUDED
43#include <openssl/evp.h>
44#include <openssl/rsa.h>
45#include <openssl/bn.h>
46#include <openssl/pem.h>
146 m_pkey = other.m_pkey;
147 other.m_pkey =
nullptr;
156 EVP_PKEY_up_ref(other.m_pkey);
157 m_pkey = other.m_pkey;
167 EVP_PKEY_up_ref(other.m_pkey);
168 m_pkey = other.m_pkey;
177 EVP_PKEY_free(m_pkey);
185 bool isNull(
void)
const {
return (m_pkey ==
nullptr); }
194 EVP_PKEY_free(m_pkey);
195#if OPENSSL_VERSION_MAJOR >= 3
196 m_pkey = EVP_RSA_gen(bits);
197 return (m_pkey !=
nullptr);
199 m_pkey = EVP_PKEY_new();
200 if (m_pkey ==
nullptr)
205 BIGNUM* rsa_f4 = BN_new();
206 if (rsa_f4 ==
nullptr)
208 EVP_PKEY_free(m_pkey);
212 BN_set_word(rsa_f4, RSA_F4);
216 RSA* rsa = RSA_new();
220 EVP_PKEY_free(m_pkey);
224 int ret = RSA_generate_key_ex(
234 EVP_PKEY_free(m_pkey);
238 ret = EVP_PKEY_assign_RSA(m_pkey, rsa);
243 EVP_PKEY_free(m_pkey);
266 EVP_PKEY_free(m_pkey);
267#if OPENSSL_VERSION_MAJOR >= 3
268 m_pkey = EVP_PKEY_new_raw_private_key(type,
nullptr,
269 reinterpret_cast<const unsigned char*
>(key.data()), key.size());
271 m_pkey = EVP_PKEY_new_mac_key(type,
nullptr,
272 reinterpret_cast<const unsigned char*
>(key.data()), key.size());
274 return (m_pkey !=
nullptr);
283 assert(m_pkey !=
nullptr);
284 BIO *mem = BIO_new(BIO_s_mem());
285 assert(mem !=
nullptr);
286 int ret = PEM_write_bio_PrivateKey(
299 int len = BIO_read(mem, buf,
sizeof(buf));
301 pem = std::string(buf, len);
314 BIO *mem = BIO_new(BIO_s_mem());
315 BIO_puts(mem, pem.c_str());
316 if (m_pkey !=
nullptr)
318 EVP_PKEY_free(m_pkey);
320 m_pkey = PEM_read_bio_PrivateKey(mem,
nullptr,
nullptr,
nullptr);
322 return (m_pkey !=
nullptr);
332 FILE* f = fopen(filename.c_str(),
"wb");
337 if (fchmod(fileno(f), 0600) != 0)
342 int ret = PEM_write_PrivateKey(
370 FILE* f = fopen(filename.c_str(),
"rb");
375 EVP_PKEY* pkey = PEM_read_PrivateKey(f, NULL, NULL, NULL);
385 EVP_PKEY_free(m_pkey);
396 assert(m_pkey !=
nullptr);
397 BIO *mem = BIO_new(BIO_s_mem());
398 assert(mem !=
nullptr);
399 int ret = PEM_write_bio_PUBKEY(mem, m_pkey);
404 int len = BIO_read(mem, buf,
sizeof(buf));
406 pem = std::string(buf, len);
419 if (m_pkey !=
nullptr)
421 EVP_PKEY_free(m_pkey);
423 BIO* mem = BIO_new(BIO_s_mem());
424 assert(mem !=
nullptr);
425 int rc = BIO_puts(mem, pem.c_str());
426 std::cout <<
"### rc=" << rc << std::endl;
429 m_pkey = PEM_read_bio_PUBKEY(mem,
nullptr,
nullptr,
nullptr);
432 return (m_pkey !=
nullptr);
439 operator EVP_PKEY*(void) {
return m_pkey; }
440 operator const EVP_PKEY*(void)
const {
return m_pkey; }
449#if OPENSSL_VERSION_MAJOR >= 3
450 return (EVP_PKEY_eq(m_pkey, other.m_pkey) != 1);
452 return (EVP_PKEY_cmp(m_pkey, other.m_pkey) != 1);
459 EVP_PKEY* m_pkey =
nullptr;
A class representing private and public keys.
std::string publicKeyPem(void) const
Get the public key on PEM form.
bool readPrivateKeyFile(const std::string &filename)
Read key data from PEM file.
bool privateKeyFromPem(const std::string &pem)
Create key from the given PEM data.
SslKeypair(SslKeypair &other)
Copy constructor.
SslKeypair & operator=(SslKeypair &other)
Copy assignment operator.
bool writePrivateKeyFile(const std::string &filename)
Write key data to file on PEM format.
bool newRawPrivateKey(int type, const T &key)
Generate a key using the given algorithm and raw key data.
SslKeypair(SslKeypair &&other)
Move constructor.
bool isNull(void) const
Check if the object is empty.
bool publicKeyFromPem(const std::string &pem)
Create public key from PEM string.
std::string privateKeyPem(void) const
Return the private key on PEM form.
~SslKeypair(void)
Destructor.
SslKeypair(EVP_PKEY *pkey)
Constructor.
SslKeypair(void)
Default constructor.
bool operator!=(const SslKeypair &other) const
Check if two keys is not equal to each other.
bool generate(unsigned int bits)
Generate a new RSA keypair.
Namespace for the asynchronous programming classes.