PHP SSL 证书序列号(十六进制)

发布于 2024-11-16 08:50:01 字数 303 浏览 5 评论 0原文

我需要显示有关序列号的 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接收吗? 谢谢。

I need to display SSL certificat information about Serial Number.
When I use

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

I receive like
-5573199485241205751
But when I run command
openssl x509 -in 'mycert.crt' -noout -serial
I receive
serial=B2A80498A3CEDC09
is it possible receive in PHP?
Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

静待花开 2024-11-23 08:50:01

您可能会以字节数组形式接收序列号。将这些字节转换为十六进制,您应该获得使用 openssl 看到的序列号。

You are probably recieving the serial number as a byte array. Convert those bytes to hex and you should get the serial that you are seeing using openssl.

傻比既视感 2024-11-23 08:50:01

感谢 Petey B,这个想法很有趣,所以帮助我找到了搜索方向。
解决办法是:

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

Thanks Petey B the idea was interesting so have help me to find direction to search.
The solution is:

$serial_number= strtoupper(dechex($serial_number));
゛时过境迁 2024-11-23 08:50:01
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,所以目前无法测试它。

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

That doesn't seem to work. Probably due to float conversion.

I saw one solution with bcmath.

Here's from 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);

I don't have bcmath enabled so I cant test it at the moment.

青春有你 2024-11-23 08:50:01

您可以使用 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
?>

You could use phpseclib for this purposes:

<?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
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文