Simple Way to Parse an x509 Certificate with PHP

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

5

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:

<?php
$sslcert = file_get_contents("/etc/ssl/certs/secure.v-nessa.net");
$sslcert = array(openssl_x509_parse($sslcert,TRUE));
//print_r($sslcert);
foreach ($sslcert as $name => $arrays){
foreach ($arrays as $title => $value){
if(is_array($value)){
echo $value . "\n";
foreach($value as $subtitle => $subvalue){
echo $subtitle . " : " . $subvalue . "\n";
}
}else{
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 – http://bit.ly/9UZFw0 […]

[…] 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 ;)

Hi

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

And I’m curiousing your blog

It’s good

Greetings :)

Post a comment