首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法设置私钥文件:'./cert.pem‘类型为PEM

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

Stack Overflow用户
提问于 2013-05-18 13:18:49
回答 5查看 128.6K关注 0票数 42

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

系统信息:

  • 操作系统: fedora 14
  • curl : curl 7.30.0
  • OpenSSL : OpenSSL 1.0.0a-fips

命令是,

代码语言:javascript
运行
复制
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文件所有的权限,但还是会抛出一个错误。

EN

回答 5

Stack Overflow用户

发布于 2013-05-18 18:54:22

在读取了您使用的选项的cURL 文档之后,证书的私钥似乎不在同一个文件中。如果它位于不同的文件中,则需要使用--密钥文件并提供密码。

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

此外,本文档还提到,注意此选项假定了一个“证书”文件,即私钥和连接的私有证书!

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

您可以在这个这里上获得更多的帮助。

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

票数 31
EN

Stack Overflow用户

发布于 2017-06-30 13:13:36

当我使用Open时,我遇到了这个问题,解决方案是将cert拆分到3个文件中,并使用所有这些文件使用Curl进行调用:

代码语言:javascript
运行
复制
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
票数 13
EN

Stack Overflow用户

发布于 2020-05-08 13:31:02

我也遇到了同样的问题,最终我找到了一种不分裂文件的解决方案,方法是遵循Petter的回答

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

代码语言:javascript
运行
复制
openssl pkcs12 -in cert.p12 -out cert.pem

这会将所有证书(CA + CLIENT)与私钥一起转换并导出到一个文件中。

问题是当我试图通过运行验证证书和密钥的散列是否匹配时:

代码语言:javascript
运行
复制
// 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是列表中的第一位。

因此,解决方案是只将客户端证书与私钥一起导出:

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
**HASHES MATCHED !!!** 

So I was able to make a curl request by running

```javascript

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

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
票数 12
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16624704

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档