Linesofcode
Linesofcode

Reputation: 5903

cURL unable to use client certificate (no key found or wrong pass phrase?)

I have read and tried thousands of solutions in different posts and none of them seems to work with me. This three are example of that.

cURL is unable to use client certificate , in local server

php openssl_get_publickey() and curl - unable to use client certificate (no key found or wrong pass phrase?)

Getting (58) unable to use client certificate (no key found or wrong pass phrase?) from curl

I received a .p12 certificate which I converted to .pem file in https://www.sslshopper.com/ssl-converter.html

The password is correct otherwise it wouldn't convert it.

$xml = 'my xml here';
$url = 'https://qly.mbway.pt/Merchant/requestFinancialOperationWS';

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

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_TIMEOUT, 60); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSLCERT, base_url() . 'public/cert.pem');
curl_setopt($ch, CURLOPT_SSLCERTPASSWD, 'my password here');
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); 

$data = curl_exec($ch); 

if(!$data)
    print_r('ERROR: ' . curl_error($ch));
else
    print_r('SUCCESS: ' . curl_error($ch));

I have tried with SoapUI application and works fine but with cURL I'm receiving the error:

unable to use client certificate (no key found or wrong pass phrase?)

I have tried without success:

  1. Disable CURLOPT_SSL_VERIFYPEER and/or CURLOPT_SSL_VERIFYHOST
  2. Add CURLOPT_SSLKEYTYPE and/or CURLOPT_SSLKEY fields

EDIT 1:

I have been trying around with SOAPClient besides cURL and it seems that the error might be the headers.

My headers after print_r($soapClient) are the following:

Host: qly.mbway.pt
Connection: Keep-Alive
User-Agent: PHP-SOAP/5.5.9-1ubuntu4.14
Content-Type: application/soap+xml; charset=utf-8; action=""
Content-Length: 1750

I would like to know how can I remove the action=""? I tried to extend the original class without success in terms of changing the header.

class MySoapClient extends SoapClient 
{   
   public function __construct($wsdl, $options = array())
   {
     $ctx_opts = array('http' => array('header' => array('Content-Type' => 'application/soapyyyyyml')));

     $ctx = stream_context_create($ctx_opts);

     parent::__construct($wsdl, array('stream_context' => $ctx));
   }
}

Upvotes: 4

Views: 6946

Answers (1)

Linesofcode
Linesofcode

Reputation: 5903

Solved with cURL.

The problem was the path of the pem file.

I was using base_url() . 'public/cert.pem' but that's not possible. Instead I need to use a relative path such as ./public/cert.pem.

Upvotes: 2

Related Questions