Async 1.8.0
Async::SslX509Extensions Class Reference

A class representing X.509 extensions. More...

#include <AsyncSslX509Extensions.h>

Public Member Functions

 SslX509Extensions (void)
 Default constructor.
 
 SslX509Extensions (STACK_OF(X509_EXTENSION) *exts)
 Constructor.
 
 SslX509Extensions (SslX509Extensions &&other)
 Move Constructor.
 
 SslX509Extensions (const SslX509Extensions &other)
 Copy constructor.
 
SslX509Extensionsoperator= (const SslX509Extensions &)=delete
 Disallow copy assignment.
 
 ~SslX509Extensions (void)
 Destructor.
 
bool addBasicConstraints (const std::string &bc)
 Add basic constraints extension.
 
bool addKeyUsage (const std::string &ku)
 Add key usage.
 
bool addExtKeyUsage (const std::string &eku)
 Add extended key usage.
 
bool addSubjectAltNames (const std::string &san)
 Add subject alternative names.
 
SslX509ExtSubjectAltName subjectAltName (void) const
 Get the subject alternative names extension.
 
bool addExtension (const SslX509ExtSubjectAltName &san)
 Add a subject alternative names object.
 
 operator const STACK_OF (X509_EXTENSION) *() const
 Cast to a pointer to a stack of X509_EXTENSION objects.
 

Detailed Description

A class representing X.509 extensions.

Author
Tobias Blomberg / SM0SVX
Date
2022-05-22
#include <AsyncSslX509.h>
int main(void)
{
// Create a key pair for the CA
if (!ca_pkey.generate(2048))
{
std::cout << "*** ERROR: Failed to generate CA key" << std::endl;
return 1;
}
if (!ca_pkey.writePrivateKeyFile("demo_ca.key"))
{
std::cout << "*** WARNING: Failed to write CA key file" << std::endl;
}
// Create a CA certificate and sign it with the key above
Async::SslX509 ca_cert;
ca_cert.setSerialNumber(1);
ca_cert.addIssuerName("CN", "Demo Root CA");
ca_cert.addIssuerName("L", "My City");
ca_cert.addIssuerName("C", "XX");
ca_cert.setSubjectName(ca_cert.issuerName());
ca_exts.addBasicConstraints("critical, CA:TRUE");
ca_exts.addKeyUsage("critical, cRLSign, digitalSignature, keyCertSign");
ca_exts.addSubjectAltNames("email:ca@example.org");
ca_cert.addExtensions(ca_exts);
time_t t = time(nullptr);
ca_cert.setNotBefore(t);
ca_cert.setNotAfter(t + 24*3600);
ca_cert.setPublicKey(ca_pkey);
ca_cert.sign(ca_pkey);
std::cout << "--------------- CA Certificate ----------------" << std::endl;
ca_cert.print();
std::cout << "-----------------------------------------------" << std::endl;
if (!ca_cert.writePemFile("demo_ca.crt"))
{
std::cout << "*** WARNING: Failed to write CA certificate file"
<< std::endl;
}
// Create a key pair for the server certificate
Async::SslKeypair cert_pkey;
if (!cert_pkey.generate(2048))
{
std::cout << "*** ERROR: Failed to generate server certificate key"
<< std::endl;
return 1;
}
if (!cert_pkey.writePrivateKeyFile("demo.key"))
{
std::cout << "*** WARNING: Failed to write CA key file" << std::endl;
}
// Create a Certificate Signing Request
csr.addSubjectName("CN", "hostname.example.org");
csr.addSubjectName("L", "My City");
csr.addSubjectName("C", "XX");
"DNS:hostname.example.org"
", DNS:alias.example.org"
", DNS:localhost"
", IP:127.0.0.1"
", email:admin@example.org"
", URI:https://www.example.org"
", otherName:msUPN;UTF8:sb@sb.local");
csr.addExtensions(csr_exts);
csr.setPublicKey(cert_pkey);
csr.sign(cert_pkey);
std::cout << "--------- Certificate Signing Request ---------" << std::endl;
csr.print();
std::cout << "-----------------------------------------------" << std::endl;
if (!csr.writePemFile("demo.csr"))
{
std::cout << "*** WARNING: Failed to write CSR file" << std::endl;
}
std::cout << "The CSR verification "
<< (csr.verify(cert_pkey) ? "PASSED" : "FAILED")
<< std::endl;
// Create the certificate using the CSR then sign it using the CA cert
cert.setSerialNumber(2);
cert.setIssuerName(ca_cert.subjectName());
cert.setNotBefore(t);
cert.setNotAfter(t + 3600);
cert_exts.addBasicConstraints("critical, CA:FALSE");
cert_exts.addKeyUsage("critical, nonRepudiation, digitalSignature, keyEncipherment, keyAgreement");
cert_exts.addExtKeyUsage("serverAuth");
Async::SslX509ExtSubjectAltName san(exts.subjectAltName());
cert_exts.addExtension(san);
cert.addExtensions(cert_exts);
Async::SslKeypair csr_pkey(csr.publicKey());
cert.setPublicKey(csr_pkey);
cert.sign(ca_pkey);
std::cout << "------------- Server Certificate --------------" << std::endl;
cert.print();
std::cout << "-----------------------------------------------" << std::endl;
if (!cert.writePemFile("demo.crt"))
{
std::cout << "*** WARNING: Failed to write certificate file"
<< std::endl;
}
std::cout << "The certificate verification "
<< (cert.verify(ca_pkey) ? "PASSED" : "FAILED")
<< std::endl;
return 0;
}
SSL context meant to be used with TcpConnection and friends.
Represent private and public keys.
Implements a representation of a X.509 certificate.
A class representing a certificate signing request.
void addExtensions(SslX509Extensions &exts)
Add extensions to this CSR.
const X509_NAME * subjectName(void) const
Return the subject name as a X509_NAME pointer.
bool setPublicKey(SslKeypair &pubkey)
Set the public key.
void print(const std::string &prefix="") const
Print the info in this CSR to std::cout.
SslKeypair publicKey(void) const
Get the public key.
SslX509Extensions extensions(void) const
Get the extensions in this CSR.
bool writePemFile(FILE *f)
Write the CSR data to a PEM file.
bool verify(SslKeypair &pubkey) const
Verify the signature of this CSR.
bool sign(SslKeypair &privkey)
Sign the CSR using the given private key.
bool addSubjectName(const std::string &field, const std::string &value)
Add a subject name component.
bool setVersion(long version)
Set the version of the request.
A class representing private and public keys.
bool writePrivateKeyFile(const std::string &filename)
Write key data to file on PEM format.
bool generate(unsigned int bits)
Generate a new RSA keypair.
A class representing the X.509 Subject Alternative Name extension.
A class representing X.509 extensions.
bool addExtKeyUsage(const std::string &eku)
Add extended key usage.
bool addSubjectAltNames(const std::string &san)
Add subject alternative names.
bool addExtension(const SslX509ExtSubjectAltName &san)
Add a subject alternative names object.
bool addBasicConstraints(const std::string &bc)
Add basic constraints extension.
bool addKeyUsage(const std::string &ku)
Add key usage.
A class representing an X.509 certificate.
void addIssuerName(const std::string &field, const std::string &value)
Add a name to the issuer distinguished name.
void print(const std::string &prefix="") const
Print this certificate to std::cout.
void setSerialNumber(long serial_number=-1)
Set the serial number of the certificate.
bool setPublicKey(SslKeypair &pkey)
Set the public key for this certificate.
bool verify(SslKeypair &keypair)
Verify that this certificate is signed by the given key.
const X509_NAME * subjectName(void) const
Get the subject distinguished name.
bool setSubjectName(const X509_NAME *name)
Set the subject distinguished name.
bool writePemFile(FILE *f)
Write this certificate to file in PEM format.
bool setIssuerName(const X509_NAME *name)
Set the issuer distinguished name.
void addExtensions(const SslX509Extensions &exts)
Add v3 extensions to this certificate.
bool sign(SslKeypair &pkey)
Sign this certificate using the given key.
const X509_NAME * issuerName(void) const
Get the issuer distinguished name.
void setNotBefore(std::time_t in_time)
Set the date and time from which this certificate is valid.
void setNotAfter(std::time_t in_time)
Set the date and time up to which this certificate is valid.
bool setVersion(long version)
Set the version of this certificate.
Examples
AsyncSslX509_demo.cpp.

Definition at line 119 of file AsyncSslX509Extensions.h.

Constructor & Destructor Documentation

◆ SslX509Extensions() [1/4]

Async::SslX509Extensions::SslX509Extensions ( void )
inline

Default constructor.

Definition at line 125 of file AsyncSslX509Extensions.h.

◆ SslX509Extensions() [2/4]

Async::SslX509Extensions::SslX509Extensions ( STACK_OF(X509_EXTENSION) * exts)
inlineexplicit

Constructor.

Parameters
extsPointer to a stack of X509_EXTENSION objects

Definition at line 134 of file AsyncSslX509Extensions.h.

◆ SslX509Extensions() [3/4]

Async::SslX509Extensions::SslX509Extensions ( SslX509Extensions && other)
inline

Move Constructor.

Parameters
otherThe object to move from

Definition at line 143 of file AsyncSslX509Extensions.h.

◆ SslX509Extensions() [4/4]

Async::SslX509Extensions::SslX509Extensions ( const SslX509Extensions & other)
inlineexplicit

Copy constructor.

Parameters
otherThe object to copy from

Definition at line 153 of file AsyncSslX509Extensions.h.

◆ ~SslX509Extensions()

Async::SslX509Extensions::~SslX509Extensions ( void )
inline

Destructor.

Definition at line 171 of file AsyncSslX509Extensions.h.

Member Function Documentation

◆ addBasicConstraints()

bool Async::SslX509Extensions::addBasicConstraints ( const std::string & bc)
inline

Add basic constraints extension.

Parameters
bcBasic constraints string
Returns
Return true on success

Ex: addBasicConstraints("critical, CA:FALSE");

Examples
AsyncSslX509_demo.cpp.

Definition at line 187 of file AsyncSslX509Extensions.h.

◆ addExtension()

bool Async::SslX509Extensions::addExtension ( const SslX509ExtSubjectAltName & san)
inline

Add a subject alternative names object.

Parameters
sanA subject alternative names object containing names to add
Examples
AsyncSslX509_demo.cpp.

Definition at line 249 of file AsyncSslX509Extensions.h.

◆ addExtKeyUsage()

bool Async::SslX509Extensions::addExtKeyUsage ( const std::string & eku)
inline

Add extended key usage.

Parameters
ekuExtended key usage string
Returns
Return true on success

Ex: addExtKeyUsage("serverAuth");

Examples
AsyncSslX509_demo.cpp.

Definition at line 213 of file AsyncSslX509Extensions.h.

◆ addKeyUsage()

bool Async::SslX509Extensions::addKeyUsage ( const std::string & ku)
inline

Add key usage.

Parameters
kuKey usage string
Returns
Return true on success

Ex: addKeyUsage( "critical, digitalSignature, keyEncipherment, keyAgreement" );

Examples
AsyncSslX509_demo.cpp.

Definition at line 201 of file AsyncSslX509Extensions.h.

◆ addSubjectAltNames()

bool Async::SslX509Extensions::addSubjectAltNames ( const std::string & san)
inline

Add subject alternative names.

Parameters
Subjectalternative names string
Returns
Return true on success

Ex: addSUbjectAltNames("DNS:dom.org,IP:1.2.3.4,email:user@dom.org")

Examples
AsyncSslX509_demo.cpp.

Definition at line 225 of file AsyncSslX509Extensions.h.

◆ operator const STACK_OF()

Async::SslX509Extensions::operator const STACK_OF ( X509_EXTENSION ) const
inline

Cast to a pointer to a stack of X509_EXTENSION objects.

Definition at line 263 of file AsyncSslX509Extensions.h.

◆ operator=()

SslX509Extensions & Async::SslX509Extensions::operator= ( const SslX509Extensions & )
delete

Disallow copy assignment.

◆ subjectAltName()

SslX509ExtSubjectAltName Async::SslX509Extensions::subjectAltName ( void ) const
inline

Get the subject alternative names extension.

Returns
Returns an object representing the SAN
Examples
AsyncSslX509_demo.cpp.

Definition at line 234 of file AsyncSslX509Extensions.h.

Referenced by Async::SslCertSigningReq::print().


The documentation for this class was generated from the following file: