Async 1.8.0
Async::SslX509ExtSubjectAltName Class Reference

A class representing the X.509 Subject Alternative Name extension. More...

#include <AsyncSslX509ExtSubjectAltName.h>

Public Types

using ForeachFunction = std::function<void(int, std::string)>
 

Public Member Functions

 SslX509ExtSubjectAltName (const std::string &names)
 Default constructor.
 
 SslX509ExtSubjectAltName (const X509_EXTENSION *ext)
 Constructor.
 
 SslX509ExtSubjectAltName (GENERAL_NAMES *names)
 Constructor.
 
 SslX509ExtSubjectAltName (SslX509ExtSubjectAltName &&other)
 Move Constructor.
 
 SslX509ExtSubjectAltName (const SslX509ExtSubjectAltName &)=delete
 Disallow copy construction.
 
SslX509ExtSubjectAltNameoperator= (const SslX509ExtSubjectAltName &)=delete
 Disallow copy assignment.
 
 ~SslX509ExtSubjectAltName (void)
 Destructor.
 
bool isNull (void) const
 Check if the object is initialized.
 
 operator const X509_EXTENSION * () const
 Cast to X509_EXTENSION pointer.
 
void forEach (ForeachFunction f, int type=-1) const
 Loop through all names calling the given function for each one.
 
std::string toString (int type=-1) const
 Convert all SANs to a string.
 

Detailed Description

A class representing the X.509 Subject Alternative Name extension.

Author
Tobias Blomberg / SM0SVX
Date
2022-05-27
#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 115 of file AsyncSslX509ExtSubjectAltName.h.

Member Typedef Documentation

◆ ForeachFunction

using Async::SslX509ExtSubjectAltName::ForeachFunction = std::function<void(int, std::string)>

Definition at line 118 of file AsyncSslX509ExtSubjectAltName.h.

Constructor & Destructor Documentation

◆ SslX509ExtSubjectAltName() [1/5]

Async::SslX509ExtSubjectAltName::SslX509ExtSubjectAltName ( const std::string & names)
inlineexplicit

Default constructor.

Constructor

Parameters
namesA string of comma separated names

Names are specified on a tag:value format. For example: DNS:example.org, IP:1.2.3.4, email:user@.nosp@m.exam.nosp@m.ple.o.nosp@m.rg

Definition at line 132 of file AsyncSslX509ExtSubjectAltName.h.

◆ SslX509ExtSubjectAltName() [2/5]

Async::SslX509ExtSubjectAltName::SslX509ExtSubjectAltName ( const X509_EXTENSION * ext)
inline

Constructor.

Parameters
extAn existing X509_EXTENSION object

Definition at line 142 of file AsyncSslX509ExtSubjectAltName.h.

◆ SslX509ExtSubjectAltName() [3/5]

Async::SslX509ExtSubjectAltName::SslX509ExtSubjectAltName ( GENERAL_NAMES * names)
inlineexplicit

Constructor.

Parameters
namesA pointer to an existing GENERAL_NAMES object

Definition at line 155 of file AsyncSslX509ExtSubjectAltName.h.

◆ SslX509ExtSubjectAltName() [4/5]

Async::SslX509ExtSubjectAltName::SslX509ExtSubjectAltName ( SslX509ExtSubjectAltName && other)
inline

Move Constructor.

Parameters
otherThe object to move from

Definition at line 165 of file AsyncSslX509ExtSubjectAltName.h.

◆ SslX509ExtSubjectAltName() [5/5]

Async::SslX509ExtSubjectAltName::SslX509ExtSubjectAltName ( const SslX509ExtSubjectAltName & )
delete

Disallow copy construction.

◆ ~SslX509ExtSubjectAltName()

Async::SslX509ExtSubjectAltName::~SslX509ExtSubjectAltName ( void )
inline

Destructor.

Definition at line 185 of file AsyncSslX509ExtSubjectAltName.h.

Member Function Documentation

◆ forEach()

void Async::SslX509ExtSubjectAltName::forEach ( ForeachFunction f,
int type = -1 ) const
inline

Loop through all names calling the given function for each one.

Parameters
fThe function to call for each name
typeThe name type to call the function for (default: all)

Type can be GEN_DNS, GEN_IPADD or GEN_EMAIL. Other types are ignored.

Definition at line 230 of file AsyncSslX509ExtSubjectAltName.h.

References Async::IpAddress::toString().

Referenced by toString().

◆ isNull()

bool Async::SslX509ExtSubjectAltName::isNull ( void ) const
inline

Check if the object is initialized.

Returns
Returns true if the object is null

Definition at line 198 of file AsyncSslX509ExtSubjectAltName.h.

◆ operator const X509_EXTENSION *()

Async::SslX509ExtSubjectAltName::operator const X509_EXTENSION * ( ) const
inline

Cast to X509_EXTENSION pointer.

Returns
Return a pointer to a X509_EXTENSION, or null if uninitialized

Definition at line 221 of file AsyncSslX509ExtSubjectAltName.h.

◆ operator=()

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

Disallow copy assignment.

◆ toString()

std::string Async::SslX509ExtSubjectAltName::toString ( int type = -1) const
inline

Convert all SANs to a string.

Parameters
typeThe name type consider (default: all)
Returns
Return a ", " separated string with SANs

Type can be GEN_DNS, GEN_IPADD or GEN_EMAIL.

Definition at line 308 of file AsyncSslX509ExtSubjectAltName.h.

References forEach().

Referenced by Async::SslCertSigningReq::print(), and Async::SslX509::print().


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