...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
A Certificate Authority (CA) is a trusted entity that signs digital certificates, enabling users to verify their authenticity. Rather than storing every individual certificate for each server (which would be impractical due to the sheer volume and frequent renewals), users can store a limited set of root certificates to authenticate server certificates as needed.
Boost.Asio provides various methods for loading certificate authority certificates:
It is important to set up peer verification so that the TLS/SSL handshake fails if certificate verification is unsuccessful:
// Verify the remote server's certificate ctx.set_verify_mode(net::ssl::verify_peer);
A client must also verify that the hostname or IP address in the certificate
matches the expected one. The net::ssl::host_name_verification
helper function object can perform this verification according to the rules
described in RFC 6125:
// Verify the remote server's Hostname or IP address stream.set_verify_callback( net::ssl::host_name_verification("host.name"));
A server can also request and verify a client certificate to authenticate the client:
// Verify the remote client's certificate ctx.set_verify_mode( net::ssl::verify_peer | net::ssl::verify_fail_if_no_peer_cert);
A Server Certificate is a digital certificate that confirms a server's identity as the legitimate destination for a client. It contains a verifiable signature that ensures it was issued by a trusted certificate authority (CA).
When a server certificate is issued by an intermediate certificate authority, and the client lacks those intermediate certificates, the server should provide all the relevant certificates to the client. This allows the client to verify the final certificate in the chain against the root certificate.
The following Boost.Asio methods can be used for loading a certificate or a certificate chain:
A server can authenticate clients by requiring and verifying their certificates, preventing access for those without a valid certificate and private key. The server enforces this by modifying peer verification settings:
// Verify the remote client's certificate ctx.set_verify_mode( net::ssl::verify_peer | net::ssl::verify_fail_if_no_peer_cert);
If used, the necessary CA certificates must be loaded into the server's SSL context to enable verification of the client's certificate.
The Subject Alternative Name (SAN) is an extension in X.509 certificates that allows multiple domain names, subdomains, or IP addresses to be associated with a single SSL/TLS certificate. Before that it was the Common Name field in the certificate subject which could contain a single hostname.
RFC 6125 recommends that if a certificate includes a SAN dNSName field, the client must ignore the subject CN field. Some modern browsers, such as Google Chrome, check only the SAN section in an SSL/TLS certificate and reject certificates that contain only the CN field.
The private key of a certificate is required during the SSL/TLS handshake to prove that the certificate's provider is its rightful owner
The following Boost.Asio methods can be used for loading a private key:
If the private key is secured with a password, the net::ssl::context::set_password_callback allows specifying a callable object to retrieve the password.
A self-issued certificate is a certificate where the issuer and subject are the same entity.
A self-signed certificate is a self-issued certificate in which the digital signature can be verified using the public key within the certificate.
![]() |
Warning |
---|---|
Installing an untrusted, self-issued, or self-signed CA certificate poses a significant security risk, as there are no restrictions on the domains for which it can issue certificates. This allows attackers to generate fraudulent certificates for any public domain, enabling man-in-the-middle attacks if they gain access to your network. |
Diffie-Hellman (DH) key exchange is a cryptographic protocol that allows
two parties to securely establish a shared secret over an insecure communication
channel. The key exchange process involves both parties agreeing on a set
of parameters, known as Diffie-Hellman parameters, which include a large
prime number p
and a generator
g
. Since generating these
parameters is a computationally expensive task, a user might prefer to provide
a precomputed value at startup.
The following Boost.Asio methods can be used for loading DH parameters:
If no DH parameter is provided, OpenSSL will refuse to perform any handshake that uses DHE-based cipher suites but will still work with other cipher suites, such as those based on ECDHE.
In the following example, we will generate a self-signed CA certificate and use it to issue both server and client certificates.
openssl req -new -newkey rsa:4096 -keyout ca.key -x509 -out ca.crt -subj "/CN=localhost" -days 365
openssl req -new -newkey rsa:4096 -keyout server.key -out server.csr -subj "/CN=localhost" -addext "subjectAltName=DNS:localhost,IP:127.0.0.1"
openssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key -copy_extensions copy -days 365 -out server.crt
openssl req -new -newkey rsa:4096 -keyout client.key -out client.csr -subj "/CN=client.1"
openssl x509 -req -in client.csr -CA ca.crt -CAkey ca.key -days 365 -out client.crt
openssl dhparam -out dh4096.pem 4096
Server example: server.cpp
Note that the server is configured in such a way that it requests and verifies the client certificate. You can disable this by commenting out the related line in the example.
You can test the server using this cURL command:
curl https://localhost:8080 --cacert ca.crt --cert client.crt --key client.key
Also, you can use the client example: client.cpp