【问题标题】:2 ways SSL with CURL PHP not working2 种方式 SSL 与 CURL PHP 不工作
【发布时间】:2018-06-25 11:25:11
【问题描述】:

在 SOAPUI 中,我仅在 SSL 设置中导入 .pfx 文件,然后我测试 API 方法,它可以正常工作。现在,我想在我的 PHP 应用程序中实现 SOAPUI 中的相同工作示例。所以,我使用 CURL PHP 来管理它。我所做的基本上是,我将在 SOAP UI 中使用的相同证书导入到我的 PHP 文件中。我想使用我的私钥使用我的公钥访问受保护的服务器。

更新日期:2018 年 6 月 29 日

PFX 文件:test-cert.pfx

 $url = 'https://domain.com.ph/api/id_num';

    $headers = array(
                    "Content-Type: application/json",
                    "token really_long_numbers",
                    );

    $curl = curl_init();

curl_setopt ($curl, CURLOPT_URL, $url );        
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_PORT , 443);
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 2);
curl_setopt($curl, CURLOPT_SSLVERSION, 1);
curl_setopt($curl, CURLOPT_SSLCERT, "/Applications/XAMPP/xamppfiles/htdocs/d/client.pem");
curl_setopt($curl, CURLOPT_SSLKEY, "/Applications/XAMPP/xamppfiles/htdocs/d/key.pem");
curl_setopt($curl, CURLOPT_SSLKEYPASSWD, "******");
curl_setopt($curl, CURLOPT_CAINFO, "/Applications/XAMPP/xamppfiles/htdocs/d/cacert.pem");

我尝试使用以下命令从 PFX 生成 CA

openssl pkcs12 -in test-cert.pfx -out test-cert.pem -clcerts

与/或

openssl pkcs12 -in test-cert.pfx -out test-cert.pem -clcerts -nodes

现在是新错误

cURL 错误 #:SSL 证书问题:无法获取本地颁发者 证书

更新:2018 年 7 月 2 日

我已经在 php.ini 文件中添加了以下行,当然,还从 http://curl.haxx.se/ca/cacert.pem 下载了 cacert.pem 文件

[卷曲] ; CURLOPT_CAINFO 选项的默认值。这必须是一个 ;绝对路径。

curl.cainfo="/Applications/XAMPP/xamppfiles/etc/openssl/certs/cacert.pem"
openssl.cafile="/Applications/XAMPP/xamppfiles/etc/openssl/certs/cacert.pem"

我的php.ini配置文件所在位置

Configuration File (php.ini) Path: /etc
Loaded Configuration File:         /etc/php.ini
Scan for additional .ini files in: (none)
Additional .ini files parsed:      (none)

并在 curl 中添加以下行

CURLOPT_CAINFO => '/path/to/my/exported/cacert.pem'

CURLOPT_CAPATH => '/path/to/my/exported/cacert.pem'

我还在 apache 配置和 php.ini 中启用以下行 php_openssl.dllmod_ssl

最后,我也在cacert.pem 位置下载了 ca-bundle.crt。

还是不行。

【问题讨论】:

    标签: php ssl curl


    【解决方案1】:
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_PORT , 443);
    curl_setopt($curl, CURLOPT_VERBOSE, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 2);
    curl_setopt($curl, CURLOPT_SSLVERSION, 1);
    curl_setopt($curl, CURLOPT_SSLCERT, getcwd() . "/my_cert.pem");
    curl_setopt($curl, CURLOPT_SSLKEY, getcwd() . "/my_key.pem");
    curl_setopt($curl, CURLOPT_CAINFO, getcwd() . "/cacert.pem");
    

    您需要指定所有 3 个 - 您的私钥 (SSLKEY)、您的公共证书 (SSLCERT) 和证书颁发机构链 (CAINFO),直到根 CA。 密钥和证书应该是纯文本 PEM 格式而不是 PFX

    openssl pkcs12 -in test-cert.pfx -out test-cert.pem -nodes

    更新日期:2018 年 6 月 29 日

    http://curl.haxx.se/ca/cacert.pem(或同样包含RSA-1024证书的https://github.com/bagder/ca-bundle/blob/e9175fec5d0c4d42de24ed6d84a06d504d5e5a09/ca-bundle.crt)下载到/Applications/AMPPS/extra/etc/openssl/certs/cacert.pem 然后更新php.ini并重启Apache

    [curl]
    ; A default value for the CURLOPT_CAINFO option. This is required to be an
    ; absolute path.
    curl.cainfo="/Applications/AMPPS/extra/etc/openssl/certs/cacert.pem"
    openssl.cafile="/Applications/AMPPS/extra/etc/openssl/certs/cacert.pem"
    

    【讨论】:

    • 额外说明:链必须包含所有中间证书,而不仅仅是根 CA。
    • 实际上,它可能只包含中间证书 - 如果它是知名且受信任的权威机构(因此可以合理地期望远程端将其包含在其 CA 包中)。但这取决于具体情况。
    • 当然,你是对的,根 CA 很可能应该已经在本地证书存储中
    • @IVOGELOV 仔细按照您的指示进行操作后,它仍然没有工作。我尝试过的解决方案是 1. 我插入 CERT/Public 开始的那个 -----BEGIN CERTIFICATE----- 2. 我插入 SSLCERT/Private 那个开始的 -----BEGIN ENCRYPTED私钥----- 3. 我使用这个openssl pkcs12 -in test-cert.pfx -cacerts -nokeys -chain -out cacerts.crt 创建CA 证书并导入CAINFO。结果是unable to set private key file: dapskey.pem' type PEM 请查看上面的更新代码
    • 您确定 PEM 文件与脚本位于同一目录中吗?如果不是 - 然后使用每个文件的完整路径名而不是 getcwd()。还要确认ca.crt 是纯文本 PEM 格式。
    猜你喜欢
    • 2016-07-11
    • 1970-01-01
    • 2015-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-15
    相关资源
    最近更新 更多