Core\Http\TlsInfo::cipher
$tlsInfo->cipher(): stringReturns the name of the cipher suite that encrypts the connection a reply came over.
A cipher suite is the set of algorithms the client and the server agree on when they connect. The name comes from the IANA list, which most TLS tools also use: for example TLS_AES_128_GCM_SHA256. TLS 1.3 and TLS 1.2 use different names. A TLS 1.2 name also says how the keys were exchanged, such as ECDHE_RSA. If the list has no name for a suite, cipher() returns its number in hexadecimal, which starts with 0x.
You get a Core\Http\TlsInfo from Core\Http\Response::tls(). That method returns null for a reply that did not come over TLS, so check for null first.
Good to know: the examples build their TLS connections with Core\Test::tlsSession, so they run without a network. They show how to read the name, the names for each TLS version, and a check against a list of approved ciphers.
Return value
Returns string. A name such as TLS_AES_128_GCM_SHA256. If the registry has no name for the suite, the result is its 0x code.
Examples
Reading the cipher
<?nvs// Shows how to read the cipher that encrypts the connection a reply came over.
// `Core\Test::tlsSession` builds a TLS connection for a test, so the example runs// without a network. `Core\Test::answerHttp` gives a fixed reply that reports it.Core\Test::answerHttp('https://api.example.com/health', 200, { tls: Core\Test::tlsSession({}) });
?Core\Http\TlsInfo $info = Core\Http\Client::get('https://api.example.com/health')->tls();
// `cipher()` returns the name of the cipher suite, as the IANA list writes it.// This prints `TLS_AES_128_GCM_SHA256`.if ($info != null) { echo $info->cipher(), "\n";}TLS_AES_128_GCM_SHA256The version and the cipher together
<?nvs// Shows that each TLS version has its own cipher names. A TLS 1.2 name also// says how the keys are exchanged, such as `ECDHE_RSA`.
// Two TLS connections built for a test. Each one names its version and its cipher.array<Core\Http\TlsInfo> $sessions = [ Core\Test::tlsSession({ version: 'TLSv1.3', cipher: 'TLS_CHACHA20_POLY1305_SHA256' }), Core\Test::tlsSession({ version: 'TLSv1.2', cipher: 'TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256' }),];
// This prints `TLSv1.3 TLS_CHACHA20_POLY1305_SHA256`// and then `TLSv1.2 TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256`.foreach ($sessions as Core\Http\TlsInfo $info) { echo $info->version(), ' ', $info->cipher(), "\n";}TLSv1.3 TLS_CHACHA20_POLY1305_SHA256TLSv1.2 TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256Checking against an allowed list
<?nvs// Shows a security check that allows only the ciphers on a company's approved list.
class CipherPolicy{ // Returns `true` when the connection uses one of the approved ciphers. public static function allows(Core\Http\TlsInfo $info): bool { array<string> $approved = [ 'TLS_AES_256_GCM_SHA384', 'TLS_AES_128_GCM_SHA256', 'TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384', ]; return Core\Arr::contains($approved, $info->cipher()); }}
// Fixed replies with TLS connections built for a test, so the example runs without a network.Core\Test::answerHttp('https://billing.example.com/invoices', 200, { tls: Core\Test::tlsSession({ cipher: 'TLS_AES_256_GCM_SHA384' }),});Core\Test::answerHttp('https://legacy.example.com/invoices', 200, { tls: Core\Test::tlsSession({ version: 'TLSv1.2', cipher: 'TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256' }),});
// The program sends the invoice data only when the cipher is approved.// This prints `billing.example.com: send` and then `legacy.example.com: blocked`.array<string> $hosts = ['billing.example.com', 'legacy.example.com'];foreach ($hosts as string $host) { ?Core\Http\TlsInfo $info = Core\Http\Client::get('https://' . $host . '/invoices')->tls(); string $decision = 'blocked'; if ($info != null) { if (CipherPolicy::allows($info)) { $decision = 'send'; } } echo $host, ': ', $decision, "\n";}billing.example.com: sendlegacy.example.com: blocked