Below are the most common causes of the “ssl/tls alert handshake failure” (alert 40) in OpenSSL-based clients—and how to fix them in your code.
- You aren’t sending SNI (Server Name Indication)
- You haven’t loaded any trusted CAs (so the server’s cert can’t be validated)
- You’re still permitting/using very old SSL protocols or missing TLS1.2+
- You’re not setting the hostname for certificate verification
Below is a revised version of your client that addresses all of the above:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netdb.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#define HOSTNAME "public-privacy.com"
#define PORT "443"
#define PATH "/robots.txt"
#define BUF_SIZE 4096
static void die(const char *msg) {
perror(msg);
ERR_print_errors_fp(stderr);
exit(EXIT_FAILURE);
}
int main(void) {
SSL_CTX *ctx;
SSL *ssl;
int sock;
struct addrinfo hints, *res, *rp;
char req[512], buf[BUF_SIZE];
int n;
/* 1) Initialize OpenSSL */
SSL_library_init();
OpenSSL_add_all_algorithms();
SSL_load_error_strings();
/* 2) Create SSL context that negotiates the highest TLS version */
ctx = SSL_CTX_new(TLS_client_method());
if (!ctx) die("SSL_CTX_new");
/* 3) Force disable SSLv2/3 and TLS1.0/1.1 if you like */
SSL_CTX_set_options(ctx,
SSL_OP_NO_SSLv2 |
SSL_OP_NO_SSLv3 |
SSL_OP_NO_COMPRESSION /* optional */
);
/* 4) Load system default CA certificates to verify the server */
if (!SSL_CTX_set_default_verify_paths(ctx))
die("SSL_CTX_set_default_verify_paths");
/* 5) Tell OpenSSL to verify the server certificate */
SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL);
/* 6) Resolve the hostname */
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
if (getaddrinfo(HOSTNAME, PORT, &hints, &res) != 0)
die("getaddrinfo");
for (rp = res; rp; rp = rp->ai_next) {
sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (sock < 0) continue;
if (connect(sock, rp->ai_addr, rp->ai_addrlen) == 0) break;
close(sock);
}
if (!rp) die("connect");
freeaddrinfo(res);
/* 7) Create SSL object and attach to socket */
ssl = SSL_new(ctx);
if (!ssl) die("SSL_new");
SSL_set_fd(ssl, sock);
/* 8) Send SNI */
if (!SSL_set_tlsext_host_name(ssl, HOSTNAME))
die("SSL_set_tlsext_host_name");
/* 9) (OpenSSL ≥ 1.1.0) set hostname for cert verification */
if (!SSL_set1_host(ssl, HOSTNAME))
die("SSL_set1_host");
/* 10) Perform the TLS handshake */
if (SSL_connect(ssl) != 1)
die("SSL_connect");
/* 11) Check certificate verification result */
if (SSL_get_verify_result(ssl) != X509_V_OK) {
fprintf(stderr, "Certificate verification failed.\n");
exit(EXIT_FAILURE);
}
/* 12) Build and send HTTP GET */
snprintf(req, sizeof req,
"GET %s HTTP/1.1\r\n"
"Host: %s\r\n"
"User-Agent: wget-lite/1.0\r\n"
"Connection: close\r\n\r\n",
PATH, HOSTNAME);
if (SSL_write(ssl, req, strlen(req)) <= 0)
die("SSL_write");
/* 13) Read & print the response */
while ((n = SSL_read(ssl, buf, BUF_SIZE-1)) > 0) {
buf[n] = 0;
fputs(buf, stdout);
}
/* 14) Clean up */
SSL_shutdown(ssl);
SSL_free(ssl);
close(sock);
SSL_CTX_free(ctx);
return 0;
}
Compile and link exactly as before:
gcc fetch_robots.c -o fetch_robots -lssl -lcrypto
Why this fixes handshake failures:
• SNI (Server Name Indication)
Many modern servers host multiple TLS sites on one IP. If you don’t send the hostname in the ClientHello, they refuse the handshake.
• Trusted CAs & hostname verify
If you don’t load any trust store, or don’t tell OpenSSL which host you want, the server certificate can’t be validated (and some servers close the connection).
• Forcing TLSv1.2+ & disabling legacy SSL
Old SSLv3 or TLSv1.0 are often disabled by server policy. Using TLS_client_method + disabling SSLv2/3 and optionally TLS1.0/1.1 guarantees you speak a modern protocol.
Give that a spin—on sites that were failing, the handshake should now succeed. Let me know how it goes! 🚀