【问题标题】:Trying to create a certificate through openssl using shell_exec in PHP尝试在 PHP 中使用 shell_exec 通过 openssl 创建证书
【发布时间】:2015-06-10 08:13:03
【问题描述】:

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

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

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

我发现问题是在运行此命令后要求输入我的 CA 密码,然后回答“是”两次以接受证书的创建。我无法弄清楚如何使它工作。我已经坚持了将近三天,Google 或 Stack Overflow 都没有答案。

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

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

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

【问题讨论】:

  • 我要做的第一件事是将../openssl.cnf 更改为完整路径名-也许PHP的当前命令目录与脚本的位置无关?
  • 另外,我猜你需要提供控制台参数,这样就不需要键盘输入。这个控制台命令有可能吗?
  • (顺便说一句,您可能可以直接使用 php.net/openssl 来做您想做的事情 - 也许这样会更容易?)。
  • 实际上我正在使用 php.net/openssl 但我发现的问题是没有将证书添加到 openssl 数据库,以便我可以撤销。感谢您的帮助

标签: php ssl openssl shell-exec


【解决方案1】:

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

它根据 dn 提供的信息生成一个新的 CSR(证书签名请求),它代表证书中要使用的专有名称。

生成自签名的 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);
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-27
    • 2023-04-09
    • 2016-10-07
    • 1970-01-01
    • 1970-01-01
    • 2019-04-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多