我使用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
Run Code Online (Sandbox Code Playgroud)
我已经给了该.pem
文件的所有权限,仍然卷曲是一个错误.
当我使用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)
我遇到了同样的问题,最终我找到了一个无需拆分文件的解决方案,按照 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)
归档时间: |
|
查看次数: |
77760 次 |
最近记录: |