cURL 58错误无法加载客户端密钥-8178

时间:2017-08-07 11:31:18

标签: php ssl curl

在这里输入代码我正在尝试建立第一个ssl连接但是我无法确定错误代码58的原因是什么无法加载。

我使用php curl来测试连接,这里的代码是使用

$port = "443"; 

        $cert = getcwd() . "/controllers/ssl/certificate.pem";
        $testxml = "testset/npsLv01.xml";

        $headers = array(
            'Content-Type: text/xml; charset="utf-8"',
            'Content-Length: '.strlen($testxml),
            'Accept: text/xml',
            'Cache-Control: no-cache',
            'Pragma: no-cache',
            'SOAPAction: "Send"'
        ); 

        try {
        $ch = curl_init($testurl);

        if (FALSE === $ch)
            throw new Exception('failed to initialize');

        //curl opties om ssl verbinding op te zetten
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSLVERSION, 6);
        curl_setopt($ch, CURLOPT_SSLCERT, $cert);
        curl_setopt($ch, CURLOPT_PORT, $port);


        //post data afhandeling
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $testxml);

        //headers voor het verwerken van de post
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

        //timeout settings
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);

        //trying stuff out
        curl_setopt($ch, CURLOPT_NOBODY, true);
        curl_setopt($ch, CURLOPT_CERTINFO, true);
        curl_setopt($ch, CURLOPT_VERBOSE, 1);

        //exec de request
        $data = curl_exec($ch);

        if (FALSE === $data)
            throw new Exception(curl_error($ch), curl_errno($ch));

        } catch(Exception $e) {

            echo 'error code = ';
            echo $e->getCode();
            echo " error message = ";
            echo $e->getMessage();
            die;

        }
        var_dump($data); die;

什么时候exec这个代码我得到以下错误: 错误代码= 58错误消息=无法加载客户端密钥-8178

-----BEGIN CERTIFICATE-----
MIIEwzCCA6ugAwIBAgIDAPQqMA0GCSqGSIb3DQEBCwUAMEcxCzAJBgNVBAYTAlVT
MRY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ZjWmhxgHsw3hUEv8zTvz5IBVV6s9cQ
DAP8m/0Ip4yM26eO8R5j3LMBL3+vV8M8SKeDaCGnL+enP/C1DPz1hNFTvA5yT2AM
QriYrRmIV9cE7Ie/fodOoyH5U/02mEiN1vi7SPIpyGTRzFRIU4uvt2UevykzKdkp
YEj4/5G8V1jlNS67abZZAgMBAAGjggEdMIIBGTAfBgNVHSMEGDAWgBTAephojYn7
qwVkDBF9qn1luMrMTjAdBgNVHQ4EFgQUw5zz/NNGCDS7zkZ/oHxb8+IIy1kwEgYD
VR0TAQH/BAgwBgEB/wIBADAOBgNVHQ8BAf8EBAMCAQYwNQYDVR0fBC4wLDAqoCig
JoYkaHR0cDovL2cuc3ltY2IuY29tL2NybHMvZ3RnbG9iYWwuY3JsMC4GCCsGAQUF
BwEBBCIwIDAeBggrBgEFBQcwAYYSaHR0cDovL2cuc3ltY2QuY29tMEwGA1UdIARF
MEMwQQYKYIZIAYb4RQEHNjAzMDEGCCsGAQUFBwIBFiVodHRwOi8vd3d3Lmdlb3Ry
dXN0LmNvbS9yZXNvdXJjZXMvY3BzMA0GCSqGSIb3DQEBCwUAA4IBAQCjWB7GQzKs
rC+TeLfqrlRARy1+eI1Q9vhmrNZPc9ZE768LzFvB9E+aj0l+YK/CJ8cW8fuTgZCp
fO9vfm5FlBaEvexJ8cQO9K8EWYOHDyw7l8NaEpt7BDV7o5UzCHuTcSJCs6nZb0+B
kvwHtnm8hEqddwnxxYny8LScVKoSew26T++TGezvfU5ho452nFnPjJSxhJf3GrkH
uLLGTxN5279PURt/aQ1RKsHWFf83UTRlUfQevjhq7A6rvz17OQV79PP7GqHQyH5O
ZI3NjGFVkP46yl0lD/gdo0p0Vk8aVUBwdSWmMy66S6VdU5oNMOGNX2Esr8zvsJmh
gP8L8mJMcCal

-----END CERTIFICATE-----

2 个答案:

答案 0 :(得分:1)

通过将CURLOPT_SSLCERT设置为/controllers/ssl/certificate.pem,您可以将此文件设置为客户端用于对服务器进行身份验证的证书。在这种情况下,证书需要附带与服务器中的公钥匹配的私钥。但是,它没有,这就是为什么你得到错误“无法加载客户端密钥”

在使用证书文件的内容编辑问题之后,看起来对文件的目的存在误解。该文件包含两个证书,其中第二个是用于公共名称为*.ijsselgemeenten.nl的服务器的叶证书,而第一个证书是颁发此证书RapidSSL SHA256 CA - G3的子CA的证书。我的猜测是,这些不应该用作客户端证书,但这是服务器证书,客户应该得到它。

在这种情况下:

  • CURLOPT_SSL_VERIFYPEER不应设置为false,因为应验证服务器
  • 不应设置
  • CURLOPT_SSLCERT,因为不应使用客户端证书进行身份验证
  • 如果服务器和本地CA存储都已正确设置,它现在应该可以正常工作。如果不是(即在服务器上缺少子CA),可能会尝试将CURLOPT_CAINFO设置为给定文件。

答案 1 :(得分:0)

今天我解决了相同的问题,并且想分享一个解决方案,这可能对某人有用。

我被发送了 certfile.p12 来发出ssl请求。我通过openssl实用程序将文件提取到 public.pem privatekey.pem 。 在我的本地计算机Mac OS和Windows上,代码运行良好。

// Works great on Mac OS and Windows
$data = [
    'field1' => 'demo1',
    'field2' => 'demo2',
    'field3' => 'demo3'
];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://some_api.com');
curl_setopt($ch, CURLOPT_PORT , 443);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSLCERT, getcwd() . '/cert/public.pem');
curl_setopt($ch, CURLOPT_SSLKEY, getcwd() . '/cert/privatekey.pem');
curl_setopt($ch, CURLOPT_SSLKEYPASSWD, 'HerePassphraseOfPrivateKey');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));

$response = curl_exec($ch);
curl_close($ch);

但是在CentOS 7上我开始出现错误“ cURL 58错误无法加载客户端密钥-8178 “ ,事实证明Mac OS和CentOS与curl SSL Verification

的工作方式不同

我的解决方案:

在这种情况下,我通过openssl将 certfile.p12 提取到一个文件中:

openssl pkcs12 -in certfile.p12 -out keys.pem -nodes

一点点将php代码更改为此:

// Works great on Mac OS, Windows and CentOS 7 by my testing
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://some_api.com');
curl_setopt($ch, CURLOPT_PORT , 443);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSLCERT, getcwd() . '/cert/keys.pem');
curl_setopt($ch, CURLOPT_SSLCERTPASSWD, 'HerePassphraseOfPrivateKey');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));

它奏效了:)