Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the signature algorithm out of a certificate?

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.

enter image description here

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

like image 336
Mike Avatar asked Mar 09 '12 01:03

Mike


People also ask

How do you calculate Signature Algorithm?

The Signature Algorithm can be checked in the General Information menu: Also, you can scroll the page down and view the certificate information indicates the Signature Algorithm of the certificate along with other information in the Raw OpenSSL Data window.

How can I get certificate algorithm?

In the “Certificate” dialog, click “Details” and select “Signature hash algorithm” and lookout for the value. Click Security tab and “View Certificate” button. In the “Certificate Viewer” dialog, click “Certificate Signature Algorithm” under “Certificate Fields” and lookout for the value.

What is the Signature Algorithm in a certificate?

A signature algorithm is used to sign a piece of data and to calculate its hash with a certain hash function. Then the message is sent along with the hash and the name of the signature algorithm so that the recipient can calculate and compare the hash to make sure that the message was not altered during transmission.

How can I get my signature certificate?

View certificate details Open the file that contains the certificate you want to view. Click File > Info > View Signatures. In the list, on a signature name, click the down-arrow, and then click Signature Details. In the Signature Details dialog box, click View.


2 Answers

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.

like image 188
Martin Avatar answered Sep 30 '22 14:09

Martin


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');
}
like image 40
HomeCoder Avatar answered Sep 30 '22 13:09

HomeCoder