gpt4 book ai didi

php - 尝试在 PHP 中使用 shell_exec 通过 openssl 创建证书

转载 作者:太空宇宙 更新时间:2023-11-03 12:58:01 25 4
gpt4 key购买 nike

我实际上在为自己做一个小项目,是一个创建证书签名请求的 Web 应用程序,还有证书 .pem/.crt 及其 .key .

实际问题是我正在尝试运行:

shell_exec(openssl ca -config ../openssl.cnf -in $CSR_FILE -out $CRT_FILE)

而且我发现问题是在运行此命令后要求我的 CA 密码,然后两次回答"is"以接受证书的创建。我不知道如何让它工作。我已经坚持了将近三天,Google 或 Stack Overflow 都没有答案。

我尝试运行该命令并添加另一个 shell_exec(passphrase),以这种方式两次传递密码和“y”。

shell_exec("openssl....","passphrase","y","y")

非常感谢,感谢所有帮助。

最佳答案

您不必为此使用 shell_exec()。您可以使用 openssl_csr_new() 创建自签名证书PHP 函数。

它根据dn提供的信息生成一个新的CSR(Certificate Signing Request),代表证书中要使用的Distinguished Name。

用于生成自签名证书的 PHP 代码

<?php 
// For SSL certificates, the commonName is usually the domain name of
// that will be using the certificate, but for S/MIME certificates,
// the commonName will be the name of the individual who will use the certificate.
$dn = array(
"countryName" => "UK",
"stateOrProvinceName" => "Somerset",
"localityName" => "Glastonbury",
"organizationName" => "The Brain Room Limited",
"organizationalUnitName" => "PHP Documentation Team",
"commonName" => "Wez Furlong",
"emailAddress" => "wez@example.com"
);

// Generate a new private (and public) key pair
$privkey = openssl_pkey_new();

// Generate a certificate signing request
$csr = openssl_csr_new($dn, $privkey);

// You will usually want to create a self-signed certificate at this
// point until your CA fulfills your request.
// This creates a self-signed cert that is valid for 365 days
$sscert = openssl_csr_sign($csr, null, $privkey, 365);

// Now you will want to preserve your private key, CSR and self-signed
// cert so that they can be installed into your web server.

openssl_csr_export($csr, $csrout) and var_dump($csrout);
openssl_x509_export($sscert, $certout) and var_dump($certout);
openssl_pkey_export($privkey, $pkeyout, "mypassword") and var_dump($pkeyout);

// Show any errors that occurred here
while (($e = openssl_error_string()) !== false) {
echo $e . "\n";
}
//save certificate and privatekey to file
file_put_contents("certificate.cer", $certout);
file_put_contents("privatekey.pem", $pkeyout);
?>

关于php - 尝试在 PHP 中使用 shell_exec 通过 openssl 创建证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30750734/

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