Back to product page

PrivateKey property


Private key used for authentication.

Type

A Variant value. For VC users this is SAFEARRAY VT_UI1 or LPDISPATCH (IKeys *).

Syntax

  • Basic
object.PrivateKey [= value]
The PrivateKey(object,value) syntax has these parts:
objectAn expression evaluating to an object of type wodSSH.
valueA Variant value.

Remarks

The PrivateKey property is used when you want to authenticate with the server using your private/public key pair, instead of using Password. This is a feature that should be supported by all SSH servers.

The Idea of using keys is this: you own a private key (and no one else knows this value). You supply the server with the public key that corresponds to your private key. Once you initiate a connection, wodSSH will request publickey authentication. The server will check its internal list of public keys (usually stored in -/.ssh/authorized_keys2 or -/.ssh2/authorized files). If a match is found, it will send a request to wodSSH to prove you own the private key. Internally, wodSSH will sign some data using the key you provided, and the server will check the signature. If they match, it will allow you to login. Some servers will also require you to enter a password, in which case this make the server even more secure.

To generate a PrivateKey that you can use with the server, use the Keys object (included in setup package) like this (VB sample):
 
   Dim key As New WODSSHKeyLib.Keys
   key.Generate RSAkey ' 1024 bits is default
   key.Save RSAkey, "C:\my_rsa_key.txt", "My secret password"
 

The above sample will generate your private key and store it to file on disk, protected with a password. You can immediately continue your code like this:
 
   Ssh1.Login = "johndoe"
   Ssh1.PrivateKey = key ' or also Ssh1.PrivateKey = key.PrivateKey(RSAkey)

   Ssh1.Authentication = authPubkey
   .......
   Ssh1.Connect
 

Since generation of keys may be a lengthy process (for large bit numbers it can take a few seconds), you shouldn't generate it every time you need to use it. Rather, since it was saved, you should try to load it from disk. A typical scenario would be :
 
   Dim key As New WODSSHKeyLib.Keys
 
   On Error Resume Next
   ' try to load previously saved key
   key.Load "C:\my_rsa_key.txt", "My secret password"
   If Err <> 0 Then 'key was not saved yet
       key.Generate RSAkey ' 1024 bits is default
       key.Save RSAkey, "C:\my_rsa_key.txt", "My secret password"
        ' next time you run this code it will be able to load it from the disk,
        ' so expensive Generate will not be called anymore.
   End If
 

Now that you have your PrivateKey created, you should let the server know about it. You should do this by pasting the public key to the appropriate files on the server. For SSH servers (version 2), these are

-/.ssh2/authorization which should have a line of text Key somefile.pub in a separate line of the file,
-/.ssh2/somefile.pub which should contain your public key, as returned by Keys.PublicKeySSH property.

For OpenSSH servers, you should paste the contents of Keys.PublicKeyOpenSSH property to:

-/.ssh/authorized_keys2 as a new line in the file.

For VC users, you can prepare a returned key (loaded from file, for example) by converting it to SAFEARRAY like this:

(Buffer holds key data, and Bufsize holds key length)
 
   SAFEARRAY *psa;
   SAFEARRAYBOUND rgsabound[1];
   char HUGEP *data;
 
   rgsabound[0].lLbound=0;
   rgsabound[0].cElements=Bufsize;
 
   psa = SafeArrayCreate(VT_UI1,1,rgsabound);
   if (!psa)
   return WSA_NOTOUGH_MEMORY;
 
   SafeArrayAccessData(psa, (void HUGEP* FAR*)&data);
 
   memcpy(data,Buffer, Bufsize);
   SafeArrayUnaccessData(psa);
 
   VARIANT var;
   var.vt =VT_ARRAY | VT_UI1;
   var.parray=psa;
 

and now you can pass this VARIANT to the PrivateKey property. Or, you can pass LPDISPATCH from the IKeys object directly in the same way:
 
   VARIANT var;
   var.vt = VT_DISPATCH;
   var.pdispVal = (LPDISPATCH)your_keys_object_instance
 

and it will work too.

Platforms

Windows