php - 使用 PHP 检查 SSL 证书哈希

标签 php ssl certificate

我正在尝试制作一个简单的工具来检查 SSL 证书(csr、 key 和 crt)文件的哈希值。我的代码似乎没有正常工作。它会检查哈希值,但模拟错误的证书不会给我错误。

尝试制作简单的 HTML 和 PHP 应用。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>SSL Test</title>
</head>
<body>
<div style="text-align:center">
    <h1>Certificate Test</h1>
    <form name="certForm" action="verify.php" method="post">
        <div>
            <label for="csr">CSR file:</label>
            <input type="file" name="csr" id="csr" accept=".csr"/>
        </div>
        <div>
            <label for="key">KEY file:</label>
            <input type="file" name="key" id="key" accept=".key"/>
        </div>
        <div>
            <label for="crt">CRT file:</label>
            <input type="file" name="crt" id="crt" accept=".crt,.cert"/>
        </div>

        <button type="submit">Check</button>
        <button type="reset">Reset</button>
    </form>
</div>

</body>
</html>
<?php
header('Content-Type: text/html; charset=utf-8');

$csr = $_POST['csr'];
$key = $_POST['key'];
$crt = $_POST['crt'];

if (!$csr || !$key || !$crt) {
    die('Files not specified. Go back and try again');
}

$hashCsr = exec("openssl req -in $csr -pubkey -noout -outform pem | sha256sum");
$hashKey = exec("openssl pkey -in $key -pubout -outform pem | sha256sum");
$hashCrt = exec("openssl x509 -in $crt -pubkey -noout -outform pem | sha256sum");

echo "<p><strong>File:</strong> $csr <strong>Hash:</strong> $hashCsr</p>";
echo "<p><strong>File:</strong> $key <strong>Hash:</strong> $hashKey</p>";
echo "<p><strong>File:</strong> $crt <strong>Hash:</strong> $hashCrt</p>";

if (($hashCsr === $hashKey) && ($hashCsr === $hashCrt) && ($hashKey === $hashCrt)) {
    echo "<p style='color: green;'>Certificates match!</p>";
}
else {
    echo "<p style='color: red;'>Certificates do NOT match!</p>";
}
?>

如果哈希匹配,则显示成功消息,否则显示错误消息。

最佳答案

如果所有变量:$hashCsr、$hashKey 和 $hashCrt 都是空的,它将通过您的“证书匹配”测试。

if (($hashCsr === $hashKey) && ($hashCsr === $hashCrt) && ($hashKey === $hashCrt) && $hashCsr != '')
{
    echo "<p style='color: green;'>Certificates match!</p>";
}
else
{
    echo "<p style='color: red;'>Certificates do NOT match!</p>";
}

顺便说一下,你可以使用 php openssl extension

关于php - 使用 PHP 检查 SSL 证书哈希,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56886156/

相关文章:

c# - WCF 证书身份验证不起作用

为 RapidSSL 数字证书设置 APR 连接器的 Tomcat 6

php - 为什么数据库没有反射(reflect) php INSERT 所做的更改?

ssl - 打开 HTTPS 页面时的浏览器步骤

iis - IIS 5.1 中的 HTTPS

installation - 使用Inno Setup,如何导入证书.cer文件?

php - 在此服务器上找不到请求的资源

javascript - PHP 如果条件不工作

php - 正则表达式 - 积极的后视问题

ssl - 无法创建 SSL/TLS 安全通道仅发生在 Windows Server 2012 中