Home > database >  Authentication failure with curl in PHP ( openssh connexion OK )
Authentication failure with curl in PHP ( openssh connexion OK )

Time:01-31

I am trying to connect via php curl on an SFTP destination, with key authentication.

I manage to connect outside of curl, with openSSH: ( it work )

sftp -i myPrivateKey sftp://[email protected]

however, I cannot reproduce this connection in curl, I have an authentication failure error. how is that possible?

    $dest = 'sftp://myusername@mydestination:22';

    $curlHandler = curl_init();
    curl_setopt($curlHandler, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curlHandler, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curlHandler, CURLOPT_URL, $dest);
    curl_setopt($curlHandler, CURLOPT_PROTOCOLS, CURLPROTO_SFTP);
    curl_setopt($curlHandler, CURLOPT_VERBOSE, true);
    curl_setopt($curlHandler, CURLOPT_SSH_PUBLIC_KEYFILE, mypublickey);
    curl_setopt($curlHandler, CURLOPT_SSH_PRIVATE_KEYFILE,myprivatekey);

    $resp = curl_exec($curlHandler);

log:

* SSH public key authentication failed: Callback returned error
* Failure connecting to agent
* Authentication failure
* Closing connection 0

I am sure of the validity of the keys, I checked the fingerprints with the remote server, and I manage to connect outside of php-curl.

I also checked that my keys were in restricted access and accessible

CodePudding user response:

Finally, it turns out that ubuntu LTS 14 uses the libgcrypt utility, which creates SSH keys with private keys starting with" ----BEGIN OPENSSH ---" and in the case of SFTP, we need a .pem starting with "-----BEGIN RSA PRIVATE KEY-----", so I converted my private key, and it worked

ssh-keygen -f id_rsa -m pem -p
  • Related