TLS
TLS (Transport Layer Security) encrypts a byte stream, usually a TCP socket, and authenticates the server with a certificate. HTTPS is HTTP over TLS. In C you use a library — commonly OpenSSL, LibreSSL, or mbedTLS — not a language feature.
Detailed Explanation
A plain socket sends bytes in the clear. Anyone on the path can read or change them. TLS sits on the socket: you hand the library the file descriptor, it does a handshake, and then you SSL_read / SSL_write instead of recv / send. The application protocol (HTTP, SMTP, your RPC) is unchanged.
The handshake negotiates algorithms and checks the certificate. Checking means: the chain leads to a trust store you configured, the name matches the host you intended, and the cert is in its validity window. Skipping the name check is how "HTTPS" to an impostor happens. SSL_set_tlsext_host_name (SNI) tells the server which cert to present; without it, virtual hosting on TLS breaks.
C TLS libraries are famous for API surface. OpenSSL's SSL_CTX is the shared configuration (ciphers, trust store). SSL * is one connection. Errors are a queue; SSL_get_error after a failed SSL_read distinguishes WANT_READ (non-blocking) from a real close. Memory is yours to free: SSL_free, SSL_CTX_free.
You should not implement TLS. You should not disable verification to "make it work." You should not ship a ten-year-old OpenSSL. libcurl and modern HTTP servers already link a TLS library; prefer those if you only need HTTPS.
Certificates and private keys are files (PEM) or OS stores. The private key is a secret. A C program that logs it, or that chmods it world-readable, has failed independently of cryptography.
Code Examples
/* Conceptual OpenSSL client sketch. Link -lssl -lcrypto.
Check every return in real code; this shows the object graph. */
#include <openssl/ssl.h>
#include <stdio.h>
int main(void)
{
SSL_CTX *ctx = SSL_CTX_new(TLS_client_method());
if (ctx == NULL) {
return 1;
}
if (SSL_CTX_set_default_verify_paths(ctx) != 1) {
SSL_CTX_free(ctx);
return 1;
}
SSL *ssl = SSL_new(ctx);
if (ssl == NULL) {
SSL_CTX_free(ctx);
return 1;
}
SSL_set_verify(ssl, SSL_VERIFY_PEER, NULL);
/* SSL_set_fd(ssl, sock); SSL_connect(ssl); SSL_write / SSL_read ... */
SSL_free(ssl);
SSL_CTX_free(ctx);
printf("ctx ready\n");
return 0;
}Common Gotchas
`SSL_read` returning `0` or `-1` needs `SSL_get_error`
a non-blocking socket reports `WANT_READ`, not EOF.
TLS is not HTTP
you can wrap any byte protocol. HTTPS is just the common pairing.
Mixing `recv` on the fd with `SSL_read` desynchronizes the TLS state
once the library owns the fd, only talk through it.
`SSL_VERIFY_NONE` or skipping hostname checks accepts a stranger's cert
`SSL_VERIFY_NONE` or skipping hostname checks accepts a stranger's cert