Skip to content
Related Articles

Related Articles

Improve Article

PHP openssl_pkcs12_read() Function

  • Last Updated : 23 Sep, 2020

The openssl_pkcs12_read() function is a built-in function in PHP and is used by the PKCS # 12 certificate store to convert it into an array provided by pkcs12. A PKCS #12 file may be encrypted and signed.

Syntax:

bool openssl_pkcs12_read( string $pkcs12, array &$certs, string $pass )

Parameters: This function accepts three parameters as mentioned above and described below:

  • $pkcs12: PKCS #12 is one of the family of standards called Public-Key Cryptography Standards (PKCS) published by RSA Laboratories.
  • $certs: If successful, this will contain the Certificate Store Information.
  • $pass: This parameter is used for the encryption password for unlocking the PKCS#12 file.

Return Values: This function returns TRUE on success or FALSE on failure.

Example:

PHP




<?php
  
$dn = array(
    "countryName" => 'xx',
    "stateOrProvinceName" => 'uttar prradesh',
    "localityName" => 'varanasi',
    "organizationName" => 'geeksforgeeks',
    "organizationalUnitName" => 'geeks team',
    "commonName" => 'people',
    "emailAddress" => 'user@geeks.com'
);
   
$privateKeyPass = 'dummyPassword';
$numberOfDays   = 108;
   
$privateKey = openssl_pkey_new();
$csr = openssl_csr_new($dn, $privateKey);
   
// Create a csr file, change null to
// a filename to save 
$sscert = openssl_csr_sign($csr
    null, $privateKey, $numberOfDays);
   
// On success $publicKey will hold
// the PEM content 
openssl_x509_export($sscert, $publicKey);
   
// Export the privateKey as a PEM content
openssl_pkey_export($privateKey
        $privateKey, $privateKeyPass);
   
   
// Parses the $privateKey and used by
// openssl_pkcs12_export_to_file.
$key = openssl_pkey_get_private(
        $privateKey, $privateKeyPass);
   
$certificateOutput = null;
  
// Save the pfx file to $certificateOutput
openssl_pkcs12_export($sscert
    $certificateOutput, $key, $privateKeyPass);
   
// openssl_pkcs12_read reads the pkcs12
// certificate and store into array
openssl_pkcs12_read ($certificateOutput
        $readableOutput, $privateKeyPass );
          
var_dump($readableOutput);
  
?>

Output:

array(2) {
 ["cert"]=>
  string(1444) "-----BEGIN CERTIFICATE-----
MIID/DCCAuSgAwIBAgIBADANBgkqhkiG9w0BAQU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