Async 1.8.0
Async::SslContext Class Reference

SSL context meant to be used with TcpConnection and friends. More...

#include <AsyncSslContext.h>

Public Member Functions

 SslContext (void)
 Default constructor.
 
 SslContext (SSL_CTX *ctx)
 Constructor.
 
 SslContext (const SslContext &)=delete
 Do not allow copy construction.
 
SslContextoperator= (const SslContext &)=delete
 Do not allow assignment.
 
 ~SslContext (void)
 Destructor.
 
bool setCertificateFiles (const std::string &keyfile, const std::string &crtfile)
 Set which key and certificate file to use for connections.
 
bool caCertificateFileIsSet (void) const
 Find out if the CA certificate file is set.
 
bool setCaCertificateFile (const std::string &cafile)
 Set which CA certificate file to use for verifying certificates.
 
 operator SSL_CTX * (void)
 Cast to pointer to SSL_CTX.
 
 operator const SSL_CTX * (void) const
 

Static Public Member Functions

static void sslPrintErrors (const std::string &fname)
 Print the latest SSL errors.
 

Detailed Description

SSL context meant to be used with TcpConnection and friends.

Author
Tobias Blomberg / SM0SVX
Date
2020-08-01
#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
AsyncHttpServer_demo.cpp.

Definition at line 116 of file AsyncSslContext.h.

Constructor & Destructor Documentation

◆ SslContext() [1/3]

Async::SslContext::SslContext ( void )
inline

Default constructor.

Definition at line 133 of file AsyncSslContext.h.

◆ SslContext() [2/3]

Async::SslContext::SslContext ( SSL_CTX * ctx)
inline

Constructor.

Parameters
ctxUse this existing context

Definition at line 159 of file AsyncSslContext.h.

◆ SslContext() [3/3]

Async::SslContext::SslContext ( const SslContext & )
delete

Do not allow copy construction.

◆ ~SslContext()

Async::SslContext::~SslContext ( void )
inline

Destructor.

Definition at line 174 of file AsyncSslContext.h.

Member Function Documentation

◆ caCertificateFileIsSet()

bool Async::SslContext::caCertificateFileIsSet ( void ) const
inline

Find out if the CA certificate file is set.

Returns
Returns true if the CA certificate has been set

Definition at line 224 of file AsyncSslContext.h.

◆ operator const SSL_CTX *()

Async::SslContext::operator const SSL_CTX * ( void ) const
inline

Definition at line 243 of file AsyncSslContext.h.

◆ operator SSL_CTX *()

Async::SslContext::operator SSL_CTX * ( void )
inline

Cast to pointer to SSL_CTX.

Returns
Returns a pointer to the internal SSL_CTX

Definition at line 242 of file AsyncSslContext.h.

◆ operator=()

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

Do not allow assignment.

◆ setCaCertificateFile()

bool Async::SslContext::setCaCertificateFile ( const std::string & cafile)
inline

Set which CA certificate file to use for verifying certificates.

Parameters
cafileThe path to the CA file
Returns
Returns true on success

Definition at line 231 of file AsyncSslContext.h.

◆ setCertificateFiles()

bool Async::SslContext::setCertificateFiles ( const std::string & keyfile,
const std::string & crtfile )
inline

Set which key and certificate file to use for connections.

Parameters
keyfileThe path to the key file
crtfileThe path to the certificate file
Returns
Returns true on success
Examples
AsyncHttpServer_demo.cpp.

Definition at line 186 of file AsyncSslContext.h.

References sslPrintErrors().

◆ sslPrintErrors()

static void Async::SslContext::sslPrintErrors ( const std::string & fname)
inlinestatic

Print the latest SSL errors.

Parameters
fnameThe name of the last called function

Definition at line 123 of file AsyncSslContext.h.

Referenced by setCertificateFiles().


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