Convert C++ to C# OpenSSL app

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Convert C++ to C# OpenSSL app

OpenSSLGRT

I just got Mr. Sanchez OpenSSL port to PDA (http://karajan.it.uc3m.es/~pervasive/wce_lite_compat/) – and it works great! I have a simple SSL Client in C++. Now I want to convert that to C#. I assume(???) that I would use the two DLLs but libeay32.dll and ssleay32.dll that I now have on the PDA. I know how to use simple dlls from C++ in C# but I am having trouble figuring out how to convert this more complicated program.  Any help would be greatly!!! Appretiated

 

A simple app (which I got a lot from a great source on the web!) in C++ posts vars to a server and works fine – now to get it into C# (I can get the C++ generic code translated OK it is just how do I get at the OpenSSL stuff like for example:  SSL_load_error_strings();

 

#include "stdafx.h"

#include <stdio.h>

#include <memory.h>

#include <winsock2.h>

#include <openssl/ssl.h>

#include <openssl/err.h>

 

int _tmain(int argc, _TCHAR* argv[])

{

      char buff1[65536];

      char buff2[65536];

      char head[1024];

      char tmpstr[256];

 

      //Regular (Windows) socket stuff

      int sock;

      struct sockaddr_in sa;

      WSADATA myWSAData;

 

      //SSL specific stuff

      SSL_METHOD *meth = NULL;

      SSL_CTX *ctx = NULL;

      SSL *ssl = NULL;

 

      //Create POST body.

      strcpy(buff1, "body");

 

      //Create POST header

      char data[40];

      strcpy(data, "var1=test1&var2=test2");

      strcpy(head, "POST /testreceiver/receiver.aspx HTTP/1.0\r\n");

      strcat(head, "Host: myserver.com\r\n");

      strcat(head, "Content-Type: application/x-www-form-urlencoded\r\n");

      strcat(head, "Content-Length: ");

      sprintf(tmpstr, "%d", strlen(data));

      strcat(head, tmpstr);

      strcat(head, "\r\n");

      strcat(head, "Referer: http://dev2/\r\n");

      strcat(head, "\r\n");

      strcat(head, data);

      strcat(head, "\r\n\r\n");

     

      //Connect socket

      WSAStartup(MAKEWORD(2,2), &myWSAData);

      sock = socket(AF_INET, SOCK_STREAM, 0);

      memset (&sa, '\0', sizeof(sa));

      sa.sin_family = AF_INET;

      sa.sin_addr.s_addr = inet_addr("64.140.217.122");

      sa.sin_port = htons(443);

      connect(sock, (struct sockaddr*) &sa, sizeof(sa));

 

      //Establish SSL

      SSL_load_error_strings();

      SSL_library_init();

      meth = TLSv1_client_method();

      ctx = SSL_CTX_new(meth);

      ssl = SSL_new(ctx);

      SSL_set_fd(ssl, sock);

 

      //See section 5 of

//http://www.rtfm.com/openssl-examples/part2.pdf

      //We can get a list of supported ciphers using C:\OpenSSL\bin>openssl ciphers

      SSL_set_cipher_list(ssl, "-a DES-CBC3-SHA");    //TLS_RSA_WITH_3DES_EDE_CBC_SHA

                                                      //see http://www.openssl.org/docs/apps/ciphers.html

 

      SSL_connect(ssl);

 

      //POST

      SSL_write(ssl, head, strlen(head));

 

      memset(buff2, 0x00, sizeof(buff2)); //init recv buffer

      SSL_read(ssl, buff2, sizeof(buff2) - 1); //read server response

      printf("%s\n", buff2);                               

     

      //Clean up

      SSL_shutdown(ssl);

      SSL_free(ssl);

      SSL_CTX_free(ctx);

      ERR_free_strings();

 

      closesocket(sock);

      WSACleanup();

 

      //Keep cmd window open until user hits <return>

      cin.get();

 

      return 0;

}

 

 

 

Reply | Threaded
Open this post in threaded view
|

Re: Convert C++ to C# OpenSSL app

Thomas J. Hruska
OpenSSLGRT wrote:

> I just got Mr. Sanchez OpenSSL port to PDA
> (http://karajan.it.uc3m.es/~pervasive/wce_lite_compat/) - and it works
> great! I have a simple SSL Client in C++. Now I want to convert that to C#.
> I assume(???) that I would use the two DLLs but libeay32.dll and
> ssleay32.dll that I now have on the PDA. I know how to use simple dlls from
> C++ in C# but I am having trouble figuring out how to convert this more
> complicated program.  Any help would be greatly!!! Appretiated
>
>  
>
> A simple app (which I got a lot from a great source on the web!) in C++
> posts vars to a server and works fine - now to get it into C# (I can get the
> C++ generic code translated OK it is just how do I get at the OpenSSL stuff
> like for example:  SSL_load_error_strings();

Look at how PayPal interacts with OpenSSL.  They build an intermediate
unmanaged DLL with C++ that does the actual communication with OpenSSL
and then call the intermediate DLL from C#.  Source code for this is
available in the PayPal SDK.

--
Thomas Hruska
Shining Light Productions

Home of BMP2AVI, Nuclear Vision, ProtoNova, and Win32 OpenSSL.
http://www.slproweb.com/

Ask me about discounts on any Shining Light Productions product!

______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [hidden email]
Automated List Manager                           [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Convert C++ to C# OpenSSL app

Hashim Saleem
One other option is to write a managed wrapper in visual C++ .net using the
IJW (It just works) technology. This way, the resulting wrapper (.net
assembly) could be called from any .net supported language e.g. C#, VB.net,
J# etc.

I can provide an example of this if it is required.

Regards,
Hashim Saleem

----- Original Message -----
From: "Thomas J. Hruska" <[hidden email]>
To: <[hidden email]>
Sent: Sunday, January 22, 2006 8:34 AM
Subject: Re: Convert C++ to C# OpenSSL app


> OpenSSLGRT wrote:
>> I just got Mr. Sanchez OpenSSL port to PDA
>> (http://karajan.it.uc3m.es/~pervasive/wce_lite_compat/) - and it works
>> great! I have a simple SSL Client in C++. Now I want to convert that to
>> C#.
>> I assume(???) that I would use the two DLLs but libeay32.dll and
>> ssleay32.dll that I now have on the PDA. I know how to use simple dlls
>> from
>> C++ in C# but I am having trouble figuring out how to convert this more
>> complicated program.  Any help would be greatly!!! Appretiated
>>
>>  A simple app (which I got a lot from a great source on the web!) in C++
>> posts vars to a server and works fine - now to get it into C# (I can get
>> the
>> C++ generic code translated OK it is just how do I get at the OpenSSL
>> stuff
>> like for example:  SSL_load_error_strings();
>
> Look at how PayPal interacts with OpenSSL.  They build an intermediate
> unmanaged DLL with C++ that does the actual communication with OpenSSL and
> then call the intermediate DLL from C#.  Source code for this is available
> in the PayPal SDK.
>
> --
> Thomas Hruska
> Shining Light Productions
>
> Home of BMP2AVI, Nuclear Vision, ProtoNova, and Win32 OpenSSL.
> http://www.slproweb.com/
>
> Ask me about discounts on any Shining Light Productions product!
>
> ______________________________________________________________________
> OpenSSL Project                                 http://www.openssl.org
> User Support Mailing List                    [hidden email]
> Automated List Manager                           [hidden email]

______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [hidden email]
Automated List Manager                           [hidden email]