卷曲 - 无法使用p12证书连接

ex3*_*x3v 15 php ssl curl

我正在尝试使用Curl连接到某些外部公司提供的服务来收集一些数据.除了解决问题之外,他们还向我发送了p12建立连接所需的证书文件.

当我尝试使用它时curl,我收到以下错误:

#58: not supported file type 'P12' for certificate
Run Code Online (Sandbox Code Playgroud)

到目前为止,我已经尝试更新curl和php-curl.没有改变.

我的代码:

...
curl_setopt($ch, CURLOPT_SSLCERT, 'cert_path');
curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'P12');
curl_setopt($ch, CURLOPT_SSLCERTPASSWD, 'my_pass');
...
Run Code Online (Sandbox Code Playgroud)

有趣的是,这段代码适用于我们的生产环境,而它不适用于我的本地机器(Linux Mint 16).

ex3*_*x3v 28

找到了解决方案.

最简单的方法是.pem.p12文件中提取密钥和证书.

例如(测试linux):

openssl pkcs12 -in file.p12 -out file.key.pem -nocerts -nodes
openssl pkcs12 -in file.p12 -out file.crt.pem -clcerts -nokeys
Run Code Online (Sandbox Code Playgroud)

将在当前目录中创建密钥/证书对.

现在,使用它:

curl_setopt($ch, CURLOPT_SSLCERT, 'file.crt.pem');
curl_setopt($ch, CURLOPT_SSLKEY, 'file.key.pem');
curl_setopt($ch, CURLOPT_SSLCERTPASSWD, 'pass');
curl_setopt($ch, CURLOPT_SSLKEYPASSWD, 'pass');
Run Code Online (Sandbox Code Playgroud)

pass来自.p12文件的密码在哪里.

  • 这个答案并不完全正确。您不需要2个单独的文件。您的命令不会在文件上创建“通过”,因此它们不会被加密。只需使用-clcerts`选项执行1个文件。 (2认同)
  • @SteveExdia如果我没记错的话,这就是你需要的:`openssl pkcs12 -in file.p12 -out file.crt.pem -clcerts`。这是虚拟代码,解释我如何使用 php 做到这一点(或多或少):http://sandbox.onlinephpfunctions.com/code/41487980de8fd47316bd8a38a84494e47990024c (2认同)