Note that there are some explanatory texts on larger screens.

plurals
  1. POc# vs c++ ssl_connect
    primarykey
    data
    text
    <p>Hello i have a c++ application that uses openssl and winapi sockets, and the code looks like this:</p> <pre><code>sock = socket(AF_INET, SOCK_STREAM, 0); if (connect(my_sock, (struct sockaddr*)&amp;dest_addr, sizeof(dest_addr))==0) { WSAIoctl(my_sock, SIO_KEEPALIVE_VALS, &amp;alive, sizeof(alive),NULL, 0, &amp;dwSize, NULL, NULL); } } ..... SSL_load_error_strings(); SSL_library_init(); ctx = SSL_CTX_new(SSLv23_client_method()); if(!ctx) { return false; } ssl = SSL_new(ctx); SSL_CTX_free(ctx); if(!ssl) { return false; } SSL_set_fd(ssl, (int)sock); //making the socket use ssl mode result = SSL_connect(ssl); </code></pre> <p>Im using the static ssl libraries with this c++ application, which i downloaded from <a href="http://slproweb.com/download/Win32OpenSSL-1_0_1e.exe" rel="nofollow">here</a></p> <p>Everything works fine, and ssl_connect returns 1. But what i need to do is re-write this code using c#. So i tried, and c# code looks like this:</p> <pre><code> [DllImport("ssleay32.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "SSL_read")] public static extern int SSL_Read(IntPtr ssl, ref byte[] buf, int num); [DllImport("ssleay32.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "SSL_write")] public static extern int SSL_Write(IntPtr ssl, byte[] buf, int num); [DllImport("ssleay32.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "SSL_set_fd")] extern public static int SSL_set_fd(IntPtr ssl, int fd); [DllImport("ssleay32.dll", CallingConvention= CallingConvention.Cdecl, EntryPoint = "SSL_CTX_new")] extern public static IntPtr SSL_CTX_new(IntPtr method); [DllImport("ssleay32.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "SSL_new")] extern public static IntPtr SSL_new(IntPtr ctx); [DllImport("ssleay32.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "SSL_connect")] extern public static int SSL_connect(IntPtr ssl); [DllImport("ssleay32.dll",CallingConvention= CallingConvention.Cdecl, EntryPoint = "SSL_load_error_strings")] extern public static void SSL_load_error_strings(); [DllImport("ssleay32.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "SSL_library_init")] extern public static int SSL_library_init(); [DllImport("ssleay32.dll",CallingConvention= CallingConvention.Cdecl, EntryPoint = "SSLeay_add_all_algorithms")] extern public static int SSLeay_add_all_algorithms(); [DllImport("ssleay32.dll",CallingConvention= CallingConvention.Cdecl, EntryPoint = "SSLv23_client_method")] extern public static IntPtr SSLv23_client_method(); [DllImport("ssleay32.dll",CallingConvention= CallingConvention.Cdecl, EntryPoint = "SSLv3_client_method")] extern public static IntPtr SSLv3_client_method(); [DllImport("ssleay32.dll",CallingConvention= CallingConvention.Cdecl, EntryPoint = "SSL_CTX_free")] extern public static void SSL_CTX_free(IntPtr ctx); [DllImport("ssleay32.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "SSL_free")] extern public static void SSL_free(IntPtr ssl); [DllImport("ssleay32.dll",CallingConvention= CallingConvention.Cdecl, EntryPoint = "SSL_get_error")] extern public static int SSL_get_error(IntPtr ssl, int ret); [DllImport("ssleay32.dll",CallingConvention= CallingConvention.Cdecl, EntryPoint = " SSL_CTX_set_mode")] extern public static long SSL_CTX_set_mode(IntPtr ctx, long mode); [DllImport("libeay32", CallingConvention = CallingConvention.Cdecl, EntryPoint = "OPENSSL_add_all_algorithms_noconf")] extern public static void OpenSSL_add_all_algorithms(); [DllImport("libeay32", CallingConvention = CallingConvention.Cdecl, EntryPoint = "ERR_get_error")] extern public static int ERR_get_error(); [DllImport("ssleay32.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "SSL_CTX_ctrl")] public extern static int SSL_CTX_ctrl(IntPtr ctx, int cmd, int arg, IntPtr parg); [DllImport("ssleay32.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "SSLv23_method")] public extern static IntPtr SSLv23_method(); public bool Ssl_Init() { SSL_load_error_strings(); OpenSSL_add_all_algorithms(); SSL_library_init(); IntPtr method = SSLv23_client_method(); IntPtr ctx = SSL_CTX_new(method); if (ctx == IntPtr.Zero) { return false; } _ssl = SSL_new(ctx); if (_ssl == IntPtr.Zero) { return false; } int val = SSL_set_fd(_ssl, this.socket.Handle.ToInt32()); //always returns 1 int result = SSL_connect(_ssl); if(result!=1) return false; SSL_CTX_free(ctx); Ssl_Enabled.Set(); return true; } </code></pre> <p>My c# socket is created like so:</p> <pre><code> socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); this.socket.Connect(host,port); socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, 1000); </code></pre> <p>I always get SSL_Connect = 0 in my c# code, with SSL_get_error() = 5 (error_syscall). So basically my question is, what's might be wrong with using .net socket with openssl? (because c++ application works perfectly with the same kind of code).</p> <p>Update: I tried to use OPENSSL_add_all_algorithms_conf but it seems it doesn't change anything... I'm begging you for help!!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload