openssl_x509_read
(PHP 4 >= 4.0.6, PHP 5)
openssl_x509_read — Parse an X.509 certificate and return a resource identifier for it
Описание
openssl_x509_read() parses the certificate supplied by x509certdata and returns a resource identifier for it.
Список параметров
- x509certdata
-
Возвращаемые значения
Returns a resource identifier on success, or FALSE on failure.
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Криптографические расширения
- OpenSSL
- openssl_cipher_iv_length
- openssl_csr_export_to_file
- openssl_csr_export
- openssl_csr_get_public_key
- openssl_csr_get_subject
- openssl_csr_new
- openssl_csr_sign
- openssl_decrypt
- openssl_dh_compute_key
- openssl_digest
- openssl_encrypt
- openssl_error_string
- openssl_free_key
- openssl_get_cert_locations
- openssl_get_cipher_methods
- openssl_get_md_methods
- openssl_get_privatekey
- openssl_get_publickey
- openssl_open
- openssl_pbkdf2
- openssl_pkcs12_export_to_file
- openssl_pkcs12_export
- openssl_pkcs12_read
- openssl_pkcs7_decrypt
- openssl_pkcs7_encrypt
- openssl_pkcs7_sign
- openssl_pkcs7_verify
- openssl_pkey_export_to_file
- openssl_pkey_export
- openssl_pkey_free
- openssl_pkey_get_details
- openssl_pkey_get_private
- openssl_pkey_get_public
- openssl_pkey_new
- openssl_private_decrypt
- openssl_private_encrypt
- openssl_public_decrypt
- openssl_public_encrypt
- openssl_random_pseudo_bytes
- openssl_seal
- openssl_sign
- openssl_spki_export_challenge
- openssl_spki_export
- openssl_spki_new
- openssl_spki_verify
- openssl_verify
- openssl_x509_check_private_key
- openssl_x509_checkpurpose
- openssl_x509_export_to_file
- openssl_x509_export
- openssl_x509_fingerprint
- openssl_x509_free
- openssl_x509_parse
- openssl_x509_read
Коментарии
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 );
?>
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
;)
To get the real timestamps as integer values for the validity daterange you can use as follows:
<?php
$data = openssl_x509_parse(file_get_contents('/path/to/cert.crt'));
$validFrom = date('Y-m-d H:i:s', $data['validFrom_time_t']);
$validTo ) date('Y-m-d H:i:s', $data['validTo_time_t']);
echo $validFrom . "\n";
echo $validTo . "\n";
?>