EldoS | Feel safer!

Software components for data protection, secure storage and transfer

SSH public and private key - .Net C#

Also by EldoS: Rethync
The cross-platform framework that simplifies synchronizing data between mobile and desktop applications and servers and cloud storages
#9796
Posted: 04/24/2009 09:56:29
by Blue White (Basic support level)
Joined: 01/22/2009
Posts: 27

Hi,

I am working on a windows service that should upload a file to an SFTP server.
I have found out how to upload the file from the Sample applications.



Code
SftpClient.Port = 22;

SftpClient.Address = tbHostName.Text;
SftpClient.Username = tbUserName.Text;
SftpClient.Password = tbPassword.Text;
SftpClient.AuthenticationTypes = SBSSHConstants.Unit.SSH_AUTH_TYPE_PASSWORD;




But i didn't know yet how to send the public and private key to the SFTP server in order to authenticate.

Please note that I have generated a key pair using "Putty SSH Key generation utility"

can you please show me an example on how to upload file to an SFTP server specifically how to declare the public and private key.

Thanking you in advance.
#9798
Posted: 04/24/2009 10:14:26
by Blue White (Basic support level)
Joined: 01/22/2009
Posts: 27

the last line of code is incorrect, it should be:


Code
ftpClient.AuthenticationTypes = SBSSHConstants.Unit.SSH_AUTH_TYPE_publickey;
#9799
Posted: 04/24/2009 10:46:35
by Eugene Mayevski (EldoS Corp.)

Most of SFTP sample applications, included with SecureBlackbox, support public key authentication. Please check them again.


Sincerely yours
Eugene Mayevski
#9800
Posted: 04/24/2009 11:55:13
by Blue White (Basic support level)
Joined: 01/22/2009
Posts: 27

I have checked all samples under SFTPSecureBlackbox and I have found the following :

Code
Client.OnKeyValidate += new TSSHKeyValidateEvent(Client_OnKeyValidate);



But What I am trying to do is to connect to an SFTP server, and I need to present to the public key and private key that are already generated.
I dont need to validate any key because i am the client.
so is there a property like these ones:
SftpClient.Address = tbHostName.Text;
sftpClient.Username = tbUserName.Text;
....
?

See, all i need is to present to the SFTP Server the public and private keys.
I dont need to validate keys.

and I have searched the samples for "SSHKEY" word, it only occurs when validating key...

can you please help me on how to present the public and private key to the SFTP server in order to authenticate and be able to upload a file to it?


Thanks again.
B
#9802
Posted: 04/24/2009 12:27:25
by Ken Ivanov (EldoS Corp.)

The following code does the job:
Code
            KeyStorage.Clear()
            Dim key As New TElSSHKey
            Dim privateKeyAdded As Boolean = False
            If dlg.edPrivateKey.TextLength > 0 Then
                Dim pwd As String
                pwd = InputDialog("Enter password", "Enter password for private key:", "")
                Dim err As Integer
                err = key.LoadPrivateKey(dlg.edPrivateKey.Text, pwd)
                If err = 0 Then
                    KeyStorage.Add(key)
                    SftpClient.AuthenticationTypes = SftpClient.AuthenticationTypes Or SBSSHConstants.Unit.SSH_AUTH_TYPE_PUBLICKEY
                    privateKeyAdded = True
                Else
                    Log("Private key file could not be loaded due to error " + err.ToString(), True)
                End If
            End If

            If Not privateKeyAdded Then
                SftpClient.AuthenticationTypes = SftpClient.AuthenticationTypes And Not SBSSHConstants.Unit.SSH_AUTH_TYPE_PUBLICKEY
            End If


Quote
I dont need to validate any key because i am the client.

It's just wrong. You *must* validate server keys to make your connection secure. It won't be otherwise.
#9804
Posted: 04/26/2009 08:26:49
by Blue White (Basic support level)
Joined: 01/22/2009
Posts: 27

Quote

The following code does the job:
Code

            KeyStorage.Clear()
            Dim key As New TElSSHKey
            Dim privateKeyAdded As Boolean = False
            If dlg.edPrivateKey.TextLength > 0 Then
                Dim pwd As String
                pwd = InputDialog("Enter password", "Enter password for private key:", "")
                Dim err As Integer
                err = key.LoadPrivateKey(dlg.edPrivateKey.Text, pwd)
                If err = 0 Then
                    KeyStorage.Add(key)
                    SftpClient.AuthenticationTypes = SftpClient.AuthenticationTypes Or SBSSHConstants.Unit.SSH_AUTH_TYPE_PUBLICKEY
                    privateKeyAdded = True
                Else
                    Log("Private key file could not be loaded due to error " + err.ToString(), True)
                End If
            End If

            If Not privateKeyAdded Then
                SftpClient.AuthenticationTypes = SftpClient.AuthenticationTypes And Not SBSSHConstants.Unit.SSH_AUTH_TYPE_PUBLICKEY
            End If



Thanks for your explanation but knowing that I lack so much knowledge about this security topic, seems I did not explain myself well.

I have a windows application built in C# language on VS 2008,
it has to wake up everyday, and upload a file to the same SFTP Server.
I already have: the username, password, and Address for this SFTP server.

and I have 2 files : one containing the public key and the other one containing the private key.

as you know in order to authenticate myself ( as a client) to the SFTP Server (i only need public key authentication, i dont need password) I have to present to this SFTP server my public key.

I already know what to write to connect to the right SFTP Server:
Code
SftpClient.Port = 22;

SftpClient.Address = tbHostName.Text;
SftpClient.Username = tbUserName.Text;
SftpClient.Password = tbPassword.Text;
SftpClient.AuthenticationTypes = SBSSHConstants.Unit.SSH_AUTH_TYPE_PUBLICKEY


But I still need to know how to present to the Server my public key. ( do I need to present the private key also ? )

I did some search in the Securebox HTML help that comes along with the Securebox, and I have found the following methods which I might need in order to present to the SFTP server my public key so that I can be authenticated and able to upload a file to it.

The problem is I dono what else is missing other than using these methods/properties. and I dono which ones exactly i need to use. I dono in which order to use them...
Id be grateful if you showed me an example that uses these methods properly.

Please note again that I already have the keypair in 2 files, I just need to pass them to the SFTP server in order to be authenticated and able to upload my file to this SFTP server.

Methods found in Securebox HTML help are:
1. Key object must be initialized with
Code
int LoadPublicKey([in] string PublicKeyFile);

2.
Code
int LoadPrivateKey([in] string PrivateKeyFile, [in] string Passphrase /* = "" */);
// to load the private key
3.
Code
void Add(ElSSHKey key, ElSSHPublicKeyAttributes attributes, bool Overwrite);
(This method adds the key to the server storage.)
4. client requested public key authentication (
Code
event TSSHAuthPublicKeyEvent OnAuthPublicKey;
   delegate void TSSHAuthPublicKeyEvent(object Sender, string Username, ElSSHKey Key, ref bool Accept)
)

5.
Code
event TSBSSHPublicKeyAddEvent OnAdd;
    delegate void TSBSSHPublicKeyAddEvent(object Sender, ElSSHKey Key, ElSSHPublicKeyAttributes Attributes, bool Overwrite, ref int ErrorCode, ref string Comment);


6.
Code
ElSSHKey ServerKey
; (This property contains server's public key)(get the server's public key)(Use read-only this property to obtain public key which identifies the SSH server which the client is connected to.)

7. validate the server's public key (using:
Code
ElSSHKey ServerKey;
)
(I dont understand why i need to validate the server's public key, since I am choosing to connect to the right server using the SftpClient.Address, SftpClient.Username and SftpClient.password )


would you please show me an example on how to use these methods to authenticate myself to the sftp server, and it would be great if there's a similar example which implements these methods.

Thanks again.
#9805
Posted: 04/27/2009 00:46:59
by Eugene Mayevski (EldoS Corp.)

You have been given the complete piece of code which performs authentication with a keypair (and yes, you must load a private key). Please use this sample as a guide.


Sincerely yours
Eugene Mayevski
#9811
Posted: 04/27/2009 01:40:14
by Blue White (Basic support level)
Joined: 01/22/2009
Posts: 27

are there any C# examples?
#9813
Posted: 04/27/2009 01:51:00
by Ken Ivanov (EldoS Corp.)

Taken from the SimpleSFTPDemo sample:

Code
KeyStorage.Clear();
TElSSHKey Key = new TElSSHKey();
SftpClient.AuthenticationTypes = SBSSHConstants.Unit.SSH_AUTH_TYPE_PASSWORD | SBSSHConstants.Unit.SSH_AUTH_TYPE_KEYBOARD;

if (dlg.txtPrivateKey.Text != "")
{
   int err = Key.LoadPrivateKey(dlg.txtPrivateKey.Text, "");
   if (err == 0)
   {
      KeyStorage.Add(Key);
      SftpClient.AuthenticationTypes = SftpClient.AuthenticationTypes | SBSSHConstants.Unit.SSH_AUTH_TYPE_PUBLICKEY;
   }
   else
   {
      SftpClient.AuthenticationTypes = SftpClient.AuthenticationTypes & (~SBSSHConstants.Unit.SSH_AUTH_TYPE_PUBLICKEY);
      Log("Private key file could not be loaded due to error "  + err.ToString(), true);
   }
}
else
{
   SftpClient.AuthenticationTypes = SftpClient.AuthenticationTypes & (~SBSSHConstants.Unit.SSH_AUTH_TYPE_PUBLICKEY);
}
#9815
Posted: 04/27/2009 02:48:34
by Blue White (Basic support level)
Joined: 01/22/2009
Posts: 27

Thanks again for your support.

however, the code piece above does not use the method LoadPublicKey. how is the public key passed to the SFTP Server then?


so in my case i have to use LoadPublicKey method to load public key from a file, then after loading both the private and public
i will use Add(key) method to add the keypair to the server storage. right?

do i need to do anything else than this to pass to the SFTP
server my Public and Private keys?


Best.
Also by EldoS: CallbackFilter
A component to monitor and control disk activity, track file and directory operations (create, read, write, rename etc.), alter file data, encrypt files, create virtual files.

Reply

Statistics

Topic viewed 26337 times

Number of guests: 1, registered members: 0, in total hidden: 0




|

Back to top

As of July 15, 2016 EldoS Corporation will operate as a division of /n software inc. For more information, please read the announcement.

Got it!