Core\Http\TlsInfo::peerChain
Returns the certificates that the server of a TLS connection sent, as an array of texts.
A server proves who it is with a certificate. It usually sends more than one: its own certificate first, and then the certificate of the authority that signed it. peerChain() returns them in that order. Each entry is one certificate in PEM form (a text that starts with -----BEGIN CERTIFICATE-----), which other tools can read. The texts are tainted (they came from outside the program), because the other server chose them. The array is empty if the server sent no certificate.
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 read the certificates, join them into the text of one .pem file, and notice when a server starts to use a different certificate.
Return value
Returns array<tainted string>. One PEM block per certificate, leaf first. The result is an empty array if the server sent none. Every entry is tainted, because the server chose these bytes.
Examples
Reading the certificates
<?nvs// Shows how to read the certificates that the server of a reply sent.
// `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.var $session = Core\Test::tlsSession({ subject: 'CN=api.example.com' });Core\Test::answerHttp('https://api.example.com/health', 200, { tls: $session });
?Core\Http\TlsInfo $info = Core\Http\Client::get('https://api.example.com/health')->tls();
// `peerChain()` returns one text per certificate. The first one is the server's own.// This prints `2`, and then the first line of each certificate.if ($info != null) { array<tainted string> $chain = $info->peerChain(); echo Core\Arr::count($chain), "\n"; foreach ($chain as tainted string $pem) { echo Core\Str::lines($pem)[0], "\n"; }}2-----BEGIN CERTIFICATE----------BEGIN CERTIFICATE-----One text for a pem file
<?nvs// Shows how to join the certificates into one text. A `.pem` file has this form,// so other tools can read it.
// A TLS connection built for a test, so the example runs without a network.var $session = Core\Test::tlsSession({ subject: 'CN=shop.example.com, O=Shop' });
// The loop puts the certificates one after the other.// Each certificate already ends with a line break.tainted string $bundle = '';foreach ($session->peerChain() as tainted string $pem) { $bundle = $bundle . $pem;}array<tainted string> $lines = Core\Str::lines($bundle);
// Each certificate starts with a `BEGIN CERTIFICATE` line, so this counts them.// This prints `certificates: 2`.uint $count = 0;foreach ($lines as tainted string $line) { if ($line == '-----BEGIN CERTIFICATE-----') { $count = $count + 1; }}echo 'certificates: ', $count, "\n";
// This prints the first line and the last line of the whole text.echo $lines[0], "\n";echo $lines[Core\Arr::count($lines) - 1], "\n";certificates: 2-----BEGIN CERTIFICATE----------END CERTIFICATE-----Noticing a new certificate
<?nvs// Shows how to notice that a server started to use a different certificate.// The program remembers the first certificate and compares every later one to it.
// Fixed replies with TLS connections built for a test, so the example runs// without a network. The `v2` address has a new certificate for the same server.Core\Test::answerHttp('https://api.example.com/v1/*', 200, { tls: Core\Test::tlsSession({ subject: 'CN=api.example.com' }),});Core\Test::answerHttp('https://api.example.com/v2/*', 200, { tls: Core\Test::tlsSession({ subject: 'CN=api.example.com' }),});
array<string> $urls = [ 'https://api.example.com/v1/orders', 'https://api.example.com/v1/stock', 'https://api.example.com/v2/orders',];
// The first entry of `peerChain()` is the certificate of the server itself.// This prints `first`, then `same`, then `changed`.?tainted string $remembered = null;foreach ($urls as string $url) { ?Core\Http\TlsInfo $info = Core\Http\Client::get($url)->tls(); if ($info == null) { continue; } tainted string $leaf = $info->peerChain()[0]; if ($remembered == null) { $remembered = $leaf; echo 'first: ', $url, "\n"; } elseif ($leaf == $remembered) { echo 'same: ', $url, "\n"; } else { echo 'changed: ', $url, "\n"; }}first: https://api.example.com/v1/orderssame: https://api.example.com/v1/stockchanged: https://api.example.com/v2/orders