Simple Way to Parse an x509 Certificate with PHP

Posted by Nessa | Posted in , | Posted on November 3, 2010


PHP has a nifty little function for parsing an x.509 SSL certificate into an array to easily pull out the elements: openssl_x509_parse .Essentially, all you need to do is load up the contents of the certificate, either through a file or POST value, and enclose it in the array. Here’s a simple script:

$sslcert = file_get_contents("/etc/ssl/certs/");
$sslcert = array(openssl_x509_parse($sslcert,TRUE));
foreach ($sslcert as $name => $arrays){
foreach ($arrays as $title => $value){
echo $value . "\n";
foreach($value as $subtitle => $subvalue){
echo $subtitle . " : " . $subvalue . "\n";
echo $title .  "\n";

The results are several multidimensional arrays, so depending on the data you need, you may need to keep adding foreach loops to get that data.

Be Sociable, Share!

Comments (5)

[…] This post was mentioned on Twitter by junichi_y, Aron Watson. Aron Watson said: Simple Way to Parse an x509 Certificate with PHP – […]

[…] a new post to her blog, Vanessa Vasile has a reminder about a built-in PHP function that can help quite a bit when you’re working with SSL […]

Hi Vanessa,

line 8: “echo $title;\n” makes more sense ;)


Interesting post. I’m currently start to work with 509 certificates.

And I’m curiousing your blog

It’s good

Greetings :)

Post a comment