Page 1 of 1

SFTP logon password issue

Posted: Wed 28 Sep 2016 23:08
by hugoluiz
Hi all,
I am having trouble connecting a SFTP server using Password option. The same problem happen when I use the SecureBridge7 SFTPClient demo software.
If I choose keyboard-interactive option and type the password it's works fine, if I choose Password option doesn't work.
I can connect fine to the SFTP server using Filezilla. Filezilla show me the Unknow host key, I click OK and work fine.
I believe I need to accept the host key as well, but I tried every tip I found and nothing till know.
To make simple I prefer to use the SecureBridge SFTPClient demo code as a example.

Regards, Hugo

Re: SFTP logon password issue

Posted: Thu 29 Sep 2016 14:07
by ViktorV
To solve the issue, please double-click the ScFileStorage component in our SFTPClient demo project, and on the Keys tab remove all the keys. Then compile and run our project, try to connect to the server - there must appear a dialog containing information about the key sent from the server - click Ok.

Re: SFTP logon password issue

Posted: Thu 19 Jan 2017 23:56
by kevinevanssr
I am repeatedly getting a "Host Key not verified" message in this same scenario. I removed the key as recommended above but the error message remains. I'm new to the SFTP programming process so any assist with the code below would be appreciated. Need help with the connection validation process.

procedure TForm1.btnSFTPUploadToASMClick(Sender: TObject);
var
openDialog : TOpenDialog;
Key: TScKey;
Algorithm: TScAsymmetricAlgorithm;
BitCount: integer;
msg, fp :string;

begin


openDialog := TOpenDialog.Create(self);
openDialog.Options := [ofAllowMultiSelect, ofFileMustExist];
openDialog.Title := 'File SFTP to ASM';
openDialog.Filter := 'ZIP file|*.zip|All files (*.*)|*.*';
openDialog.FilterIndex := 1;


if openDialog.Execute then
begin

with ScSSHClient do begin

HostName := eAppleSFTPserverASM.Text; // host
HostKeyName := eAppleSFTPserverASM.Text; //HostKey Name
User := eAppleSFTPUsernameASM.Text; // username
Password := eAppleSFTPPasswordASM.Text; // password
port := 22;
KeyStorage := ScFileStorage;
Authentication := atPassword;

Key := SCFileStorage.Keys.FindKey(HostKeyName);

if Key = nil then begin
Key := TScKey.Create(SCFileStorage.Keys);
Key.KeyName := HostKeyName;
Algorithm := aaRSA;
BitCount := 2048;
end
else begin
Key.Ready := True;
Algorithm := Key.Algorithm;
BitCount := Key.BitCount;
end;

try
Key.Generate(Algorithm, BitCount);
Key.ExportTo(Key.KeyName + '.pub', True, '');

msg := 'The client key file has been generated in the current application directory.'#13#10 +
'To connect with authentication by key, you should pass the "' + Key.KeyName +
'.pub" file to the server and set the server to work with this file.';
MessageDlg(msg, mtInformation, [mbOk], 0);
except
on E: Exception do
MessageDlg('Cannot generate key: ' + E.Message, mtWarning, [mbOk], 0);
end;
Key.GetFingerPrint(haMD5, fp);

// Error happens when the next line is executed.
Connect;

end;

ScSFTPClient.Initialize;
ScSFTPClient.UploadFile(OpenDialog.FileName, '/dropbox/' +ExtractFileName(OpenDialog.FileName), False);

openDialog.Free;

end;

end;

Re: SFTP logon password issue

Posted: Fri 20 Jan 2017 10:01
by ViktorV
This error occurs if the key received from the server and the key specified in HostKeyName do not match. You should handle the TScSSHClient.OnServerKeyValidate event. Therefore, you should delete the following code from your sample:

Code: Select all

Key := SCFileStorage.Keys.FindKey(HostKeyName);

if Key = nil then begin
Key := TScKey.Create(SCFileStorage.Keys);
Key.KeyName := HostKeyName;
Algorithm := aaRSA;
BitCount := 2048;
end
else begin
Key.Ready := True;
Algorithm := Key.Algorithm;
BitCount := Key.BitCount;
end;

try
Key.Generate(Algorithm, BitCount);
Key.ExportTo(Key.KeyName + '.pub', True, '');

msg := 'The client key file has been generated in the current application directory.'#13#10 +
'To connect with authentication by key, you should pass the "' + Key.KeyName +
'.pub" file to the server and set the server to work with this file.';
MessageDlg(msg, mtInformation, [mbOk], 0);
except
on E: Exception do
MessageDlg('Cannot generate key: ' + E.Message, mtWarning, [mbOk], 0);
end;
Key.GetFingerPrint(haMD5, fp);
And add the following one:

Code: Select all

procedure TForm1.ScSSHClientServerKeyValidate(Sender: TObject;
  NewServerKey: TScKey; var Accept: Boolean);
var
  Key: TScKey;
  fp, msg: string;
begin
  Key := ScFileStorage.Keys.FindKey(ScSSHClient.HostName);
  if (Key = nil) or not Key.Ready then begin
    NewServerKey.GetFingerPrint(haMD5, fp);
    msg := 'The authenticity of server can not be verified.'#13#10 +
           'Fingerprint for the key received from server: ' + fp + '.'#13#10 +
           'Key length: ' + IntToStr(NewServerKey.BitCount) + ' bits.'#13#10 +
           'Are you sure you want to continue connecting?';

    if MessageDlg(msg, mtConfirmation, [mbOk, mbCancel], 0) = mrOk then begin
      Key := TScKey.Create(nil);
      try
        Key.Assign(NewServerKey);
        Key.KeyName := ScSSHClient.HostName;
        ScFileStorage.Keys.Add(Key);
      except
        Key.Free;
        raise;
      end;

      Accept := True;
    end;
  end;
end;

Re: SFTP logon password issue

Posted: Fri 20 Jan 2017 19:26
by kevinevanssr
Viktor - Thank you. Much appreciated.

I made the changes from your response and I'm now receiving a different error.
Socket Closed cannot receive data.

Is this error indicating the validation of the key is complete but the connection is timing out?
I believe I have all the other settings the same as the working WinSCP connect.

I changed the timeout to 90 per your response in a different string. same error.

Also, I if i change the connected opting in the RAD IDE on the ScSSHClient, it connects after asking me to "ok" the provided key.

What am i missing?

Re: SFTP logon password issue

Posted: Mon 23 Jan 2017 09:21
by ViktorV
Please clarify the issue you encountered using our SFTPClient demo project. You can find the SFTPClient project in the %SecureBridgeDemos%\SFTPClient directory. %SecureBridgeDemos% is the SecureBridge Demo projects installation path on your computer.
If the issue is not reproduced on our SFTPClient demo project, please bring you code into accordance with the code used in our SFTPClient project. Otherwise, please compose a full sample demonstrating the specified behavior and send it to us using the contact form https://www.devart.com/company/contactform.html, in order for us to provide you a more detailed answer.