(PHP 7 >= 7.4.0, PHP 8)
openssl_x509_verify — 验证 x509 证书的数字签名是否与公钥匹配
$certificate
, OpenSSLAsymmetricKey|OpenSSLCertificate|array|string $public_key
): int
openssl_x509_verify() 验证 certificate
证书是否由与公钥 public_key
相对应的私钥签名。
x509
有关有效值的列表,请参见 密钥/证书参数。
public_key
OpenSSLAsymmetricKey - 由 openssl_get_publickey() 返回的密钥
string - PEM 格式的密钥(例如 -----BEGIN PUBLIC KEY----- MIIBCgK...
)
如果签名正确,则返回 1;如果签名不正确,则返回 0;如果出错,则返回 -1。
版本 | 描述 |
---|---|
8.0.0 |
certificate 现在接受 OpenSSLCertificate 实例;以前,接受 OpenSSL X.509 类型的 资源。 |
8.0.0 |
public_key 现在接受 OpenSSLAsymmetricKey 或 OpenSSLCertificate 实例;以前,接受 OpenSSL key 或 OpenSSL X.509 类型的 资源。 |
示例 #1 openssl_x509_verify() 示例
<?php
$hostname = "news.php.net";
$ssloptions = array(
"capture_peer_cert" => true,
"capture_peer_cert_chain" => true,
"allow_self_signed"=> false,
"CN_match" => $hostname,
"verify_peer" => true,
"SNI_enabled" => true,
"SNI_server_name" => $hostname,
);
$ctx = stream_context_create( array("ssl" => $ssloptions) );
$result = stream_socket_client("ssl://$hostname:443", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $ctx);
$cont = stream_context_get_params($result);
$x509 = $cont["options"]["ssl"]["peer_certificate"];
$certparsed = openssl_x509_parse($x509);
foreach($cont["options"]["ssl"]["peer_certificate_chain"] as $chaincert)
{
$chainparsed = openssl_x509_parse($chaincert);
$chain_public_key = openssl_get_publickey($chaincert);
$r = openssl_x509_verify($x509, $chain_public_key);
if ($r==1)
{
echo $certparsed['subject']['CN'];
echo " was digitally signed by ";
echo $chainparsed['subject']['CN']."\n";
}
}
?>