Async 1.8.0
AsyncSslX509_demo.cpp

An example of how to use the SslCertSigningReq class

#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");
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.
SslX509ExtSubjectAltName subjectAltName(void) const
Get the subject alternative names extension.
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.