Серийный номер SSL-сертификата PHP в шестнадцатеричном формате


Мне нужно отобразить информацию о сертификате SSL о серийном номере. Когда я использую

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

Я получаю, как -5573199485241205751 Но когда я выполняю команду openssl x509 - в "mycert.crt" -нет -серийный Я получаю serial=B2A80498A3CEDC09 можно ли получить в PHP? Спасибо.

Author: Petey B, 2011-06-21

4 answers

Спасибо Пити Б. идея была интересной, так что помогите мне найти направление для поиска. Решение таково:

$serial_number= strtoupper(dechex($serial_number));
 2
Author: user710818, 2011-06-21 21:47:05

Вероятно, вы получаете серийный номер в виде массива байтов. Преобразуйте эти байты в шестнадцатеричные, и вы должны получить серийный номер, который вы видите, используя openssl.

 3
Author: Petey B, 2011-06-21 15:50:25
php > $value = hexdec('B2A80498A3CEDC09');
php > echo dechex($value);
b2a80498a3cee000

Похоже, это не работает. Вероятно, из-за преобразования с плавающей точкой.

Я видел одно решение с bcmath.

Вот из конфузы (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, поэтому в данный момент я не могу его протестировать.

 1
Author: loconut, 2011-10-10 01:49:08

Вы могли бы использовать 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
?>
 0
Author: Mikel Vysotsky, 2016-02-09 12:37:51