Skip to content

Instantly share code, notes, and snippets.

@snoj
Created February 12, 2016 15:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save snoj/d01f4a225f2a28caa15b to your computer and use it in GitHub Desktop.
Save snoj/d01f4a225f2a28caa15b to your computer and use it in GitHub Desktop.
Extract certificates and key from PFX.
<?php
//Requires php with the openssl module.
//This is useful for instances where installing openssl isn't an option
// but PHP is available, like on Windows servers using WAMP.
//use php extractpfx.php /path/tocert.pfx "pass phrase"
//http://php.net/manual/en/function.openssl-pkcs12-read.php
if(file_exists($argv[1])) {
$rawpfx = file_get_contents($argv[1]);
$crts = array();
$parsed = openssl_pkcs12_read($rawpfx, $crts, (isset($argv[2])) ? $argv[2] : "");
echo $crts["cert"];
if(isset($crts["pkey"]))
echo $crts["pkey"];
if(isset($crts["extracerts"])) {
foreach($crts["extracerts"] as $c) {
echo $c;
}
}
} else {
error_log("File doesn't exist");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment