The TLS Handshake Failure Behind "Internal Error" Messages
When debugging SSL connectivity, encountering a tlsv1 alert internal error is often a red flag for certificate issues or cipher mismatch. However, a specific failure mode exists where a server appears perfectly functional in standard tools but rejects connections from OpenSSL-based clients. This scenario frequently surfaces when developers attempt to connect to domains like thepiratebay.gd using PHP's fsockopen() or the command-line openssl s_client.
In these cases, browsers and tools like curl establish a session without issue, yet OpenSSL tools hang or fail immediately after sending the Client Hello. The error log typically reads: SSL23_GET_SERVER_HELLO: tlsv1 alert internal error. This distinction is critical because it rules out standard protocol incompatibilities where the client simply doesn't support a required cipher suite. Instead, this points to a deeper handshake state disagreement or an incomplete session setup on the server side.
Decoding the Handshake Failure
The root cause of this specific failure often lies in how the server handles Session Tickets or Resumption State during the initial TLS 1.2 negotiation. When a client sends a Client Hello, it may include session tickets to resume a previous connection. If the server expects a ticket but the client does not provide one, or vice versa, the handshake can stall.
In the specific case of thepiratebay.gd, the server appears to advertise TLS 1.2 with the ECDHE-ECDSA-AES128-GCM-SHA256 cipher suite. However, when a client forces this configuration using openssl s_client -cipher ECDHE-ECDSA-AES128-GCM-SHA256 -connect thepiratebay.gd:443 -tls1_2, the connection drops with an "internal error" rather than a standard "handshake failure."
This behavior suggests that while the server accepts the cipher suite, it may be rejecting the connection because it cannot complete the key exchange or verify the client's state. The OpenSSL client logs often show that zero bytes are written during the handshake (SSL handshake has read 7 bytes and written 0 bytes), indicating the server sent a fatal alert before completing the protocol steps required to establish the master secret.
Why Browsers Succeed Where OpenSSL Fails
The divergence between browser behavior and OpenSSL tools is the most telling symptom. Modern browsers implement robust fallback mechanisms. If a specific cipher negotiation fails or a session resume attempt is rejected, the browser will often silently drop the ticket and retry the handshake with fresh ephemeral keys (ECDHE). It may also adjust its TLS version preferences dynamically if a strict request for TLS 1.2 triggers an unexpected server response.
In contrast, openssl s_client operates in a stricter mode by default. When you explicitly force -tls1_2 and a specific cipher list, the tool expects the server to adhere strictly to that configuration. If the server's implementation has a bug where it mishandles the transition from the Client Hello to the Server Helloâspecifically regarding how it processes empty or malformed session ticket extensionsâit will send an immediate alert_internal_error.
This is not necessarily a bug in your OpenSSL version (whether 0.9.8, 1.0.1, or 1.0.2), but rather a quirk in the server's TLS stack that fails to handle the specific handshake flow initiated by the command-line tool. The PHP fsockopen() function relies on the underlying SSL library, which mirrors this strict behavior, resulting in the same failure observed in your script.
Practical Workarounds for Connectivity Checks
If you must check connectivity to a server exhibiting this behavior, relying on openssl s_client with forced ciphers is likely to yield false negatives. The most reliable approach is to leverage tools that emulate browser behavior more closely or allow for session resumption bypass.
Using curl with the appropriate flags often succeeds where OpenSSL fails:
curl -k --tlsv1.2 https://thepiratebay.gd/
The -k flag skips certificate verification (handling the local issuer warning), and --tlsv1.2 ensures the protocol version matches. If you are writing a PHP script to check URL availability, consider using the curl