略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: openssl_pkcs12_export

2024-04-29

openssl_pkcs12_export

(PHP 5 >= 5.2.2, PHP 7, PHP 8)

openssl_pkcs12_export将 PKCS#12 兼容证书存储文件导出到变量

说明

openssl_pkcs12_export(
    mixed $x509,
    string &$out,
    mixed $priv_key,
    string $pass,
    array $args = ?
): bool

openssl_pkcs12_export() 以 PKCS#12 文件格式 将 x509 导入到以out命名类型为字符串的变量中。

参数

x509

参见密钥/证书参数以获取有效值列表。

out

成功,该字符串将为 PKCS#12 格式。

priv_key

PKCS#12 文件的私钥部分file, 参见 公/私钥参数 获取更多可用列表。

pass

用来解锁 PKCS#12 文件的解密密码。

args

可选数组,其他主键将被忽略。

Key 说明
"extracerts" PKCS#12 文件中包含的额外证书或单个证书的数组。
"friendlyname" 被证书和密钥使用的字符串

返回值

成功时返回 true, 或者在失败时返回 false

add a noteadd a note

User Contributed Notes 5 notes

up
1
Robert
8 years ago
If you need to provide multiple additional certificates, the 'extracerts' argument needs to be an array with one certificate per element:
<?php
$args
= array(
   
'extracerts' => array(
       
0 => '-----BEGIN CERTIFICATE----- cert1 ...',
       
1 => '-----BEGIN CERTIFICATE----- cert2 ...',
       
// ...
       
)
    );
?>

You can use this to prepare a PEM.

<?php
$pemChain
= '...';
preg_match_all('/(-----BEGIN CERTIFICATE-----.*?-----END CERTIFICATE-----)/si', $pemChain, $matches);
$args = array('extracerts' => $matches[0]);
openssl_pkcs12_export($signed_csr, $cerificate_out, $private_key_resource, $passphrase, $args);
?>
up
2
simoncpu was here
12 years ago
If your certificate is not password-protected, just use null or a blank string.  Otherwise, this function won't work.
up
1
Anonymous
8 years ago
If you want to include CA-Certificates in the PKCS12 it can be accomplished by using the $args parameter.
<?php
$args
= array(
              
'extracerts' => $CAcert,
              
'friendly_name' => 'My signed cert by CA certificate'
             
);
openssl_pkcs12_export($signed_csr, $cerificate_out, $private_key_resource, $passphrase, $args);
?>
up
-1
ismael at privasy dot org
8 years ago
in order to export a private key to pkcs12 format, the input certificate must contain both private and associated public key in PEM format , 

-----BEGIN RSA PRIVATE KEY-----
-----END RSA PRIVATE KEY-----

-----BEGIN CERTIFICATE-----
-----END CERTIFICATE-----

else this function might return the following error "openssl_pkcs12_export(): cannot get cert from parameter 1"
up
-7
mryom
11 years ago
Example:

<?php
$key
= openssl_pkey_get_private(Private_Key, Password);

openssl_pkcs12_export(Certificate, $iis, $key, Password);
?>

官方地址:https://www.php.net/manual/en/function.openssl-pkcs12-export.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3