简体   繁体   English

卷曲失败,并显示错误#58:无法使用客户端证书(找不到密钥或密码短语错误?)

[英]Curl failed with error #58: unable to use client certificate (no key found or wrong pass phrase?)

I get this error : Fatal error: Curl failed with error #58: unable to use client certificate (no key found or wrong pass phrase?) 我收到此错误:致命错误:Curl失败,错误#58:无法使用客户端证书(找不到密钥或密码错误?)

I have a script that extracts the certificate information from a .p12 file. 我有一个脚本可以从.p12文件中提取证书信息。 I thought this was the problem to start, however I used this to paste the contents of my generated .pem file : https://www.sslshopper.com/certificate-decoder.html and it decodes/sees it all fine. 我以为这是开始的问题,但是我用它来粘贴生成的.pem文件的内容: https : //www.sslshopper.com/certificate-decoder.html ,它解码/显示一切正常。 So I assume the .pem is ok. 所以我认为.pem可以。

$ch = curl_init();
            curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
                  curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0); 
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_SSLCERT, 'cert.pem'); 
            curl_setopt($ch, CURLOPT_TIMEOUT, 10);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            // converting
            $response = curl_exec($ch); 
            // converting
            $response1 = str_replace("<soap:Body>","",$response);
            $response2 = str_replace("</soap:Body>","",$response1);
            if($response === false){
              throw new Exception(curl_error($ch), curl_errno($ch));
            }

The pem file is as such : pem文件是这样的:

-----BEGIN CERTIFICATE-----
<cert bla bla>
-----END CERTIFICATE-----
-----BEGIN PRIVATE KEY-----
<key bla bla>
-----END PRIVATE KEY-----
-----BEGIN CERTIFICATE-----
<cert bla bla>
-----END CERTIFICATE-----–

Any suggestions welcomed. 任何建议欢迎。

Thanks 谢谢

Firstly a couple of comments: 首先有几点评论:

  • By using these settings, you're opening yourself to potential MITM attacks : 通过使用这些设置, 您可以承受潜在的MITM攻击

     curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0); 

    You'd normally want VERIFYHOST=2 and VERIFYPEER=true (which should be the default anyway). 通常,您通常希望VERIFYHOST=2VERIFYPEER=true (无论如何应该是默认值)。 Doing the verification correctly also implies you're going to need to set up a CA cert or the server cert in CAINFO or CAPATH. 正确执行验证还意味着您将需要在CAINFO或CAPATH中设置CA证书或服务器证书。 I'll quote the PHP/curl documentation for this option here: 我将在此处引用此选项PHP / curl文档

    Alternate certificates to verify against can be specified with the CURLOPT_CAINFO option or a certificate directory can be specified with the CURLOPT_CAPATH option. 可以使用CURLOPT_CAINFO选项指定要验证的备用证书,或者可以使用CURLOPT_CAPATH选项指定证书目录。

  • Pasting your private key (which you get in the -----BEGIN PRIVATE KEY----- block) into a remote website you don't know much about isn't necessarily a good idea. 将您的私钥(您在-----BEGIN PRIVATE KEY-----私钥-----BEGIN PRIVATE KEY-----块中获得的)粘贴到您不太了解的远程网站中不一定是个好主意。 Your private key is meant to remain private. 您的私钥旨在保持私密性。 You can use openssl x509 and openssl rsa to check the formats are as you expect. 您可以使用openssl x509openssl rsa检查格式是否符合您的期望。

There are two options in libcurl : CURLOPT_SSLCERT and CURLOPT_SSLKEY , for the certificate and its private key, respectively. libcurl有两个选项: CURLOPT_SSLCERTCURLOPT_SSLKEY ,分别用于证书及其私钥。 I must admit I haven't tried the PHP cURL bindings recently, and some tools will indeed let you put both cert and private key in the same file (like you've done), but while some comments in the PHP documentation suggest you can do this, this isn't really mentioned in libcurl 's documentation (you're not using the P12 format here). 我必须承认我最近没有尝试过PHP cURL绑定,有些工具的确可以让您将cert和private key都放在同一个文件中(就像您所做的一样),但是PHP文档中的一些注释建议您可以这样做, libcurl的文档中并未真正提到(您在这里没有使用P12格式)。 In addition, libcurl itself doesn't document CURLOPT_SSLCERTPASSWD (which would make sense for a password-protected key in the cert file), so there might be some slight difference in the PHP bindings and libcurl itself (you'll find most other options are almost a direct mapping). 另外,libcurl本身没有记录CURLOPT_SSLCERTPASSWD (这对于cert文件中受密码保护的密钥是有意义的),因此PHP绑定和libcurl本身可能会有一些细微差别(您会发现其他大多数选项是几乎是直接映射)。

I'd suggest moving what's between -----BEGIN/END PRIVATE KEY----- into a separate file and pointing CURLOPT_SSLKEY to that file path, and keeping the certificate (and presumably its chain, the following certs) with the current settings. 我建议将-----BEGIN/END PRIVATE KEY-----之间的内容移动到一个单独的文件中,并将CURLOPT_SSLKEY指向该文件路径,并将证书(以及大概的证书链,以下证书)与当前设置。 Make sure the certificate chain is in the right order, in particular make sure that the first one is your client cert (for which you have the private key). 确保证书链的顺序正确,尤其要确保第一个是您的客户证书(您具有私钥)。 You can check the content of each -----BEGIN/END CERTIFICATE----- block by pasting it into the standard input of openssl x509 -text -noout . 您可以通过将每个-----BEGIN/END CERTIFICATE-----块的内容粘贴到openssl x509 -text -noout的标准输入中来检查它们的内容。 Each of these blocks will give you a subject and issuer. 这些模块中的每一个都会为您提供主题和发行者。 A certificate with Subject X and Issuer Y should be followed by a certificate with Subject Y and Issuer Z (and so on). 带有主题X和颁发者Y的证书之后应带有主题Y和颁发者Z的证书(依此类推)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 cURL无法使用客户端证书(找不到密钥或错误的密码?) - cURL unable to use client certificate (no key found or wrong pass phrase?) PHP CURL错误:无法使用客户端证书(找不到密钥或密码短语错误?) - PHP CURL error: unable to use client certificate (no key found or wrong pass phrase?) 无法使用客户端证书(找不到密钥或密码错误?) - unable to use client certificate(no key found or wrong pass phrase?) 贝宝(PHP):无法使用客户端证书(找不到密钥或密码错误?) - Paypal(PHP): unable to use client certificate (no key found or wrong pass phrase?) cURL 58错误无法加载客户端密钥-8178 - cURL 58 error Unable to load client key -8178 cURL 错误 (58): 无法设置私钥文件: &#39;/var/www/work/xml/keys/client.pem&#39; 类型 PEM - cURL Error (58): unable to set private key file: '/var/www/work/xml/keys/client.pem' type PEM cURL无法在本地服务器中使用客户端证书 - cURL is unable to use client certificate , in local server cURL错误58:SSL:无法加载证书“...”及其私钥:Mac上的OSStatus -25299 - cURL error 58: SSL: Can't load the certificate “…” and its private key: OSStatus -25299 on Mac Curl 失败:NSS:未找到客户端证书(未指定昵称)-在 Centos 7 - Curl failed: NSS: client certificate not found (nickname not specified) - On Centos 7 cURL 与 SSL 证书失败:错误 58 无法设置私钥文件 - cURL with SSL certificates fails: error 58 unable to set private key file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM