I have a private and public key I must use to connect a SFTP server using Curl.
The public key works with FileZilla.
Here is my code :
<?php
$url = "sftp://host.tld";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_USERNAME, "XXXXXX");
curl_setopt($curl, CURLOPT_SSH_PRIVATE_KEYFILE, "ttt.ppk");
curl_setopt($curl, CURLOPT_SSH_PUBLIC_KEYFILE, "ttt.key");
curl_setopt($curl, CURLOPT_PROTOCOLS, CURLPROTO_SFTP);
curl_setopt($curl, CURLOPT_SSH_AUTH_TYPES,CURLSSH_AUTH_PUBLICKEY);
curl_setopt($curl, CURLOPT_DIRLISTONLY, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_VERBOSE,true);
$verbose = fopen('php://temp', 'w+');
curl_setopt($curl, CURLOPT_STDERR, $verbose);
$data = curl_exec($curl);
$verboseLog = "";
$error_no = curl_errno($curl);
$error = curl_error($curl);
if ($error_no != 0) {
rewind($verbose);
$verboseLog = stream_get_contents($verbose);
echo $error." - <pre>".$verboseLog."</pre>";
} else {
var_dump($data);
}
I have always this log :
* Trying x.x.x.x...
* TCP_NODELAY set
* Connected to host.tld (x.x.x.x) port 22 (#0)
* SSH MD5 fingerprint: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
* SSH authentication methods available: publickey
* Using SSH public key file 'ttt.key'
* Using SSH private key file 'ttt.ppk'
* SSH public key authentication failed: Invalid key data, not base64 encoded
* Authentication failure
* Closing connection 0
I tried with public key starts with
ssh-rsa AAA..........................
or with public key starts with
---- BEGIN SSH2 PUBLIC KEY ----
Comment: "rsa-key-20200330"
AAA...............
With same results.
Can someone help me ?
Thanks