31#ifndef ASYNC_CERT_SIGNING_REQ_INCLUDED 
   32#define ASYNC_CERT_SIGNING_REQ_INCLUDED 
   41#include <openssl/x509.h> 
  136      m_req = X509_REQ_new();
 
  137      assert(m_req != 
nullptr);
 
 
  154      : m_req(other.m_req), m_file_path(other.m_file_path)
 
  156      other.m_req = 
nullptr;
 
  157      other.m_file_path.clear();
 
 
  166#if OPENSSL_VERSION_MAJOR >= 3 
  167      m_req = X509_REQ_dup(other);
 
  169      m_req = X509_REQ_dup(
const_cast<X509_REQ*
>(other.m_req));
 
  171      m_file_path = other.m_file_path;
 
 
  188      if (m_req != 
nullptr)
 
  190        X509_REQ_free(m_req);
 
 
  203#if OPENSSL_VERSION_MAJOR >= 3 
  204      m_req = X509_REQ_dup(other);
 
  206      m_req = X509_REQ_dup(
const_cast<X509_REQ*
>(other.m_req));
 
  208      m_file_path = other.m_file_path;
 
 
  220      m_file_path = other.m_file_path;
 
  221      other.m_req = 
nullptr;
 
  222      other.m_file_path.clear();
 
 
  230    operator const X509_REQ*() 
const { 
return m_req; }
 
  238      if (m_req != 
nullptr)
 
  240        X509_REQ_free(m_req);
 
 
  252      if (m_req != 
nullptr)
 
  254        X509_REQ_free(m_req);
 
  256      m_req = X509_REQ_new();
 
 
  263    bool isNull(
void)
 const { 
return (m_req == 
nullptr); }
 
  275      assert(m_req != 
nullptr);
 
  276      return (X509_REQ_set_version(m_req, 
version) == 1);
 
 
  283    long version(
void)
 const { 
return X509_REQ_get_version(m_req); }
 
  294      assert(m_req != 
nullptr);
 
  295      X509_NAME* name = X509_REQ_get_subject_name(m_req);
 
  298        name = X509_NAME_new();
 
  300      assert(name != 
nullptr);
 
  301      bool success = (X509_NAME_add_entry_by_txt(name, field.c_str(),
 
  303            reinterpret_cast<const unsigned char*
>(value.c_str()),
 
  304            value.size(), -1, 0) == 1);
 
  305      success = success && (X509_REQ_set_subject_name(m_req, name) == 1);
 
 
  316      assert(m_req != 
nullptr);
 
  317      return X509_REQ_set_subject_name(m_req, name);
 
 
  326      if (m_req == 
nullptr) 
return nullptr;
 
  327      return X509_REQ_get_subject_name(m_req);
 
 
  336      std::vector<unsigned char> md;
 
  340        auto mdtype = EVP_sha256();
 
  343        unsigned int mdlen = EVP_MD_size(mdtype);
 
  345        if (X509_NAME_digest(subj, mdtype, md.data(), &mdlen) != 1)
 
 
  363        BIO *mem = BIO_new(BIO_s_mem());
 
  364        assert(mem != 
nullptr);
 
  366        int len = X509_NAME_print_ex(mem, nm, 0,
 
  367            XN_FLAG_ONELINE & ~ASN1_STRFLGS_ESC_MSB);
 
  371          len = BIO_read(mem, buf, 
sizeof(buf));
 
  374            str = std::string(buf, len);
 
 
  397#if OPENSSL_VERSION_MAJOR >= 3 
  398      int lastpos = X509_NAME_get_index_by_NID(subj, NID_commonName, -1);
 
  400      auto s = X509_NAME_dup(
const_cast<X509_NAME*
>(subj));
 
  401      int lastpos = X509_NAME_get_index_by_NID(s, NID_commonName, -1);
 
  407        X509_NAME_ENTRY *e = X509_NAME_get_entry(subj, lastpos);
 
  408        ASN1_STRING *d = X509_NAME_ENTRY_get_data(e);
 
  409        cn = 
reinterpret_cast<const char*
>(ASN1_STRING_get0_data(d));
 
 
  420      assert(m_req != 
nullptr);
 
  421#if OPENSSL_VERSION_MAJOR >= 3 
  422      X509_REQ_add_extensions(m_req, exts);
 
  424      auto e = sk_X509_EXTENSION_dup(exts);
 
  425      X509_REQ_add_extensions(m_req, e);
 
  426      sk_X509_EXTENSION_free(e);
 
 
  436      assert(m_req != 
nullptr);
 
 
  446      assert(m_req != 
nullptr);
 
  447      return SslKeypair(X509_REQ_get_pubkey(m_req));
 
 
  457      assert(m_req != 
nullptr);
 
  458      return (X509_REQ_set_pubkey(m_req, pubkey) == 1);
 
 
  468      assert(m_req != 
nullptr);
 
  469      auto md = EVP_sha256();
 
  471      auto md_size = EVP_MD_size(md);
 
  472      return (X509_REQ_sign(m_req, privkey, md) == md_size);
 
 
  484      assert(m_req != 
nullptr);
 
  485      return (X509_REQ_verify(m_req, pubkey) == 1);
 
 
  492    std::vector<unsigned char> 
digest(
void)
 const 
  494      assert(m_req != 
nullptr);
 
  495      std::vector<unsigned char> md;
 
  496      auto mdtype = EVP_sha256();
 
  498      unsigned int mdlen = EVP_MD_size(mdtype);
 
  500      unsigned int len = md.size();
 
  501      if (X509_REQ_digest(m_req, mdtype, md.data(), &len) != 1)
 
 
  515      BIO *mem = BIO_new(BIO_s_mem());
 
  516      BIO_puts(mem, 
pem.c_str());
 
  517      if (m_req != 
nullptr)
 
  519        X509_REQ_free(m_req);
 
  521      m_req = PEM_read_bio_X509_REQ(mem, 
nullptr, 
nullptr, 
nullptr);
 
  523      return (m_req != 
nullptr);
 
 
  533      m_file_path = filename;
 
  534      if (m_req != 
nullptr)
 
  536        X509_REQ_free(m_req);
 
  539      FILE *p_file = fopen(filename.c_str(), 
"r");
 
  540      if (p_file == 
nullptr)
 
  544      m_req = PEM_read_X509_REQ(p_file, 
nullptr, 
nullptr, 
nullptr);
 
  546      return (m_req != 
nullptr);
 
 
  556    const std::string& 
filePath(
void)
 const { 
return m_file_path; }
 
  565      assert(m_req != 
nullptr);
 
  570      int ret = PEM_write_X509_REQ(f, m_req);
 
 
  599    std::string 
pem(
void)
 const 
  601      assert(m_req != 
nullptr);
 
  602      BIO *mem = BIO_new(BIO_s_mem());
 
  603      int ret = PEM_write_bio_X509_REQ(mem, m_req);
 
  606      int len = BIO_read(mem, buf, 
sizeof(buf));
 
  609      return std::string(buf, len);
 
 
  616    void print(
const std::string& prefix=
"")
 const 
  629        std::cout << prefix << 
"Subject Alt Name : " << sanstr << 
"\n";
 
 
  639    X509_REQ*     m_req       = 
nullptr;
 
  640    std::string   m_file_path;
 
 
Represent private and public keys.
 
A class representing X.509 v3 extensions.
 
A class representing a certificate signing request.
 
bool setSubjectName(X509_NAME *name)
Set the subject name from a X509_NAME pointer.
 
bool readPemFile(const std::string &filename)
Read PEM formatted CSR data from file into this object.
 
SslCertSigningReq & operator=(SslCertSigningReq &other)
Copy assignment operator.
 
void addExtensions(SslX509Extensions &exts)
Add extensions to this CSR.
 
const std::string & filePath(void) const
Get the file path associated with this CSR.
 
SslCertSigningReq(void)
Default constructor.
 
void set(X509_REQ *req)
Initialize this object from an existing X509_REQ object.
 
const X509_NAME * subjectName(void) const
Return the subject name as a X509_NAME pointer.
 
~SslCertSigningReq(void)
Constructor taking PEM data.
 
bool appendPemFile(const std::string &filename)
Append the CSR data to a PEM file.
 
bool setPublicKey(SslKeypair &pubkey)
Set the public key.
 
void print(const std::string &prefix="") const
Print the info in this CSR to std::cout.
 
SslCertSigningReq(SslCertSigningReq &&other)
Move constructor.
 
bool writePemFile(const std::string &filename)
Write the CSR data to a PEM file.
 
SslKeypair publicKey(void) const
Get the public key.
 
SslX509Extensions extensions(void) const
Get the extensions in this CSR.
 
SslCertSigningReq(X509_REQ *req)
Constructor using existing X509_REQ.
 
bool isNull(void) const
Check if this object is empty.
 
std::vector< unsigned char > subjectDigest(void) const
Get the subject digest.
 
long version(void) const
Get the version of this CSR.
 
std::string pem(void) const
Get the data in this CSR as a PEM string.
 
void clear(void)
Remove all information in this object.
 
SslCertSigningReq & operator=(SslCertSigningReq &&other)
Move assigned operator.
 
bool writePemFile(FILE *f)
Write the CSR data to a PEM file.
 
bool verify(SslKeypair &pubkey) const
Verify the signature of this CSR.
 
SslCertSigningReq(SslCertSigningReq &other)
Copy constructor.
 
std::string commonName(void) const
Get the subject common name.
 
bool sign(SslKeypair &privkey)
Sign the CSR using the given private key.
 
std::string subjectNameString(void) const
Get the subject DN as a string.
 
bool addSubjectName(const std::string &field, const std::string &value)
Add a subject name component.
 
bool readPem(const std::string &pem)
Read PEM formatted CSR data into this object.
 
std::vector< unsigned char > digest(void) const
Get the sha256 digest of this CSR.
 
bool setVersion(long version)
Set the version of the request.
 
A class representing private and public keys.
 
std::string toString(int type=-1) const
Convert all SANs to a string.
 
A class representing X.509 extensions.
 
SslX509ExtSubjectAltName subjectAltName(void) const
Get the subject alternative names extension.
 
Namespace for the asynchronous programming classes.