PHP SSL证书序列号,以十六进制表示

时间:2011-06-21 13:53:27

标签: php security ssl x509 serial-number

我需要显示有关序列号的SSL证书信息。 当我使用

$cert = file_get_contents('mycert.crt');
$data=openssl_x509_parse($cert,true);
$data['serialNumber']

我收到喜欢的     -5573199485241205751 但是当我运行命令时     openssl x509 -in'mycert.crt'-noout -serial 我收到     串行= B2A80498A3CEDC09 是否有可能在PHP中收到? 感谢。

4 个答案:

答案 0 :(得分:3)

您可能正在将序列号作为字节数组接收。将这些字节转换为十六进制,你应该得到你使用openssl看到的序列。

答案 1 :(得分:2)

感谢Petey B这个想法很有意思所以帮助我找到搜索的方向。 解决方案是:

$serial_number= strtoupper(dechex($serial_number));

答案 2 :(得分:1)

php > $value = hexdec('B2A80498A3CEDC09');
php > echo dechex($value);
b2a80498a3cee000

这似乎不起作用。可能是由于浮动转换。

我看到了一个bcmath解决方案。

这是来自confusa(http://www.assembla.com/code/confusa/git/nodes/lib/ca/Certificate.php?rev=a80a040c97fde2c170bb290d756c6729883fe80a):

                    /*
                     * PHP will return the serial as an integer, whereas
                     * everybody else use the hex-represenatation of the
                     * number.
                     *
                     * Due to the fact that Comodo uses *insanely* large
                     * serial-numbers, we need to be a bit creative when we
                     * get the serial as PHP won't cope with numbers larger
                     * than MAX_INT (2**32 on 32 bits arch)
                     */
                    $serial = $this->x509_parsed['serialNumber'] . "";
                    $base = bcpow("2", "32");
                    $counter = 100;
                    $res = "";
                    $val = $serial;

                    while($counter > 0 && $val > 0) {
                            $counter = $counter - 1;
                            $tmpres = dechex(bcmod($val, $base)) . "";
                            /* adjust for 0's */
                            for ($i = 8-strlen($tmpres); $i > 0; $i = $i-1) {
                                    $tmpres = "0$tmpres";
                            }
                            $res = $tmpres .$res;
                            $val = bcdiv($val, $base);
                    }
                    if ($counter <= 0) {
                            return false;
                    }
                    return strtoupper($res);

我没有启用bcmath,所以我暂时无法测试它。

答案 3 :(得分:0)

您可以将phpseclib用于此目的:

<?php
include('Math/BigInteger.php');

$cert_body = file_get_contents('mycert.crt');
$cert_decoded = openssl_x509_parse($cert_body, true);
echo $cert_decoded['serialNumber'] // outputs 5573199485241205751

$cert_serial = new Math_BigInteger($cert_decoded['serialNumber']);
$cert_serial = strtoupper($cert_serial->toHex());

echo $cert_serial // outputs B2A80498A3CEDC09
?>