Friday 22 February 2019

How to get SSL Certificate Hash Algorithm OID using php's openssl_x509_parse

I am using php's OpenSSL Functions ( openssl_x509_parse ) to parse SSL certificate info of given site. Sample Code:

$stream = stream_context_create(
    array(
        "ssl" => array(
            "allow_self_signed" => true,
            "capture_peer_cert" => true,
            "capture_peer_cert_chain" => true,
            "verify_peer" => false,
            "verify_peer_name" => false,
            "sni_enabled" => true,
        ),
    )
);
$streamRead = @stream_socket_client("ssl://stackoverflow.com:443", $streamErrNo, $streamErrStr, 30, STREAM_CLIENT_CONNECT, $stream);
if (!$streamErrNo && $streamRead) {
    $streamContext = stream_context_get_params($streamRead);
    $streamContextMeta = stream_get_meta_data($streamRead);
    $certChainsRes = $streamContext["options"]["ssl"]["peer_certificate_chain"];
    $certChainArr = array();
    for ($i = 0; $i < count($certChainsRes); $i++) {
        $certChainData = openssl_x509_parse($certChainsRes[$i]);
        var_dump($certChainData);
    }
}

The code is working fine and it gives me data. Sample Data.

array (size=16)
  'name' => string '/C=US/ST=NY/L=New York/O=Stack Exchange, Inc./CN=*.stackexchange.com' (length=68)
  'subject' => 
    array (size=5)
      'C' => string 'US' (length=2)
      'ST' => string 'NY' (length=2)
      'L' => string 'New York' (length=8)
      'O' => string 'Stack Exchange, Inc.' (length=20)
      'CN' => string '*.stackexchange.com' (length=19)
  'hash' => string '07cc7bb0' (length=8)
  'issuer' => 
    array (size=4)
      'C' => string 'US' (length=2)
      'O' => string 'DigiCert Inc' (length=12)
      'OU' => string 'www.digicert.com' (length=16)
      'CN' => string 'DigiCert SHA2 High Assurance Server CA' (length=38)
  'version' => int 2
  'serialNumber' => string '9833040086282421696121167723365753840' (length=37)
  'serialNumberHex' => string '0765C64E74E591D68039CA2A847563F0' (length=32)
  'validFrom' => string '181005000000Z' (length=13)
  'validTo' => string '190814120000Z' (length=13)
  'validFrom_time_t' => int 1538697600
  'validTo_time_t' => int 1565784000
  'signatureTypeSN' => string 'RSA-SHA256' (length=10)
  'signatureTypeLN' => string 'sha256WithRSAEncryption' (length=23)
  'signatureTypeNID' => int 668

Questions:

  • 1: I get details like signatureTypeSN, signatureTypeLN,
    signatureTypeNID but how can I get Signature Algorithm Id (Hash
    Algorithm OID) eg. sha256WithRSAEncryption => 1.2.840.113549.1.1.11

  • 2: These details show that certificate's version is 2 'version' => int 2 but the browser shows that certificate version is 3 ![Screenshot of SSL Certificate Details in browser]1

P.S: The code is running on Ubuntu 18.04 server with php7.3. The browser I am using is Mozilla Firefox on Windows 10.



from How to get SSL Certificate Hash Algorithm OID using php's openssl_x509_parse

No comments:

Post a Comment