Wednesday, January 12, 2011

Fetch SSL Certificate - Public Key Details

$url = 'www.digicert.com'; // For example
$context = stream_context_create();
$res = stream_context_set_option($context, 'ssl', 'capture_peer_cert', true);
$res = stream_context_set_option($context, 'ssl', 'verify_host', true);
if ($socket = stream_socket_client("ssl://$url:443/", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $context)) {
if ($options = stream_context_get_options($context)) {
if (isset($options['ssl']) && isset($options['ssl']['peer_certificate'])) {
$x509_resource = $options['ssl']['peer_certificate'];
$cert_arr = openssl_x509_parse($x509_resource);
openssl_x509_export($x509_resource,$x509_string);

$public_key_res = openssl_pkey_get_public($x509_string);
$public_key_arr = openssl_pkey_get_details($public_key_res);

print_r($public_key_arr);
}
}
}

No comments: