Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SSH Host Key Fingerprint #835

Closed
DouglasMarcelinoDev opened this issue Jun 10, 2021 · 4 comments
Closed

SSH Host Key Fingerprint #835

DouglasMarcelinoDev opened this issue Jun 10, 2021 · 4 comments

Comments

@DouglasMarcelinoDev
Copy link

DouglasMarcelinoDev commented Jun 10, 2021

Using WinSCP DLL, I can configure my ssh host key fingerprint like this

SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Sftp,
HostName = config.Host,
UserName = config.User,
PortNumber = config.Port,
Password = config.Password,
SshHostKeyFingerprint = config.Key
};

where config.Key = "ssh-rsa 2048 [...]="

How can I add this "SshHostKeyFingerprint" using SSH.NET dll?

something like that

client = new SftpClient(config.Host, config.Port, config.User, config.Password, config.Key);

@IgorMilavec
Copy link
Collaborator

You need to attach to the HostKeyReceived event before establishing the connection:

var client = new SftpClient(connectionInfo);
client.HostKeyReceived += (sender, e) =>
{
    // e is an Renci.SshNet.Common.HostKeyEventArgs that you can use to check the received host key
    // throw here to abort the connection
};
client.Connect();

@DouglasMarcelinoDev
Copy link
Author

DouglasMarcelinoDev commented Jun 15, 2021

@IgorMilavec Well, when I try this, I get this error
Accessing a hash algorithm by manipulating the HashName property is not supported on this platform. Instead, you must instantiate one of the supplied subtypes (such as HMACSHA1.)

var client = new SftpClient(config.Host, config.Port, config.User, config.Password);
client.HostKeyReceived += (sender, e) =>
{

     e.CanTrust = true;

};

client.Connect();

Where I can attach config.Key = "ssh-rsa 2048 [...]="?

@IgorMilavec
Copy link
Collaborator

You can not (AFAIK) "attach" the key, you must validate it inside HostKeyReceived event handler. Something like this:

var client = new SftpClient(config.Host, config.Port, config.User, config.Password);
client.HostKeyReceived += (sender, e) =>
{
     e.CanTrust = (FormatKey(e) == config.Key);
};

client.Connect();

Of course you need to implement FormatKey function appropriately.

@IgorMilavec
Copy link
Collaborator

@DouglasMarcelinoDev this is a duplicate of #859 , please see a workaround there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants