Monday, November 18, 2024
Google search engine
HomeLanguagesPHP openssl_pkcs12_export() Function

PHP openssl_pkcs12_export() Function

The opensl_pkcs12_export() function is a built-in function in PHP which is used to store in a string called x509 in a PKCS # 12 file format. Certificate store file corresponding to PKCS # 12 variable.

Syntax:

bool openssl_pkcs12_export( mixed $x509, string &$out, 
        mixed $priv_key, string $pass [, array $args] )

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

  • $x509: This parameter is a standard defining the format of public key certificate.
  • $out: This is the variable passed by reference which holds PKCS#12 on successful execution of the above function.
  • $priv_key: This parameter is used for the private key that matches the dessert “priv_key”. The private key is a component of the PKCS # 12 file.
  • $pass: This parameter is used for encryption password for unlocking the PKCS#12 file.
  • $args: The “$args” stands for arguments and variables are values representing something else.

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

Below program illustrates the openssl_pkcs12_export() function in PHP:

Program:

PHP




<?php
  
$dn = array(
    "countryName" => 'xx',
    "stateOrProvinceName" => 'uttar prradesh',
    "localityName" => 'varanasi',
    "organizationName" => 'neveropen',
    "organizationalUnitName" => 'neveropen team',
    "commonName" => 'people',
    "emailAddress" => 'user@neveropen.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 to read the pkcs12
// certificate and store into array
openssl_pkcs12_read ( $certificateOutput
    $readableOutput$privateKeyPass );
      
var_dump(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php

RELATED ARTICLES

Most Popular

Recent Comments