无法设置私钥文件:'./ cert.pem'类型为PEM

use*_*372 23 curl openssl

我使用curl使用公共证书文件从https站点下载数据.

系统信息:

  • 操作系统: fedora 14
  • 卷曲:卷曲7.30.0
  • openssl: OpenSSL 1.0.0a-fips

命令是,

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
Run Code Online (Sandbox Code Playgroud)

我已经给了该.pem文件的所有权限,仍然卷曲是一个错误.

dop*_*ime 19

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

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

此外,此文档提到注意,此选项假定"证书"文件是私钥并且私有证书连接在一起!

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

你可以在这里得到更多的帮助.

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


Pet*_*son 9

当我使用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
Run Code Online (Sandbox Code Playgroud)

  • 伙计,你使用了 --insecure 这意味着你实际上并没有使用 .pem :| (4认同)

cur*_*e85 9

我遇到了同样的问题,最终我找到了一个无需拆分文件的解决方案,按照 Petter Ivarrson 的回答

我的问题是将 .p12 证书转换为 .pem 时。我用了:

openssl pkcs12 -in cert.p12 -out cert.pem
Run Code Online (Sandbox Code Playgroud)

这会将所有证书 (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
Run Code Online (Sandbox Code Playgroud)

这显示了不同的哈希值,这就是 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
Run Code Online (Sandbox Code Playgroud)

哈希匹配!!!

所以我可以通过运行来发出 curl 请求

curl -ivk --cert ./cert.pem:KeyChoosenByMeWhenIrunOpenSSL https://thesite.com
Run Code Online (Sandbox Code Playgroud)

没有问题!!!

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

curl --insecure --key key.pem --cacert ca.pem --cert client.pem:KeyChoosenByMeWhenIrunOpenSSL https://thesite.com
Run Code Online (Sandbox Code Playgroud)