TLS
TLS (the protocol under HTTPS) encrypts a byte stream and authenticates the server with a certificate. C++ programs use OpenSSL, BoringSSL, or a library that wraps them (libcurl, Asio SSL, httplib). You do not implement TLS yourself.
Detailed Explanation
TCP gives you bytes anyone on the path can read. TLS wraps those bytes: a handshake picks keys, the server presents an X.509 certificate, the client verifies the name and the issuer, then application data (GET /...) is encrypted. TLS 1.3 is the current protocol; TLS 1.0/1.1 are off.
C++ does not include TLS. OpenSSL is the common engine (SSL_CTX, SSL*, BIO). It is a C API. Wrap SSL_CTX and SSL in RAII. A single SSL_CTX per process (or per settings set) holds trust anchors and ciphers; SSL is per connection.
What you must configure:
- Trust store. The system CA bundle, or a file you ship.
SSL_CTX_set_default_verify_paths. - Verification on.
SSL_VERIFY_PEER. Turning it off makes HTTPS a password-less XOR. - Hostname check. OpenSSL does not always do this for you the way browsers do.
SSL_set1_host/ curl's default verify. A valid cert forevil.testmust not satisfyapi.example.com. - Protocol floor. Disable SSLv3 and old TLS.
SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION)or stricter. - Client certs (mTLS) when the server asks. Load a key with the same care as a password (permissions, not in the repo).
libcurl and httplib (with OpenSSL) default to verifying peers. Beast+Asio SSL uses ssl::context. If a request "works in the browser but not in C++", the usual cause is a missing CA bundle on the server image, or a corporate MITM proxy whose CA you did not install.
Certificates expire. Pinning a public key is extra hardening and extra operational pain. Start with the platform CA store and HTTP Public Key Pinning only if you have a rotation story.
Do not log secrets, session tickets, or full client certificates. Do not write your own cipher. Do not ignore SSL_read/SSL_write WANT_READ/WANT_WRITE in a non-blocking loop — that is how Asio's ssl stream already works; if you call OpenSSL raw, you must handle it.
Code Examples
#include <curl/curl.h>
#include <iostream>
int main() {
CURL* e = curl_easy_init();
if (!e) return 1;
curl_easy_setopt(e, CURLOPT_URL, "https://example.com/");
curl_easy_setopt(e, CURLOPT_NOBODY, 1L);
curl_easy_setopt(e, CURLOPT_TIMEOUT, 10L);
// Peer + host verification stay on (library defaults).
CURLcode rc = curl_easy_perform(e);
long tls = 0;
curl_easy_getinfo(e, CURLINFO_HTTP_VERSION, &tls);
curl_easy_cleanup(e);
std::cout << (rc == CURLE_OK ? "ok\n" : "fail\n");
return rc == CURLE_OK ? 0 : 1;
}Common Gotchas
`CURLOPT_SSL_VERIFYPEER` 0
you accepted any certificate. That is not HTTPS in the security sense.
Missing hostname verification
a stolen cert for another name would pass. Set the host.
CA bundle on minimal containers
install `ca-certificates` or point `CURLOPT_CAINFO` at a PEM you ship.
Blocking `SSL_read` on a UI thread
same as any network. Use async or a worker.