PHP 8.3.4 Released!

openssl_x509_check_private_key

(PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8)

openssl_x509_check_private_keyÜberprüft, ob ein privater Schlüssel zu einem Zertifikat passt

Beschreibung

openssl_x509_check_private_key(OpenSSLCertificate|string $certificate, OpenSSLAsymmetricKey|OpenSSLCertificate|array|string $private_key): bool

Prüft, ob der übergebene Schlüssel private_key der private Schlüssel zum Zertifikat certificate ist.

Warnung

Die Funktion überprüft nicht, ob private_key tatsächlich ein privater Schlüssel ist. Sie vergleicht lediglich die öffentlichen Bestandteile (z. B. Exponent und Modulus eines RSA-Schlüssels) und/oder die Schlüsselparameter (z. B. die EC-Parameter eines EC-Schlüssels).

Dies bedeutet beispielsweise, dass ein öffentlicher Schlüssel als private_key angegeben werden kann, woraufhin die Funktion möglicherweise true zurückgibt.

Parameter-Liste

certificate

Das Zertifikat.

private_key

Der private Schlüssel.

Rückgabewerte

Gibt true zurück, wenn private_key der private Schlüssel zum Zertifikat certificateist, andernfalls false.

Changelog

Version Beschreibung
8.0.0 certificate akzeptiert nun eine OpenSSLCertificate-Instanz; vorher wurde eine Ressource vom Typ OpenSSL X.509 akzeptiert.
8.0.0 private_key akzeptiert nun eine OpenSSLAsymmetricKey- oder OpenSSLCertificate-Instanz; vorher wurde eine Ressource vom Typ OpenSSL-Schlüssel oder OpenSSL X.509 akzeptiert.
add a note

User Contributed Notes 2 notes

up
7
tomsie at toms dot ie
6 years ago
This function DOES return TRUE if the key has a passphrase, you just need to set up the data in such a way that the function can understand it. It is not documented here.

This error message led me to the solution:

PHP Warning: openssl_x509_check_private_key(): key array must be of the form array(0 => key, 1 => phrase)

So this works:

$certFile = file_get_contents('cert.crt');
$keyFile = file_get_contents('cert.key');
$keyPassphrase = "password1234";
$keyCheckData = array(0=>$keyFile,1=>$keyPassphrase);
$result = openssl_x509_check_private_key($certFile,$keyCheckData);
up
0
jared at enhancesoft dot com
8 years ago
This function will return FALSE if the private key requires a pass phrase.
To Top