views:

912

answers:

1

I'm trying to use soap to call a webservice but I keep getting the following error "Warning: SoapClient::SoapClient(): Unable to set private key file".

I'm assuming that the error comes due to the fact the the .cer file I am using only includes public key and no private key. But i'm not sure of another way to use the .cer file. If i don't use the .cer file i can connect just fine and I am able to call and receive results when i use the __getFunctions() method. However, when i try to use other methods i need to be authorized and that leads to the problem. Below is the simple code i am trying to use. Please let me know if more information is required.

ini_set('display_errors',1);
error_reporting(E_ALL);

ini_set('soap.wsdl_cache_enabled', 0);
$username = 'user';
$password = 'pass';

$ns = 'GatewayEDI.WebServices';
$auth = array();
$auth['User'] = new SOAPVar($username, XSD_STRING, null, null, null, $ns);
$auth['Password'] = new SOAPVar($password, XSD_STRING, null, null, null, $ns);
$headerBody = new SOAPVar($auth, SOAP_ENC_OBJECT);
$header = new SOAPHeader($ns, 'AuthSOAPHeader', $headerBody);

$client=new SoapClient('https://url/Service.asmx?WSDL',
                   array(
                  'local_cert' => 'file.cer'
                   ));

$client->__setSOAPHeaders(array($header));

$param = array(
  'X12Input'=>"testing",
  "GediPayerID"=>"52",
  "ResponseDataType"=>"Xml"
);

//this leads to private key error
echo $result = $client->DoInquiryByX12Data($param,$header);
A: 

I believe your .pem/.cer file should have your private key in it:

-----BEGIN RSA PRIVATE KEY----- 
# base64 encoded key 
-----END RSA PRIVATE KEY----- 
-----BEGIN CERTIFICATE----- 
# base64 encoded cert
-----END CERTIFICATE-----

If your private key's first line has a directive similar to "Proc-Type: 4,ENCRYPTED" you'll need to include the "passphrase" option when constructing your SoapClient(). You can also strip the passphrase requirement with OpenSSL, my syntax is a bit rusty so you may want to double check if you try it:

openssl rsa -in /path/to/private.key -out /path/to/private.key

"private.key" should be just the private key in this context (you can add it into the .cer/.pem file after the passphrase has been removed.

Owen
I don't have a private key for this certificate. Can I make my own private key and add it to the certificate?
IHateSoap
Nope, the private key has to match it. Can you not get it from whoever gave you the cert in the first place?
Owen
they are claiming that there is no private key, but i don't see how it would work without a private key. If i try to remove the certificate and not use it at all then i get an authentication error. Are you saying that you don't know of any way that it could work without a private key?
IHateSoap
hmm can you post your file.cer, and the error message you get when you access a https URL that requires a certificate (ie, with no local_cert option?)
Owen
this is the error i get when i don't use the local_cert option. "Fatal error: Uncaught SoapFault exception: [HTTP] Unauthorized"The file.cer file looks something like....-----BEGIN CERTIFICATE-----MIIGPjCCBSagAwIBAgIQDBPCdedfOhIfuBrYJd8S+zANBgkqhkiG9w0BAQUFADCBvjELMAkGA1UEBhMCVVMxFzAVBgNVBAoTDlZlcmlTaWduLCBJbmMuMR8wHQYDVQQLExZWZXJpU2lnbiBUcnVzdCBOZXR3b3JrMTswOQYDVQQLEzJUZXJtcyBvZiB1c2UgYXQgaHR0cHM6Ly93d3baZdTIDZO7Hp6Op9TCd2InZc-----END CERTIFICATE-----
IHateSoap
hmm, is it possible that you don't need a certificate, but that perhaps the SoapVARs are incorrect for whatever reason? it may be helpful to get a dump of the actual SOAP call you're generating and check with them if it looks correct? as in, i wonder if the certificate is related specifically to SSL, and not to authorization to access the resource.
Owen
do you mean the username and password are incorrect?
IHateSoap
perhaps, i think my comment in general was more, are you sure you need public key authorization for this? it could be just u/p are wrong if it's basic authentication over https.
Owen
Yea I asked and got another server that does not require the certificate. unfortunately now i'm getting the authentication error.The error is the following: "Uncaught SoapFault exception: [HTTP] Unauthorized" I am setting the headers wrong or is it that i don't have the correct user and pass? Thank you
IHateSoap
Hard to tell without the documentation, but I'd definitely check u/p first just to see. Otherwise, I'd suggest using "getLastRequest()" to see the actual Soap call you're making, and ask them if that's the correct syntax? http://php.net/manual/en/soapclient.getlastrequest.php
Owen
I have the trace option enabled but i can't even use the getLastRequest() function because the authentication error happens right when i try to call the function, not before. So it just returns null since it never executes the function. Thank you.
IHateSoap