cURL is unable to use client certificate , in local server

断了今生、忘了曾经 提交于 2019-12-29 08:07:51

问题


I set up a local server using XAMPP. I have two PHP scripts , a sender and a receiver. I am trying to send an XML file from the sender to the receiver using HTTP over SSL (HTTPS).

I created a self signed certificate, configured XAMPP, and I am using this code on my sender :

<?php
  /*
   * XML Sender/Client.
   */
  // Get our XML. You can declare it here or even load a file.


  $xml = file_get_contents("data.xml");

  // We send XML via CURL using POST with a http header of text/xml.
  $ch = curl_init();

  //curl_setopt($ch, CURLOPT_SSLVERSION,3);

  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);

  curl_setopt($ch, CURLOPT_CAINFO,  getcwd().'ipm.crt');
  curl_setopt($ch, CURLOPT_SSLCERT, getcwd().'ipm.pem');

  curl_setopt($ch, CURLOPT_SSLCERTPASSWD,'pass');

  //i use this line only for debugging through fiddler. Must delete after done with debugging.
  curl_setopt($ch, CURLOPT_PROXY, '127.0.0.1:8888');

  // set URL and other appropriate options
  curl_setopt($ch, CURLOPT_URL, "https://ipv4.fiddler/iPM/receiver.php");
  curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
  curl_setopt($ch, CURLOPT_REFERER, 'https://ipv4.fiddler/iPM/receiver.php');
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  $ch_result = curl_exec($ch);
  echo "Result = ".$ch_result;

  echo 'Curl error: ' . curl_error($ch);

  curl_close($ch);
  // Print CURL result.
?>

However i always get this error : Curl error: unable to use client certificate (no key found or wrong pass phrase?)

What can i possibly do wrong? The passphrase IS the word pass .

I created my .crt by doing :

openssl req -config openssl.cnf -new -out ipm.csr -keyout ipm.pem

openssl rsa -in ipm.pem -out ipm.key

openssl x509 -in ipm.csr -out ipm.crt -req -signkey ipm.key -days 365

I don't want to put the SSL_VERIFYPEER and SSL_VERIFYHOST to false.


回答1:


Try exchanging your files like so:

curl_setopt($ch, CURLOPT_CAINFO, getcwd().'ipm.pem');
curl_setopt($ch, CURLOPT_SSLCERT, getcwd().'ipm.crt');

I suspect you have them the wrong way round.




回答2:


I think that you already fixed your problem, but I can see two problems:

  1. you forgot about a slash between getcwd() and filename:
    curl_setopt($ch, CURLOPT_CAINFO, getcwd().'/ipm.crt');
    curl_setopt($ch, CURLOPT_SSLCERT, getcwd().'/ipm.pem');
  2. you need to attach a key file as @donparalias already said



回答3:


The key must be a filename, not a passphrase.



来源:https://stackoverflow.com/questions/14470225/curl-is-unable-to-use-client-certificate-in-local-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!