我使用curl使用公共证书文件从https站点下载数据。
系统信息:
命令是,
curl -v "https://<ip:<port>" --cert "./cert.pem" --cacert "./cacert.pem" --cert-type PEM
* About to connect() to kng.com port 443 (#0)
* Trying 11.19.37.123...
* Adding handle: conn: 0x8189e68
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x8189e68) send_pipe: 1, recv_pipe: 0
* Connected to fkng.com (11.19.37.123) port 443 (#0)
* unable to set private key file: './cert.pem' type PEM
* Closing connection 0
curl: (58) unable to set private key file: './cert.pem' type PEM
我已经给了.pem
文件所有的权限,但还是会抛出一个错误。
发布于 2013-05-18 18:54:22
发布于 2017-06-30 13:13:36
当我使用Open时,我遇到了这个问题,解决方案是将cert拆分到3个文件中,并使用所有这些文件使用Curl进行调用:
openssl pkcs12 -in mycert.p12 -out ca.pem -cacerts -nokeys
openssl pkcs12 -in mycert.p12 -out client.pem -clcerts -nokeys
openssl pkcs12 -in mycert.p12 -out key.pem -nocerts
curl --insecure --key key.pem --cacert ca.pem --cert client.pem:KeyChoosenByMeWhenIrunOpenSSL https://thesite
发布于 2020-05-08 13:31:02
我也遇到了同样的问题,最终我找到了一种不分裂文件的解决方案,方法是遵循Petter的回答
我的问题是当将.p12证书转换为.pem时。我用:
openssl pkcs12 -in cert.p12 -out cert.pem
这会将所有证书(CA + CLIENT)与私钥一起转换并导出到一个文件中。
问题是当我试图通过运行验证证书和密钥的散列是否匹配时:
// Get certificate HASH
openssl x509 -noout -modulus -in cert.pem | openssl md5
// Get private key HASH
openssl rsa -noout -modulus -in cert.pem | openssl md5
这显示了不同的散列,这就是CURL失败的原因。见此处:https://michaelheap.com/curl-58-unable-to-set-private-key-file-server-key-type-pem/
我猜这是因为所有证书都在一个文件(CA + CLIENT)中,CURL使用CA证书而不是客户端证书。因为CA是列表中的第一位。
因此,解决方案是只将客户端证书与私钥一起导出:
openssl pkcs12 -in cert.p12 -out cert.pem -clcerts
``
Now when I re-run the verification:
```sh
openssl x509 -noout -modulus -in cert.pem openssl md5
openssl rsa -noout -modulus -in cert.pem cert.pem openssl md5
**HASHES MATCHED !!!**
So I was able to make a curl request by running
```javascript
curl -ivk -cert ./cert.pem:KeyChoosenByMeWhenIrunOpenSSL https://thesite.com
without problems!!!
That being said... I think the best solution is to split the certificates into separate file and use them separately like Petter Ivarsson wrote:
```javascript
卷曲-不安全-键key.pem - ca.pem -cert client.pem:KeyChoosenByMeWhenIrunOpenSSL https://thesite.com
https://stackoverflow.com/questions/16624704
复制相似问题