40

I am using curl to download data from a https site using public certificate files.

System information:

  • OS: fedora 14
  • curl: curl 7.30.0
  • openssl: OpenSSL 1.0.0a-fips

The command is,

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

I have have given all the permission to the .pem file, still curl is throwing an error.

4

5 回答 5

29

在阅读了有关您使用的选项的 cURL文档后,证书的私钥似乎不在同一个文件中。如果它在不同的文件中,您需要使用 --key 文件提及它并提供密码。

因此,请确保 cert.pem 具有私钥(连同证书)或使用 --key 选项提供它。

此外,本文档提到请注意,此选项假定“证书”文件是私钥和私人证书连接!

它们是如何连接的?这很容易。将它们一个接一个地放在同一个文件中。

您可以在此处获得更多帮助。

我相信这可能会对你有所帮助。

于 2013-05-18T18:54:22.587 回答
12

我在使用 Open SSL 时遇到了这个问题,解决方案是将证书拆分为 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
于 2017-06-30T13:13:36.763 回答
11

我遇到了同样的问题,最终我找到了一个无需拆分文件即可工作的解决方案,遵循 Petter Ivarrson 的回答

我的问题是在将 .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 证书而不是 CLIENT 证书。因为 CA 在列表中排名第一。

所以解决方案是只导出 CLIENT 证书和私钥:

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 | openssl md5

哈希匹配!!!

所以我能够通过运行发出 curl 请求

curl -ivk --cert ./cert.pem:KeyChoosenByMeWhenIrunOpenSSL https://thesite.com

没有什么问题!!!

话虽如此......我认为最好的解决方案是将证书拆分为单独的文件并像 Petter Ivarsson 所写的那样单独使用它们:

curl --insecure --key key.pem --cacert ca.pem --cert client.pem:KeyChoosenByMeWhenIrunOpenSSL https://thesite.com
于 2020-05-08T13:31:02.177 回答
3

我有类似的情况,但我在不同的文件中使用密钥和证书。

在我的情况下,您可以通过比较哈希来检查密钥和锁的匹配(请参阅https://michaelheap.com/curl-58-unable-to-set-private-key-file-server-key-type-佩姆/)。这有助于我识别不一致之处。

于 2019-05-08T13:17:46.813 回答
1

我不确定这是否会对任何人有所帮助,但我遇到了这个错误(尽管我使用 php 而不是命令行来创建它)并修复它我必须确保目录中没有旧的 .key 或 .pem 文件我在看。通过删除它们并使用身份验证制作新文件,它可以完美运行!

于 2017-06-08T15:05:35.003 回答