gpt4 book ai didi

php - 在 PHP 中使用 openssl_pkcs12_export 导出链

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:36:23 28 4
gpt4 key购买 nike

是否可以使用 PHP 的 openssl_pkcs12_export() 将证书和私钥与证书链(根证书和/或中间证书)一起导出到 .pfx

更新:我查看了 php openssl 扩展的源代码,发现 openssl_pkcs12_export() 支持文档中的 2 个参数,friendly_name >提取物。这来自 ext/openssl/openssl.c,查看第 1914-1920 行 (PHP-5.4.0):

1878 /* {{{ proto bool openssl_pkcs12_export(mixed x509, string &out, mixed priv_key, string pass[, array args])
1879 Creates and exports a PKCS12 to a var */
1880 PHP_FUNCTION(openssl_pkcs12_export)
1881 {
1882 X509 * cert = NULL;
1883 BIO * bio_out;
1884 PKCS12 * p12 = NULL;
1885 zval * zcert = NULL, *zout = NULL, *zpkey, *args = NULL;
1886 EVP_PKEY *priv_key = NULL;
1887 long certresource, keyresource;
1888 char * pass;
1889 int pass_len;
1890 char * friendly_name = NULL;
1891 zval ** item;
1892 STACK_OF(X509) *ca = NULL;
1893
1894 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zzzs|a", &zcert, &zout, &zpkey, &pass, &pass_len, &args) == FAILURE)
1895 return;
1896
1897 RETVAL_FALSE;
1898
1899 cert = php_openssl_x509_from_zval(&zcert, 0, &certresource TSRMLS_CC);
1900 if (cert == NULL) {
1901 php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot get cert from parameter 1");
1902 return;
1903 }
1904 priv_key = php_openssl_evp_from_zval(&zpkey, 0, "", 1, &keyresource TSRMLS_CC);
1905 if (priv_key == NULL) {
1906 php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot get private key from parameter 3");
1907 goto cleanup;
1908 }
1909 if (cert && !X509_check_private_key(cert, priv_key)) {
1910 php_error_docref(NULL TSRMLS_CC, E_WARNING, "private key does not correspond to cert");
1911 goto cleanup;
1912 }
1913
1914 /* parse extra config from args array, promote this to an extra function */
1915 if (args && zend_hash_find(Z_ARRVAL_P(args), "friendly_name", sizeof("friendly_name"), (void**)&item) == SUCCESS)
1916 friendly_name = Z_STRVAL_PP(item);
1917
1918 if (args && zend_hash_find(Z_ARRVAL_P(args), "extracerts", sizeof("extracerts"), (void**)&item) == SUCCESS)
1919 ca = php_array_to_X509_sk(item TSRMLS_CC);
1920 /* end parse extra config */
1921
1922 p12 = PKCS12_create(pass, friendly_name, priv_key, cert, ca, 0, 0, 0, 0, 0);
1923
1924 bio_out = BIO_new(BIO_s_mem());
1925 if (i2d_PKCS12_bio(bio_out, p12)) {
1926 BUF_MEM *bio_buf;
1927
1928 zval_dtor(zout);
1929 BIO_get_mem_ptr(bio_out, &bio_buf);
1930 ZVAL_STRINGL(zout, bio_buf->data, bio_buf->length, 1);
1931
1932 RETVAL_TRUE;
1933 }
1934
1935 BIO_free(bio_out);
1936 PKCS12_free(p12);
1937 php_sk_X509_free(ca);
1938
1939 cleanup:
1940
1941 if (keyresource == -1 && priv_key) {
1942 EVP_PKEY_free(priv_key);
1943 }
1944 if (certresource == -1 && cert) {
1945 X509_free(cert);
1946 }
1947 }
1948 /* }}} */

但是,我不太确定如何将附加证书作为参数传递...有什么线索吗?

如果没有行号更容易阅读,请告诉我

最佳答案

这是 a bug that has been brought up将近两个月前。

谢天谢地,他为文档提供了一个示例补丁:

$args = array(
'extracerts' => $CAcert,
'friendly_name' => 'My signed cert by CA certificate'
);
openssl_pkcs12_export($signed_csr, $cerificate_out, $private_key_resource, $passphrase, $args);

什么是$CAcert?在内部它被传递给一个函数 takes an array and turns it into a x509并且该函数还检测它是证书数组还是单个证书。如果传递的是数组,每个元素都应该是 x509 资源;如果传递的不是数组,则 $CAcert 应该是单个资源。 openssl_x509_read 可能是您想在此处使用的内容,因为它返回 $CAcert 中预期的 x509 资源类型。

有人说保持文档更新是 PHP 项目中最难的部分之一。如果您不擅长 C 并希望帮助 PHP 变得更好,那么这是一个很好的起点。

关于php - 在 PHP 中使用 openssl_pkcs12_export 导出链,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10027764/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com