SSH Client connection - WeOnlyDo Discussion board

SSH Client connection (wodSSHTunnel)

by Mariana Cimpoca, Timisoara, Tuesday, December 13, 2016, 08:12 (2663 days ago)

Hello,

I want to create an SSH Client connection using the private/public key mechanism.
How can I generate in Visual Studio C++ a private key using :
Generate(KeyType, VARIANT);
Load(Filename, VARIANT);
Save(KeyType, Filename, VARIANT);

These methods are of Keys class, use VARIANT data type, that is non specific for C++.
How can I initialize a VARIANT type input parameter ?
These methods are chosen correctly for my purpose ?

Thanks for you help,
Mariana C.

SSH Client connection

by Jasmine, Tuesday, December 13, 2016, 10:22 (2663 days ago) @ Mariana Cimpoca

Hi Mariana,

VARIANT is regular C/C++ type, and is widely used in COM world to hold "some sort of data", where variant type can be defined inside the data itself. You can see list of what variant supports, for example, here:

https://msdn.microsoft.com/en-us/library/windows/desktop/ms221627(v=vs.85).aspx

As for wodSSHTunnel which I see you reference here, did you check out VC sample included with the product called 'Console sample to create keys' which does exactly what you need and uses VARIANTs? I believe it could be 'drop in' sample for your needs.

I hope this helps!
Jasmine.

SSH Client connection

by Mariana Cimpoca, Timisoara, Thursday, December 15, 2016, 10:20 (2661 days ago) @ Jasmine

Hello,

Using Generate/Save methods of CKeys and considering all input parameters, I expect to find the private key in file.
I mention that the file is correctly set up and operational.

CKeys oKey;
oKey.Generate(RSAkey, BitCount);
okey.Save(RSAkey, Filename, Password);

Why the key is not stored in file ?

Thanks,
Mariana

SSH Client connection

by Jasmine, Thursday, December 15, 2016, 10:43 (2661 days ago) @ Mariana Cimpoca

Hi Mariana,

if error was not returned, then key was saved to file. Are you saying file is not there where you expect, or it's empty?

Jasmine.

SSH Client connection

by Mariana Cimpoca, Timisoara, Thursday, December 15, 2016, 14:36 (2661 days ago) @ Jasmine

Hello,

This code should create a file (with the key), because the file is not created ?
Even if I create one, still does not save anything in it.
Also, there is no exception related to the use of these methods.
try
{
oKey.Generate((long) RSAkey, sbitCount);
oKey.Save((long) RSAkey, (LPCTSTR)fNameClient, vmyPassword);
}
catch(CException* pe){
AfxMessageBox(_T("Error"));
pe->ReportError();
}

Thank you,
Mariana

SSH Client connection

by Jasmine, Thursday, December 15, 2016, 14:38 (2661 days ago) @ Mariana Cimpoca

Hi Mariana,

I don't know, since I don't know what is 'oKey', what is 'RSAKey', what is exact value of 'fNameClient' and what is in 'vmyPassword'. I can only assume they are correct, but they could be as well empty or "".

Can you hardcode exact values in it, and try again? Also, make sure you check for return value in Generate and Save, if they are declared as returning int (it depends on programming environment).

Do they return anything?

Jasmine.

SSH Client connection

by Mariana Cimpoca, Timisoara, Thursday, December 15, 2016, 15:24 (2661 days ago) @ Jasmine

Hi,

Here's the code, CKeys class is part of wodKeys.
I use wodSFTPdll.lib for my SSH client connection application.

#define RSAkey 0
#define DSAkey 1
#define DCSkey 2

void CSSHConnectionApp::KeyManager()
{
CKeys oKey; // Keys object
VARIANT sbitCount;

const CHAR *pPassword = "AuthenticationPassword";
vmyPassword.vt = VT_BSTR; // set to BSTR string
vmyPassword.bstrVal = SysAllocString((OLECHAR *)pPassword);

CString fNameClient= _T ("C:\\KeyValues.rsa");
CFile keyFile;

try
{
keyFile.Open((LPCTSTR)fNameClient, CFile::modeCreate | CFile::modeReadWrite);
}
catch(CFileException ex)
{
ex.ReportError();
keyFile.Abort(); // close file safely
}

try
{
sbitCount.iVal = oKey.get_BitCount(RSAkey);
oKey.Generate((long) RSAkey, sbitCount);

oKey.Save((long) RSAkey, (LPCTSTR)fNameClient, vmyPassword);

}
catch(CException* pe)
{
AfxMessageBox(_T("Error"));
pe->ReportError();
}

keyFile.Close();
return ;
}

Thank you,
Mariana

SSH Client connection

by Jasmine, Thursday, December 15, 2016, 21:12 (2661 days ago) @ Mariana Cimpoca

Hi Mariana,

this still doesn't help. What is CKeys? I don't recall we are providing 'CKeys' as a class. We provide COM object call Keys, which you somehow wrap in C++ class, but I'm not sure how exactly.

I don't see you ever intiialize it. Are you sure CKeys actually is an object, and it actually calls our methods?

If you can ZIP your sample project you created for testing wodKeys, I'll be happy to check it out.

I just tried our 'ConsoleApp' sample that comes with wodSSHTunnel, and it works without issues. Key is generated and saved.

Here's the code that does the work:

int main(int argc, char *argv[])
{
   IKeysPtr pKeys;

   HRESULT hr = NULL;
   CoInitialize (NULL);

   hr = pKeys.CreateInstance (CLSID_Keys, NULL);
   if (FAILED (hr))
   {
       _com_error comErr (hr);
       printf ("Unable to load wodKeys interface.\nErr #%u: %s", hr, comErr.ErrorMessage () );
       CoUninitialize ();
       return -1;
   }

   SSHKeyTypes ktype = (SSHKeyTypes)0; //RSAKey;

   VARIANT var;
   var.vt = VT_ERROR;
   pKeys->Generate(ktype, var);

   _bstr_t fname = "rsapriv.txt";
   pKeys->Save(ktype, fname, var);


   pKeys.Release();
   CoUninitialize ();


   return 0;
}

I hope this helps!
Jasmine

SSH Client connection

by Mariana Cimpoca, Timisoara, Thursday, December 22, 2016, 16:07 (2654 days ago) @ Jasmine

Hi,

thank you for your help, we solved successfully the keys(private/public) generation.
I have a question related to public key transmission from client to server.
Is there any way of transmitting the public key automatically ?
Some examples would help me.

Thank you,
Mariana

SSH Client connection

by Jasmine, Thursday, December 22, 2016, 16:15 (2654 days ago) @ Mariana Cimpoca

Hi Mariana,

no, unfortunately there isn't any automated way. You must upload/paste to appropriate place on the server, and this depends on server's implementation. Usually it is ~/.ssh/authorized_keys2 file, but it can be on other places too.

I hope this helps!
Jasmine.