you can get (and save to file) public key using openssl_pkey_get_details(resource $key ) function:
<?php
$pub_key = openssl_pkey_get_public(file_get_contents('./cert.crt'));
$keyData = openssl_pkey_get_details($pub_key);
fule_put_contents('./key.pub', $keyData['key']);
?>
openssl_pkey_get_public
(PHP 4 >= 4.2.0, PHP 5)
openssl_pkey_get_public — Extrahiert einen öffentlichen Schlüssel aus einem Zertifikat und bereitet diesen zur Nutzung vor
Beschreibung
openssl_get_publickey() extrahiert den öffentlichen Schlüssel aus certificate und bereitet ihn für den Gebrauch durch andere Funktionen vor.
Parameter-Liste
- certificate
-
certificate kann folgendes sein:
- eine X.509 Zertifikatsressource
- Eine Zeichenkette im Format file://path/to/file.pem. Die angegebene Datei muß ein PEM-kodiertes Zertifikat und/oder einen privaten Schlüssel enthalten.
- Ein öffentlicher Schlüssel im PEM-Format.
Rückgabewerte
Gibt bei Erfolg eine Schlüssel-Resource zurück, FALSE wenn ein Fehler auftritt.
openssl_pkey_get_public
07-May-2007 09:40
dankybastard at hotmail
09-Feb-2005 05:52
09-Feb-2005 05:52
You must also use the string representation of the certificate to get the public key resource:
$dn = array(); // use defaults
$res_privkey = openssl_pkey_new();
$res_csr = openssl_csr_new($dn, $res_privkey);
$res_cert = openssl_csr_sign($res_csr, null, $res_privkey, $ndays);
openssl_x509_export($res_cert, $str_cert);
$res_pubkey = openssl_pkey_get_public($str_cert);
09-Aug-2004 08:44
This documentation notes it can take a PEM-formatted private key, but as per bug #25614, this is not possible in any form. The function simply returns a FALSE.
The only thing you can get public keys out of are X.509 certificates.
Furthermore, there is NO way to export a public key into a PEM-encoded form.