PHP Doku:: Parst ein X.509-Zertitifikat und gibt eine Ressource zurück - function.openssl-x509-read.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzKryptografische ErweiterungenOpenSSLOpenSSL Funktionenopenssl_x509_read

Ein Service von Reinhard Neidl - Webprogrammierung.

OpenSSL Funktionen

<<openssl_x509_parse

Datenbankerweiterungen>>

openssl_x509_read

(PHP 4 >= 4.0.6, PHP 5)

openssl_x509_readParst ein X.509-Zertitifikat und gibt eine Ressource zurück

Beschreibung

resource openssl_x509_read ( mixed $x509certdata )

Die Funktion openssl_x509_read() parst das in x509certdata übergebene Zertifikat und gibt eine Ressource zurück.

Parameter-Liste

x509certdata

Rückgabewerte

Gibt im Erfolgsfall eine Ressource zurück oder FALSE falls ein Fehler auftritt.


3 BenutzerBeiträge:
- Beiträge aktualisieren...
Gabe Martin-Dempesy at mudbugmedia dot com
11.08.2006 0:04
For those interested in parsing the timestamps from the certificate, such as the valid to and valid from times, it should be noted that the format returned by this function is: YYMMDDHHMMSS

This code snippet is useful for generating a unix timestamp for this purpose:
<?php

$fp
= fopen("/path/to/cert.crt", "r");
$cert = fread($fp, 8192);
fclose($fp);

$data = openssl_x509_parse($cert);
/**
 * Convert a timestamp from openssl_x509_parse to a unix timestamp
 * @param string $in openssl timestamp
 * @return integer unix timestamp
 */
function openssl_to_timestamp ($in) {
       
$year  = substr($in, 0, 2); /* NOTE: Yes, this returns a two digit year */
       
$month = substr($in, 2, 2);
       
$day   = substr($in, 4, 2);
       
$hour  = substr($in, 6, 2);
       
$min   = substr($in, 8, 2);
       
$sec   = substr($in, 10, 2);
        return
gmmktime($hour, $min, $sec, $month, $day, $year);
}
var_dump(gmdate('r', openssl_to_timestamp($data['validTo'])));
?>

This will output: string(31) "Fri, 29 Aug 2008 16:45:15 +0000"
Compare this with the output of `openssl x509 -in cert.crt -noout -text`:
        Validity
            Not After : Aug 29 16:45:15 2008 GMT

4.06.2003 18:32
After some tests I've been able to get some results this way ...

<?php

 $fp
= fopen("/etc/httpd/conf/ssl/moncertif.crt", "r");
 
$cert = fread($fp, 8192);
 
fclose($fp);

echo
"Read<br>";
echo
openssl_x509_read($cert);
echo
"<br>";
echo
"*********************";
echo
"<br>";
echo
"Parse<br>";
print_r(openssl_x509_parse($cert));
/*
// or
print_r(openssl_x509_parse( openssl_x509_read($cert) ) );
*/

?>

enjoy
;)
anthony dot whitehead at rfv dot sfa dot se
3.02.2003 14:32
Short HOWTO for getting data out of a client certificate via an SSL enabled iPlanet (Netscape Enterprise or Sun ONE) web server.

The iPlanet server sets $_SERVER["CLIENT_CERT"] whenever a client authenticates with a certificate. This variable contains an encoded representation of the certificate presented by the client. This in itself is useless to scripts or applications, we need to extract the actual information from the encoding. It turns out that we are in luck, the encoding is NEARLY a standard PEM encoding which can be read by the openssl_x509_read() function. A standard PEM has a begin line, an end line and inbetween is a base64 encoding of the DER representation of the certificate. PEM requires that linefeeds be present every 64 characters, however this is already the case with our CLIENT_CERT variable. For some reason the iPlanet server neglects to attach the begin and end headers, all that is required to allow access to the certificate is replacing these headers. Here is a small code excerpt for doing just that and printing out the raw certificate data.

<?php
    $beginpem
= "-----BEGIN CERTIFICATE-----\n";
   
$endpem = "-----END CERTIFICATE-----\n";

   
// Small function to print the data recursivly.
   
function print_element($item, $key)
    {
        if(
is_array( $item ) )
        {
            echo
"$key is Array:\n";
           
array_walk( $item, 'print_element' );
            echo
"$key done\n";
        }
        else
            echo
"$key = $item\n";
    }

   
// Build the PEM string.
   
$pemdata = $beginpem.$_SERVER["CLIENT_CERT"]."\n".$endpem;

   
// Get a certificate resource from the PEM string.
   
$cert = openssl_x509_read( $pemdata );

   
// Parse the resource and print out the contents.
   
$cert_data = openssl_x509_parse( $cert );
   
array_walk( $cert_data, 'print_element' );

   
// Free the resource
   
openssl_x509_free( $cert );
?>



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)