Umair Khan
Umair Khan

Reputation: 1752

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:

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.

Upvotes: 3

Views: 2144

Answers (1)

Pyton
Pyton

Reputation: 1319

#1. Version

About version number (From RFC: https://www.rfc-editor.org/rfc/rfc5280#section-4.1.2.1)

4.1.2.1. Version

This field describes the version of the encoded certificate. When extensions are used, as expected in this profile, version MUST be 3 (value is 2). If no extensions are present, but a UniqueIdentifier is present, the version SHOULD be 2 (value is 1); however, the version MAY be 3. If only basic fields are present, the version SHOULD be 1 (the value is omitted from the certificate as the default value); however, the version MAY be 2 or 3.

Implementations SHOULD be prepared to accept any version certificate. At a minimum, conforming implementations MUST recognize version 3 certificates.

Generation of version 2 certificates is not expected by implementations based on this profile.

It means version 2 in output equals version 3 of certyficate.

#2. OID About the first part of question, the easiest way is to find list and implement it as an array then normal replace. One of them i found on https://www.rfc-editor.org/rfc/rfc7427 (search for oid = )

Also in previous one RFC 7427 (https://www.rfc-editor.org/rfc/rfc5280) You can find those OID

Upvotes: 2

Related Questions