2

I am writing a little script that displays the information of a given certificate taken from a specific website:

$url = "http://www.google.com";
$orignal_parse = parse_url($url, PHP_URL_HOST);
$get = stream_context_create(array("ssl" => array("capture_peer_cert" => TRUE)));
$read = stream_socket_client("ssl://".$orignal_parse.":443", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $get);
$cert = stream_context_get_params($read);
$certinfo = openssl_x509_parse($cert['options']['ssl']['peer_certificate']);
echo "Name: ".$certinfo['name']."<br/>";
echo "Subject: ".$certinfo['subject']['C']." ".$certinfo['subject']['ST']." ".$certinfo['subject']['CN']."<br/>";
echo "Issuer: ".$certinfo['issuer']['C']." ".$certinfo['subject']['O']." ".$certinfo['subject']['CN']."<br/>";
echo "Version: ".$certinfo['version']."<br/>";
echo "Valid from: ".$certinfo['validFrom']." to ".$certinfo['validTo']."<br/>";

For a full description of what the arrays returns go here: How to get SSL certificate info with CURL in PHP?

The problem is that $certinfo['validFrom'] returns a value like this:

170904000000Z

And I ahave no idea what it is. What I want is to convert $certinfo['validFrom'] and $certinfo['validTo'] to a human date format, but I don't know what type of time this is, I thought it was Unix but it didn't work, then I noticed that there is a "Z" at the end of the string:

echo "Valid from: ".gmdate("Y-m-d",$certinfo['validFrom'])." to ".gmdate("Y-m-d",$certinfo['validTo'])."<br/>"; 

returns: Valid from: 7385-09-24 to 7683-11-24

And that is obviously wrong.

I checked the certificate on the browser and the values:

Valid from: 170904000000Z to 180313235959Z

Well converted should be

Valid from 2017-09-03 to 2017-03-13

Thanks

multimediaxp
  • 9,348
  • 13
  • 49
  • 80

2 Answers2

2

170904000000Z Isnt it ymdhmsT ? 4th September 2017 at midnight UTC?

miknik
  • 5,748
  • 1
  • 10
  • 26
1

PHP documentation for openssl_x509_parse says

The structure of the returned data is (deliberately) not yet documented, as it is still subject to change.

but there are some hints on user comment here, like the sample code below. Note, that as quoted above, you shouldn't rely on this array index in production code as it may change w/o notice.

$validFrom = date('Y-m-d H:i:s', $data['validFrom_time_t']);
$validTo = date('Y-m-d H:i:s', $data['validTo_time_t']);
multimediaxp
  • 9,348
  • 13
  • 49
  • 80
marekful
  • 14,986
  • 6
  • 37
  • 59