openssl_x509_check_private_key()
(PHP 4 >= 4.2.0, PHP 5, PHP 7)
检查私钥是否对应于证书
说明
openssl_x509_check_private_key(mixed $cert, mixed $key): bool
检查给定的私钥$key是否和证书$cert对应。
Warning这个函数不会检查密钥$key是否真的是私钥。它只是比较了和密钥匹配的公共材料(比如,RSA密钥的指数和模量)和/或密钥参数(比如,EC密钥的参数)。
这也意味着,比如,提供给$key赋一个公钥值,该函数可能返回
TRUE.
参数
- $cert
证书。
- $key
私钥。
返回值
如果给定的私钥$key和证书$cert对应,返回TRUE否则返回FALSE.
This function will return FALSE if the private key requires a pass phrase.
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);