title: Creating a PHP SOAP request with a certificate
body: <p>I am attempting to create a PHP SOAP connection to a .NET Web Service that has a self-signed certificate in order to lock down the service for communication over HTTPS. I continue to get error's and I'm not sure if it has something to do with the way I create the certificate, my Apache/PHP setup, the way I attempt to establish the SOAP request or something else. If anyone has any pointers they will be greatly appreciated. Thanks in advance.</p>
  
  <h2>Certificate Generation</h2>
  
  <p>This is how I am creating the certificate.</p>
  
  <ol>
  <li><p>Create the trusted root private key:</p>
  
  <blockquote>
    <p>genrsa -out ca_authority_prv.pem 2048</p>
  </blockquote></li>
  <li><p>Create the trusted root authority cert:</p>
  
  <blockquote>
    <p>req -new -key ca_authority_prv.pem -x509 -out ca_authority_cert.pem </p>
  </blockquote></li>
  <li><p>Make the cert authority trusted:</p>
  
  <blockquote>
    <p>x509 -in ca_authority_cert.pem -out ca_authority_trust.pem -trustout </p>
  </blockquote></li>
  <li><p>Exit OpenSSL and create a serial file:</p>
  
  <blockquote>
    <p>echo 1000 > ca_authority.srl</p>
  </blockquote></li>
  <li><p>Create the client private key:</p>
  
  <blockquote>
    <p>genrsa -out Client_prv.pem 2048</p>
  </blockquote></li>
  <li><p>Create the client request:</p>
  
  <blockquote>
    <p>req -new -key Client_prv.pem -out Client_req.pem</p>
  </blockquote></li>
  <li><p>Sign the client request with the CA:</p>
  
  <blockquote>
    <p>x509 -req -CA ca_authority_trust.pem -CAserial ca_authority.srl -CAkey ca_authority_prv.pem -in Client_req.pem -out Client_cert.pem </p>
  </blockquote></li>
  <li><p>Make the pfx for the client cert</p>
  
  <blockquote>
    <p>pkcs12 -export -in Client_cert.pem -inkey Client_prv.pem -out Client_cert.pfx </p>
  </blockquote></li>
  </ol>
  
  <h2>IIS Setup</h2>
  
  <p>Once this certificate is created I follow the same steps for a server certificate and:</p>
  
  <ol>
  <li><p>Add the trusted root CA to the Machine Trusted Root Store</p></li>
  <li><p>Add the server certificate to the machine store</p></li>
  <li><p>Setup IIS to use the server certificate and require client certificates</p></li>
  </ol>
  
  <h2>PHP SOAP Request</h2>
  
  <p>This is the code I am using to establish the PHP SOAP request (below is the error):</p>
  
  <pre><code>$wsdl = "https://localhost/MyService/MyService.asmx";
  $local_cert = "C:\\Certs\client_cert.pem";
  $passphrase = "openSaysMe";
  
      $soapClient = new SoapClient($wsdl, array('local_cert'=&gt;  $local_cert,'passphrase'=&gt;$passphrase));
      $theResponse = $soapClient-&gt;test();
  </code></pre>
  
  <h2>ERROR</h2>
  
  <pre><code>Warning: SoapClient::SoapClient() [soapclient.soapclient]: Unable to set private key file `C:\Certs\client_cert.pem' in C:\Program Files\Apache Group\Apache2\htdocs\soapWithAuth.php on line 53
  
  Warning: SoapClient::SoapClient() [soapclient.soapclient]: failed to create an SSL handle in C:\Program Files\Apache Group\Apache2\htdocs\soapWithAuth.php on line 53
  
  Warning: SoapClient::SoapClient() [soapclient.soapclient]: Failed to enable crypto in C:\Program Files\Apache Group\Apache2\htdocs\soapWithAuth.php on line 53
  
  Warning: SoapClient::SoapClient(https://localhost/MyService/MyService.asmx) [soapclient.soapclient]: failed to open stream: operation failed in C:\Program Files\Apache Group\Apache2\htdocs\soapWithAuth.php on line 53
  
  Warning: SoapClient::SoapClient() [soapclient.soapclient]: I/O warning : failed to load external entity "https://localhost/MyService/MyService.asmx" in C:\Program Files\Apache Group\Apache2\htdocs\soapWithAuth.php on line 53
  Error: SOAP-ERROR: Parsing WSDL: Couldn't load from 'https://localhost/MyService/MyService.asmx'
  </code></pre>
  
  <h2>ERROR LOG</h2>
  
  <pre><code>PHP Warning:  SoapClient::SoapClient() [&lt;a href='soapclient.soapclient'&gt;soapclient.soapclient&lt;/a&gt;]: failed to create an SSL handle in C:\\Program Files\\Apache Group\\Apache2\\htdocs\\soapWithAuth.php on line 54
  </code></pre>