duandi1919 2019-07-04 10:38
浏览 167

使用PHP检查SSL证书哈希

I am trying to make a simple tool to check hashes of the SSL certificate (csr, key and crt) files. My code does not seem to be working correctly. It checks the hashes but simulating bad certificates do not give me an error.

Tried making simple HTML and PHP app.

<!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>";
}
?>

If hashes match, success message is shown, otherwise error message is shown.

  • 写回答

2条回答 默认 最新

  • du6333137 2019-07-04 13:05
    关注

    If all variables: $hashCsr, $hashKey and $hashCrt are empty it will pass your "certificate match" test.

    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>";
    }
    

    By the way you can use php openssl extension

    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作