Search code examples
phpsslcurlcertificatenusoap

cURL error 58 while trying to access soap webserver


I'm trying to call (via PHP script) a remote (SOAP) webserver over https, it requires a password protected certificate. I'm using nuSoap for making the call but I always get the following error

nusoap_client: got wsdl error: Getting https://ws-t.pitre.tn.it/wcfrouting/wsdl/Documents.wsdl - HTTP ERROR: cURL ERROR: 58: unable to use client certificate (no key found or wrong pass phrase?)

require_once("../nusoap/lib/nusoap.php");

$pitre_wsdl = "https://ws-t.pitre.tn.it/wcfrouting/wsdl/Documents.wsdl";
$client = new nusoap_client($pitre_wsdl, "wsdl");
$err = $client->getError();

if ($err) {
    print("Error");
    exit();
}

$client->setCredentials(
    "",
    "",
    "certificate",
    array (
        "sslcertfile"   =>  "../pitre/cert.p12",
        "sslkeyfile"    =>  "../pitre/cert.p12",
        "certpassword"  =>  "mypass",
        "verifypeer"    =>  FALSE,
        "verifyhost"    =>  FALSE
    )
);

$result = $client->call(
    "GetTemplatesDocuments",
    array (
        "CodeAdm"   =>  "myCode"
    )
);

With the browser I can access the wisdl without problems. I tried the following answer:

cURL with SSL certificates fails: error 58 unable to set private key file

I got the same result.

Am I missing something?


Solution

  • I found the answer, my solution is the following:

    I wasn't able to make it work with nu_soap so I switched to SoapClient

    Fist of all I had to convert my p12 certificate to pem format using openssl

    openssl pkcs12 -in certificato.p12 -out certificato.pem -clcerts
    

    Then I downloaded the CA certificates from here https://curl.haxx.se/docs/caextract.html

    Here's my working code

    $params->a              = "a";
    $params->b               = "b";
    $params->c               = "c";
    $params->d               = "d";
    $params->e               = "e"; 
    
    $context = stream_context_create(array (
        "ssl"   =>  array (
            "verify_peer"       =>  false,
            "verify_peer_name"  =>  true,
            "local_cert"        =>  getcwd()."\certificato.pem",  //complete path is mandatory
            "passphrase"        =>  "mypassphrase",
            "allow_self_signed" =>  true
        ),
        "https" =>  array (
            "curl_verify_ssl_peer"  =>  false,
            "curl_verify_ssl_host"  => false
        )
    ));
    
    $pitre_client = new SoapClient($pitre_wsdl, array (
        "trace"             =>  1,
        "exceptions"        =>  true,
        "location"          =>  "https://ws-t.pitre.tn.it/wcfrouting/servicerouter.svc",
        "cafile"            =>  getcwd()."\cacert.pem", //complete path is mandatory
        "stream_context"    =>  $context
    ));
    
    // the call
    $response = $pitre_client->GetTemplatesDocuments(
        array (
            'request' => $params  //request key can be different
        )
    );
    

    I hope this will help someone facing the same issue