Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Expiry date from the SSL Certificate file in PHP

Tags:

php

ssl

I want to get the expiry date from the SSL Certificate file. There is a web page in PHP that I've created, in which user can upload his SSL Certificate file and I will have to get the expiry date of that file using PHP.

like image 225
Naveed Butt Avatar asked Jul 28 '11 18:07

Naveed Butt


People also ask

How can I check SSL certificate expiry date?

Chrome has made it simple for any site visitor to get certificate information with just a few clicks: Click the padlock icon in the address bar for the website. Click on Certificate (Valid) in the pop-up. Check the Valid from dates to validate the SSL certificate is current.

How do I know when my RSA key expires?

The RSA private/public key don't have dates in them so they don't expire. RSA Private/Public keys are used for asymmetric cryptography operations. X509 certificates uses a private key to "sign" the certificate so that the corresponding public key can be used to verify the data in the certificate hasn't been modified.

What is SSL certificate expiry?

That means that every website needs to renew or replace its SSL certificate at least once every two years.


1 Answers

This worked for me:

$url = "https://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 '<pre>';
print_r($certinfo);
echo '</pre>';

fine ssl expire time in this fild

[validTo_time_t] => 1523164135 // expire time.

Convert Date & Time

$valid_from = date(DATE_RFC2822,$certinfo['validFrom_time_t']);
$valid_to = date(DATE_RFC2822,$certinfo['validTo_time_t']);
echo "Valid From: ".$valid_from."<br>";
echo "Valid To:".$valid_to."<br>";

Result:

Valid From: Mon, 09 Oct 2017 07:00:00 +0700 Valid To: Wed, 10 Oct 2018 06:59:59 +0700

like image 61
Milad.biniyaz Avatar answered Oct 30 '22 21:10

Milad.biniyaz