Last active
September 15, 2023 06:02
-
-
Save MircoBabin/5c420c277b5a039df76c0469ed04c491 to your computer and use it in GitHub Desktop.
Check https certificate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
MIT license | |
Copyright (c) 2022 Mirco Babin | |
Permission is hereby granted, free of charge, to any person | |
obtaining a copy of this software and associated documentation | |
files (the "Software"), to deal in the Software without | |
restriction, including without limitation the rights to use, | |
copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the | |
Software is furnished to do so, subject to the following | |
conditions: | |
The above copyright notice and this permission notice shall be | |
included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | |
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | |
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | |
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
OTHER DEALINGS IN THE SOFTWARE. | |
*/ | |
$now = new DateTime(); | |
$url = 'https://php.net'; | |
$certificateInfo = httpsCertificateLookup($url); | |
if ($certificateInfo['https-success']) { | |
$interval = $now->diff($certificateInfo['https-certificate-validto']); | |
echo $interval->format('%R%a days to expiration'); | |
} else { | |
echo 'Error retrieving certificate:'.PHP_EOL; | |
echo $certificateInfo['https-error']; | |
} | |
function httpsCertificateLookup(string $url) | |
{ | |
$result = [ | |
'https-success' => false, | |
'https-error' => '', | |
'https-certificate-data' => null, | |
'https-certificate-domain' => null, | |
'https-certificate-validfrom' => null, | |
'https-certificate-validto' => null, | |
]; | |
$scheme = null; | |
$host = null; | |
$port = null; | |
$parts = parse_url($url); | |
if (false !== $parts) { | |
if (isset($parts['scheme'])) { | |
$scheme = strtolower($parts['scheme']); | |
} | |
if (isset($parts['host'])) { | |
$host = strtolower($parts['host']); | |
} | |
if (isset($parts['port'])) { | |
$port = intval($parts['port']); | |
} | |
} | |
if (null === $host) { | |
$result['https-error'] = 'Could not determine host for: '.$url; | |
return $result; | |
} | |
if (null === $port) { | |
if ('http' === $scheme) { | |
$port = 80; | |
} elseif ('https' === $scheme) { | |
$port = 443; | |
} | |
if (null === $port) { | |
$result['https-error'] = 'Could not determine port for: '.$url; | |
return $result; | |
} | |
} | |
$context = @stream_context_create(['ssl' => ['capture_peer_cert' => true, 'verify_peer' => false]]); | |
$errno = 0; | |
$errstr = ''; | |
$hostAndPort = $host.':'.$port; | |
$stream = @stream_socket_client('ssl://'.$hostAndPort, $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $context); | |
if (false !== $stream) { | |
$context_parms = @stream_context_get_params($stream); | |
$certificate = $context_parms['options']['ssl']['peer_certificate']; | |
@fclose($stream); | |
$result['https-success'] = true; | |
$x509_data = @openssl_x509_parse($certificate); | |
if (is_array($x509_data)) { | |
$result['https-certificate-data'] = $x509_data; | |
if (isset($x509_data['subject']['CN'])) { | |
$result['https-certificate-domain'] = $x509_data['subject']['CN']; | |
} | |
if (isset($x509_data['validFrom_time_t'])) { | |
$time = new DateTime('@'.$x509_data['validFrom_time_t']); | |
$time->setTimezone(new DateTimeZone('UTC')); | |
$result['https-certificate-validfrom'] = $time; | |
} | |
if (isset($x509_data['validTo_time_t'])) { | |
$time = new DateTime('@'.$x509_data['validTo_time_t']); | |
$time->setTimezone(new DateTimeZone('UTC')); | |
$result['https-certificate-validto'] = $time; | |
} | |
} | |
} else { | |
$result['https-error'] = 'ssl://'.$hostAndPort.' failed '.$errno.': '.$errstr; | |
} | |
return $result; | |
} |
I'm not subscribed to the php #externals mailinglist. So I ping @derickr here. This is about a tool to check the https certificate for https://php.net and possible other sites.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
On php #externals maillist https://externals.io/message/121040 @bohwaz shared a certificate validation check. See https://gist.github.com/bohwaz/a3e4eb91e82f12768b69f8d24cf0cca8
Because this initiative comes from the php #externals mailinglist, I thought I also share my solution. Because I also have to check certificate expiration dates for websites.