Verifying TLS Usage in PHP cURL
When a server administrator decides to drop SSL support in favor of TLS exclusively, often due to vulnerabilities like POODLE, the immediate task is to ensure your client library respects that mandate. In PHP, this typically involves setting CURLOPT_SSLVERSION to CURL_SSLVERSION_TLSv1_2. While this configuration directive is standard practice, relying on it without verification creates a false sense of security. You need to confirm that the actual handshake establishes the expected protocol version before you assume compliance.
The standard approach to inspecting cURL responses is calling curl_getinfo() after the request completes. However, as many developers have discovered, this method falls short for protocol version verification. The function returns a comprehensive array of metrics including HTTP status codes, content types, and transfer times, but it does not expose the specific TLS version negotiated during the handshake.
The output from curl_getinfo() might look like this:
[
'url' => 'https://www.example.com/soap/MessagingPort',
'content_type' => 'text/xml;charset=utf-8',
'http_code' => 200,
'ssl_verify_result' => 0,
// ... other metrics
]
Notice the absence of a ssl_version or similar key in this standard output. The array simply confirms that the SSL certificate was valid (ssl_verify_result is 0) and that the connection succeeded, but it remains silent on whether TLS 1.0, 1.1, or 1.2 was actually used. This gap exists because the underlying libcurl library often aggregates these details in a way that isn't directly accessible via the public API for standard inspection.
To verify the protocol version programmatically within PHP, you must bypass the standard curl_getinfo() limitations and inspect the raw connection data. The most reliable method involves enabling verbose output during the request and parsing the debug log. By setting CURLOPT_VERBOSE to true and capturing the stderr stream, you can intercept the cURL debug output which explicitly logs the TLS protocol version upon handshake completion.
Here is an implementation pattern that captures this specific detail:
$handle = curl_init('https://www.example.com/soap/MessagingPort');
curl_setopt($handle, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
curl_setopt($handle, CURLOPT_VERBOSE, true);
curl_setopt($handle, CURLOPT_STDERR, STDERR); // Or capture to a string buffer
$response = curl_exec($handle);
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
if ($httpCode === 200) {
$debugOutput = file_get_contents('php://stderr');
// In production, you would redirect stderr to a variable or pipe instead of the global stream.
if (strpos($debugOutput, 'TLSv1.2') !== false) {
echo "Confirmed: Connection uses TLSv1.2\n";
} else {
echo "Warning: Protocol version not explicitly confirmed as TLSv1.2 in logs.\n";
}
}
curl_close($handle);
The critical line here is searching the debug buffer for the string TLSv1.2. When libcurl completes the handshake, it prints a log entry resembling * Connected to www.example.com () port 443 (SSL), but subsequent lines often specify the exact cipher suite and protocol version, such as * SSL-Session: TLS 1.2. If this string is missing, the server might be negotiating an older version despite your CURLOPT_SSLVERSION setting, or the client might be falling back due to server configuration constraints.
This debugging approach has limitations in production environments where verbose logging to stderr is undesirable for security or performance reasons. In such cases, relying solely on code inspection can be risky if the library implementation differs across environments. The trade-off between ease of use and verification depth is significant here; while CURLOPT_SSLVERSION sets an intent, only raw log inspection provides definitive proof of execution.
For applications where strict compliance is non-negotiable, integrating this debug parsing into your CI/CD pipeline or monitoring scripts is a prudent strategy. It ensures that the transition from SSL to TLS is not just configured correctly in code, but actually enforced at the network layer.