Core\Http\TlsInfo::subject
Returns the name in the certificate of the server a connection went to, as text like CN=api.example.com, O=Shop.
A certificate names who it belongs to. The name is a list of KEY=value pairs separated by , . CN is the server name, O is the organization, and C is the country. subject() returns the whole list as one string. The text is tainted (it came from outside the program), because the other server chose what is written in its 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 show the whole subject, how to take the server name out of it, and a check that a payment partner's certificate names the expected organization.
Return value
Returns tainted string. The same text that openssl x509 -subject prints, such as CN=api.example.com. It is tainted, because the server chose this text.
Errors
RuntimeErrorThe server sent no certificate, or its leaf certificate is not X.509 that this method can read.version,cipher,isVerifiedandpeerChainstill work, because they do not read inside a certificate.
Examples
Reading the subject
<?nvs// Shows how to read the name in the certificate of the server that sent a reply.
// `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, O=Shop' });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();
// `subject()` returns the name as `KEY=value` pairs. `CN` is the server name and// `O` is the organization. This prints `CN=api.example.com, O=Shop`.if ($info != null) { echo $info->subject(), "\n";}CN=api.example.com, O=ShopFinding the server name
<?nvs// Shows how to take the server name out of the certificate's subject.
// 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, C=AT' });
// The pairs are separated by `, `. The loop finds the pair that starts with `CN=`,// and `Core\Str::after` returns the text after it. This prints `server: shop.example.com`.foreach (Core\Str::split($session->subject(), ', ') as tainted string $pair) { if (Core\Str::startsWith($pair, 'CN=')) { echo 'server: ', Core\Str::after($pair, 'CN=') ?? '', "\n"; }}server: shop.example.comChecking the partner organization
<?nvs// Shows a check that the certificate of a payment partner names the expected// organization before the program sends an order.
// Fixed replies with TLS connections built for a test, so the example runs// without a network. The second server has a certificate from somebody else.Core\Test::answerHttp('https://pay.example.com/orders', 200, { tls: Core\Test::tlsSession({ subject: 'CN=pay.example.com, O=Example Payments' }),});Core\Test::answerHttp('https://pay.example.net/orders', 200, { tls: Core\Test::tlsSession({ subject: 'CN=pay.example.net, O=Unknown' }),});
string $expected = 'O=Example Payments';array<string> $urls = ['https://pay.example.com/orders', 'https://pay.example.net/orders'];
// `Core\Str::contains` checks that the subject has the expected pair.// This prints `send:` for the first address and `stop:` for the second.foreach ($urls as string $url) { bool $trusted = false; ?Core\Http\TlsInfo $info = Core\Http\Client::get($url)->tls(); if ($info != null) { $trusted = Core\Str::contains($info->subject(), $expected); } echo $trusted ? 'send: ' : 'stop: ', $url, "\n";}send: https://pay.example.com/ordersstop: https://pay.example.net/orders