Skip to content

Core\Http\TlsInfo::isVerified

$tlsInfo->isVerified(): bool

Returns true when the server's certificate was checked for this connection, and false when it was not.

When a program calls an https address, the client checks two things. The certificate must be signed by an authority the client trusts, and it must be for the server name in the address. Only when both checks run does isVerified() return true. A configuration can turn a check off for one server, for example a test server with a self-signed certificate. A program or a test uses isVerified() to see that. Every other call should still return true.

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 check itself, a warning line, and an audit that allows only one server to skip the check.

Return value

Returns bool. true if the chain was checked up to a trusted root, and the URL's host matched the certificate. The trusted roots are the configured roots or the call's own tlsCa. false if the call skipped either check. A call can skip a check only if a [capabilities.tls] grant lists the host.

Changelog

VersionChange
0.0.6Core\Http\Client::openStream opens a streamed request, and Core\Http\TlsInfo->isVerified() checks the certificate. The old names were stream and verified.

Examples

Was the certificate checked

<?nvs
// Shows how to find out whether the server's certificate was checked for 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 over that connection.
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();
// `isVerified()` returns `true` when the certificate and the server name were both
// checked. A test connection is checked by default, so this prints `checked`.
if ($info != null) {
echo $info->isVerified() ? 'checked' : 'not checked', "\n";
}
Output
checked

A warning for an unchecked connection

<?nvs
// Shows a warning that a program writes when a server's certificate was not checked.
class Warnings
{
// Prints a warning when the certificate was not checked.
// Prints nothing when it was checked.
public static function check(string $host, Core\Http\TlsInfo $info): void
{
if (!$info->isVerified()) {
echo 'warning: the certificate of ', $host, " was not checked\n";
}
}
}
// Two TLS connections built for a test. The second one says that the
// certificate was not checked, as with a server that uses a self-signed certificate.
var $checked = Core\Test::tlsSession({});
var $unchecked = Core\Test::tlsSession({ verified: false });
// This prints one line: `warning: the certificate of dev.example.com was not checked`.
Warnings::check('api.example.com', $checked);
Warnings::check('dev.example.com', $unchecked);
Output
warning: the certificate of dev.example.com was not checked

Only one partner may skip the check

<?nvs
// Shows an audit that allows one partner server to skip the certificate check,
// and reports every other server that skipped it.
// Fixed replies with TLS connections built for a test, so the example runs without
// a network. Two servers skip the check, and only `legacy.example.com` may do that.
Core\Test::answerHttp('https://legacy.example.com/orders', 200, { tls: Core\Test::tlsSession({ verified: false }) });
Core\Test::answerHttp('https://shop.example.com/orders', 200, { tls: Core\Test::tlsSession({}) });
Core\Test::answerHttp('https://stock.example.com/orders', 200, { tls: Core\Test::tlsSession({ verified: false }) });
string $allowed = 'legacy.example.com';
array<string> $hosts = ['legacy.example.com', 'shop.example.com', 'stock.example.com'];
// The loop calls every server. It reports a server that skipped the check,
// unless that server is the allowed one.
uint $problems = 0;
foreach ($hosts as string $host) {
?Core\Http\TlsInfo $info = Core\Http\Client::get('https://' . $host . '/orders')->tls();
if ($info != null) {
if (!$info->isVerified() && $host != $allowed) {
echo 'not allowed: ', $host, " skipped the check\n";
$problems = $problems + 1;
}
}
}
// This prints `not allowed: stock.example.com skipped the check` and then `problems: 1`.
echo 'problems: ', $problems, "\n";
Output
not allowed: stock.example.com skipped the check
problems: 1