How to get the signature algorithm out of a certificate?

心已入冬 提交于 2019-12-05 00:05:58

问题


I want to use the PHP function openssl_verify() to verify the signatures of different X.509 certificates.

I have all it needs (certificate, $data, $signature, $pub_key_id) except of the signature algorithm but which is stored in the certificate.

My simple question is: How can I extract signature algorithm from certificates?


回答1:


Look at this question, you can do it similar, try this:

private function GetCertSignatureAlgorithm($certSignatureBinary, $pubKeyResourceId)
{

if(false === openssl_public_decrypt($certSignatureBinary, $sigString, $pubKeyResourceId))
{
    return false;
}

if (empty($sigString) ||
    strlen($sigString) < 5)
{
    return false;
}

if (ord($sigString[0]) !== 0x30 ||
    ord($sigString[2]) !== 0x30 ||
    ord($sigString[4]) !== 0x06)
{
    return false;
}

$sigString  = substr($sigString, 4);
$len        = ord($sigString[1]);
$bytes      = 0;

if ($len & 0x80)
{
    $bytes = ($len & 0x7f);
    $len = 0;
    for ($i = 0; $i < $bytes; $i++)
    {
        $len = ($len << 8) | ord($sigString[$i + 2]);
    }
}

$oidData = substr($sigString, 2 + $bytes, $len);
$hashOid = floor(ord($oidData[0]) / 40) . '.' . ord($oidData[0]) % 40;

$value = 0;
for ($i = 1; $i < strlen($oidData); $i++)
{
    $value = $value << 7;
    $value = $value | (ord($oidData[$i]) & 0x7f);
    if (!(ord($oidData[$i]) & 0x80))
    {
        $hashOid .= '.' . $value;
        $value = 0;
    }
}

//www.iana.org/assignments/hash-function-text-names/hash-function-text-names.xml
//www.php.net/manual/en/openssl.signature-algos.php
switch($hashOid)
{
    case '1.2.840.113549.2.5':     return 'md5';
    case '1.3.14.3.2.26':          return 'sha1';
    case '2.16.840.1.101.3.4.2.1': return 'sha256';
    case '2.16.840.1.101.3.4.2.2': return 'sha384';
    case '2.16.840.1.101.3.4.2.3': return 'sha512';

    //not secure = not accepted
    //case '1.2.840.113549.2.2':     //'md2';
    //case '1.2.840.113549.2.4':     //'md4';
    //case '1.3.14.3.2.18':          //'sha';
}

throw new Exception('CertSignatureAlgorithm not found');
}



回答2:


How about this?

$cer = file_get_contents('certificate.cer');
$res = openssl_x509_read($cer);
openssl_x509_export($res, $out, FALSE);
$signature_algorithm = null;
if(preg_match('/^\s+Signature Algorithm:\s*(.*)\s*$/m', $out, $match)) $signature_algorithm = $match[1];
var_dump($signature_algorithm);

It produces the output:

string(21) "sha1WithRSAEncryption"

Which you would have to map to OPENSSL_ALGO_SHA1 yourself.




回答3:


Using phpseclib, a pure PHP X.509 parser...

<?php
include('File/X509.php');

$x509 = new File_X509();
$cert = $x509->loadX509(file_get_contents('sample.pem'));
echo $cert['signatureAlgorithm']['algorithm'];



回答4:


One way might be openssl x509 -text -noout < $certfile | grep "Signature Algorithm"



来源:https://stackoverflow.com/questions/9627870/how-to-get-the-signature-algorithm-out-of-a-certificate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!