关于openssl:如何使用PHP将PFX转换为CRT和PEM?

How to convert PFX to CRT and PEM using PHP?

如何使用PHP OpenSSL函数将.pfx(PKCS12或.p12)证书转换为.crt和.pem,因此避免使用公用服务器上不允许使用的命令行工具。


1
2
3
4
5
6
7
8
9
10
11
12
<?php
$res = [];
$openSSL = openssl_pkcs12_read($pkcs12, $res, $cert_password);
if(!$openSSL) {
    throw new ClientException("Error:".openssl_error_string());
}
// this is the CER FILE
file_put_contents('CERT.cer', $res['pkey'].$res['cert'].implode('', $res['extracerts']));

// this is the PEM FILE
$cert = $res['cert'].implode('', $res['extracerts']);
file_put_contents('KEY.pem', $cert);