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秘密鍵が証明書に対応するかを確認する

説明

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

private_keycertificate に対応する秘密鍵かどうかを調べます。

警告

この関数は private_key が秘密鍵かどうかをチェックしません。 単に、公開されている材料 (例: RSA 鍵の exponent や modulus) かつ/または キーペアのキーのパラメータ (例: ECキー の EC params) を比較しているだけです。

これはたとえば、公開鍵を private_key に与えても、 関数が true を返す可能性があるということです。

パラメータ

certificate

証明書。

private_key

秘密鍵。

戻り値

private_keycertificate に対応する秘密鍵の場合に true、それ以外の場合に false を返します。

変更履歴

バージョン 説明
8.0.0 certificate は、 OpenSSLCertificate クラスのインスタンスを受け入れるようになりました。 これより前のバージョンでは、 OpenSSL X.509 型のリソースを受け入れていました。
8.0.0 private_key は、 OpenSSLAsymmetricKey または OpenSSLCertificate クラスのインスタンスを受け入れるようになりました。 これより前のバージョンでは、 OpenSSL key または OpenSSL X.509 型のリソースを受け入れていました。
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