public static void Main(string[] args) { IntPtr ctx; IntPtr ssl; Socket fd; wolfssl.psk_delegate psk_cb = new wolfssl.psk_delegate(my_psk_server_cb); /* These paths should be changed according to use */ string fileCert = @"server-cert.pem"; string fileKey = @"server-key.pem"; StringBuilder dhparam = new StringBuilder("dh2048.pem"); StringBuilder buff = new StringBuilder(1024); StringBuilder reply = new StringBuilder("Hello, this is the wolfSSL C# wrapper"); wolfssl.Init(); Console.WriteLine("Calling ctx Init from wolfSSL"); ctx = wolfssl.CTX_new(wolfssl.useTLSv1_2_server()); if (ctx == IntPtr.Zero) { Console.WriteLine("Error creating ctx structure"); return; } Console.WriteLine("Finished init of ctx .... now load in cert and key"); if (!File.Exists(fileCert) || !File.Exists(fileKey)) { Console.WriteLine("Could not find cert or key file"); wolfssl.CTX_free(ctx); return; } if (wolfssl.CTX_use_certificate_file(ctx, fileCert, wolfssl.SSL_FILETYPE_PEM) != wolfssl.SUCCESS) { Console.WriteLine("Error in setting cert file"); wolfssl.CTX_free(ctx); return; } if (wolfssl.CTX_use_PrivateKey_file(ctx, fileKey, wolfssl.SSL_FILETYPE_PEM) != wolfssl.SUCCESS) { Console.WriteLine("Error in setting key file"); wolfssl.CTX_free(ctx); return; } StringBuilder ciphers = new StringBuilder(new String(' ', 4096)); wolfssl.get_ciphers(ciphers, 4096); Console.WriteLine("Ciphers : " + ciphers.ToString()); short minDhKey = 128; wolfssl.CTX_SetMinDhKey_Sz(ctx, minDhKey); Console.Write("Setting cipher suite to "); /* In order to use static PSK build wolfSSL with the preprocessor flag WOLFSSL_STATIC_PSK */ StringBuilder set_cipher = new StringBuilder("DHE-PSK-AES128-CBC-SHA256"); Console.WriteLine(set_cipher); if (wolfssl.CTX_set_cipher_list(ctx, set_cipher) != wolfssl.SUCCESS) { Console.WriteLine("Failed to set cipher suite"); return; } /* Test psk use with DHE */ StringBuilder hint = new StringBuilder("cyassl server"); if (wolfssl.CTX_use_psk_identity_hint(ctx, hint) != wolfssl.SUCCESS) { Console.WriteLine("Error setting hint"); wolfssl.CTX_free(ctx); return; } wolfssl.CTX_set_psk_server_callback(ctx, psk_cb); /* set up TCP socket */ IPAddress ip = IPAddress.Parse("0.0.0.0"); //bind to any TcpListener tcp = new TcpListener(ip, 11111); tcp.Start(); Console.WriteLine("Started TCP and waiting for a connection"); fd = tcp.AcceptSocket(); ssl = wolfssl.new_ssl(ctx); if (ssl == IntPtr.Zero) { Console.WriteLine("Error creating ssl object"); tcp.Stop(); wolfssl.CTX_free(ctx); return; } Console.WriteLine("Connection made wolfSSL_accept "); if (wolfssl.set_fd(ssl, fd) != wolfssl.SUCCESS) { /* get and print out the error */ Console.Write(wolfssl.get_error(ssl)); tcp.Stop(); clean(ssl, ctx); return; } wolfssl.SetTmpDH_file(ssl, dhparam, wolfssl.SSL_FILETYPE_PEM); if (wolfssl.accept(ssl) != wolfssl.SUCCESS) { /* get and print out the error */ Console.Write(wolfssl.get_error(ssl)); tcp.Stop(); clean(ssl, ctx); return; } /* print out results of TLS/SSL accept */ Console.WriteLine("SSL version is " + wolfssl.get_version(ssl)); Console.WriteLine("SSL cipher suite is " + wolfssl.get_current_cipher(ssl)); /* read and print out the message then reply */ if (wolfssl.read(ssl, buff, 1023) < 0) { Console.WriteLine("Error in read"); tcp.Stop(); clean(ssl, ctx); return; } Console.WriteLine(buff); if (wolfssl.write(ssl, reply, reply.Length) != reply.Length) { Console.WriteLine("Error in write"); tcp.Stop(); clean(ssl, ctx); return; } wolfssl.shutdown(ssl); fd.Close(); tcp.Stop(); clean(ssl, ctx); }
public static void Main(string[] args) { IntPtr ctx; IntPtr ssl; wolfssl.loggingCb loggingCb = Log; wolfssl.SetLogging(loggingCb); wolfssl.psk_delegate psk_cb = new wolfssl.psk_delegate(my_psk_server_cb); byte[] buff = new byte[1024]; byte[] stubReply = new byte[] { 0xe1, 0x6f, 0x80, 0xff, 0xbe, 0xdc, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02 }; wolfssl.Init(); Console.WriteLine("Calling ctx Init from wolfSSL"); ctx = wolfssl.CTX_dtls_new(wolfssl.useDTLSv1_2_server()); if (ctx == IntPtr.Zero) { Console.WriteLine("Error creating ctx structure"); return; } Console.WriteLine("Finished init of ctx .... now load in cert and key"); /* Test psk use with DHE */ StringBuilder hint = new StringBuilder("PROJECT_76"); //Hardcoded hint in Fo76 if (wolfssl.CTX_use_psk_identity_hint(ctx, hint) != wolfssl.SUCCESS) { Console.WriteLine("Error setting hint"); wolfssl.CTX_free(ctx); return; } wolfssl.CTX_set_psk_server_callback(ctx, psk_cb); Console.Write("Setting cipher suite to "); StringBuilder set_cipher = new StringBuilder("TLS_PSK_WITH_AES_128_GCM_SHA256"); Console.WriteLine(set_cipher); if (wolfssl.CTX_set_cipher_list(ctx, set_cipher) != wolfssl.SUCCESS) { Console.WriteLine("Failed to set cipher suite"); wolfssl.CTX_free(ctx); return; } IPAddress ip = IPAddress.Parse("127.0.0.1"); UdpClient udp = new UdpClient(3001); IPEndPoint ep = new IPEndPoint(ip, 3001); Console.WriteLine("Started UDP and waiting for a connection"); ssl = wolfssl.new_ssl(ctx); if (ssl == IntPtr.Zero) { Console.WriteLine("Error creating ssl object"); udp.Close(); wolfssl.CTX_free(ctx); return; } if (wolfssl.set_dtls_fd(ssl, udp, ep) != wolfssl.SUCCESS) { Console.WriteLine(wolfssl.get_error(ssl)); udp.Close(); clean(ssl, ctx); return; } if (wolfssl.accept(ssl) != wolfssl.SUCCESS) { Console.WriteLine(wolfssl.get_error(ssl)); udp.Close(); clean(ssl, ctx); return; } /* print out results of TLS/SSL accept */ Console.WriteLine("SSL version is " + wolfssl.get_version(ssl)); Console.WriteLine("SSL cipher suite is " + wolfssl.get_current_cipher(ssl)); /* get connection information and print ip - port */ wolfssl.DTLS_con con = wolfssl.get_dtls_fd(ssl); Console.Write("Connected to ip "); Console.Write(con.ep.Address.ToString()); Console.Write(" on port "); Console.WriteLine(con.ep.Port.ToString()); /* read information sent and send a reply */ if (wolfssl.read(ssl, buff, 1023) < 0) { Console.WriteLine("Error reading message"); Console.WriteLine(wolfssl.get_error(ssl)); udp.Close(); clean(ssl, ctx); return; } Console.WriteLine(ByteArrayToString(buff)); if (wolfssl.write(ssl, stubReply, stubReply.Length) != stubReply.Length) { Console.WriteLine("Error writing message"); Console.WriteLine(wolfssl.get_error(ssl)); udp.Close(); clean(ssl, ctx); return; } Console.WriteLine("At the end freeing stuff"); wolfssl.shutdown(ssl); udp.Close(); clean(ssl, ctx); }
static void Main(string[] args) { IntPtr ctx; IntPtr ssl; Socket fd; wolfssl.psk_delegate psk_cb = new wolfssl.psk_delegate(my_psk_server_cb); /* These paths should be changed according to use */ string fileCert = @"server-cert.pem"; string fileKey = @"server-key.pem"; StringBuilder buff = new StringBuilder(1024); StringBuilder reply = new StringBuilder("Hello, this is the wolfSSL C# wrapper"); wolfssl.Init(); Console.WriteLine("Calling ctx Init from wolfSSL"); ctx = wolfssl.CTX_new(wolfssl.useTLSv1_2_server()); if (ctx == IntPtr.Zero) { Console.WriteLine("Error creating ctx structure"); return; } Console.WriteLine("Finished init of ctx .... now load in cert and key"); if (!File.Exists(fileCert) || !File.Exists(fileKey)) { Console.WriteLine("Could not find cert or key file"); wolfssl.CTX_free(ctx); return; } if (wolfssl.CTX_use_certificate_file(ctx, fileCert, wolfssl.SSL_FILETYPE_PEM) != wolfssl.SUCCESS) { Console.WriteLine("Error in setting cert file"); wolfssl.CTX_free(ctx); return; } if (wolfssl.CTX_use_PrivateKey_file(ctx, fileKey, wolfssl.SSL_FILETYPE_PEM) != wolfssl.SUCCESS) { Console.WriteLine("Error in setting key file"); wolfssl.CTX_free(ctx); return; } StringBuilder ciphers = new StringBuilder(new String(' ', 4096)); wolfssl.get_ciphers(ciphers, 4096); Console.WriteLine("Ciphers : " + ciphers.ToString()); Console.Write("Setting cipher suite to "); /* To use static PSK build wolfSSL with WOLFSSL_STATIC_PSK preprocessor flag */ StringBuilder set_cipher = new StringBuilder("PSK-AES128-CBC-SHA256"); Console.WriteLine(set_cipher); if (wolfssl.CTX_set_cipher_list(ctx, set_cipher) != wolfssl.SUCCESS) { Console.WriteLine("Failed to set cipher suite"); Console.WriteLine("If using static PSK make sure wolfSSL was built with preprocessor flag WOLFSSL_STATIC_PSK"); wolfssl.CTX_free(ctx); return; } /* Test psk use */ StringBuilder hint = new StringBuilder("cyassl server"); if (wolfssl.CTX_use_psk_identity_hint(ctx, hint) != wolfssl.SUCCESS) { Console.WriteLine("Error setting hint"); return; } wolfssl.CTX_set_psk_server_callback(ctx, psk_cb); /* Set using custom IO callbacks delegate memory is allocated when calling SetIO**** function and freed with ctx free */ wolfssl.SetIORecv(ctx, new wolfssl.CallbackIORecv_delegate(wolfSSLCbIORecv)); wolfssl.SetIOSend(ctx, new wolfssl.CallbackIOSend_delegate(wolfSSLCbIOSend)); /* set up TCP socket */ IPAddress ip = IPAddress.Parse("0.0.0.0"); //bind to any TcpListener tcp = new TcpListener(ip, 11111); tcp.Start(); Console.WriteLine("Started TCP and waiting for a connection"); fd = tcp.AcceptSocket(); ssl = wolfssl.new_ssl(ctx); Console.WriteLine("Connection made wolfSSL_accept "); if (wolfssl.set_fd(ssl, fd) != wolfssl.SUCCESS) { /* get and print out the error */ Console.Write(wolfssl.get_error(ssl)); tcp.Stop(); clean(ssl, ctx); return; } if (wolfssl.accept(ssl) != wolfssl.SUCCESS) { /* get and print out the error */ Console.Write(wolfssl.get_error(ssl)); tcp.Stop(); clean(ssl, ctx); return; } /* print out results of TLS/SSL accept */ Console.WriteLine("SSL version is " + wolfssl.get_version(ssl)); Console.WriteLine("SSL cipher suite is " + wolfssl.get_current_cipher(ssl)); /* read and print out the message then reply */ if (wolfssl.read(ssl, buff, 1023) < 0) { Console.WriteLine("Error in read"); tcp.Stop(); clean(ssl, ctx); return; } Console.WriteLine(buff); if (wolfssl.write(ssl, reply, reply.Length) != reply.Length) { Console.WriteLine("Error in write"); tcp.Stop(); clean(ssl, ctx); return; } wolfssl.shutdown(ssl); fd.Close(); tcp.Stop(); clean(ssl, ctx); }
static void Main(string[] args) { IntPtr ctx; IntPtr ssl; Socket fd; wolfssl.psk_delegate psk_cb = new wolfssl.psk_delegate(my_psk_server_cb); wolfssl.CallbackVerify_delegate verify_cb = new wolfssl.CallbackVerify_delegate(my_verify_cb); /* These paths should be changed according to use */ string fileCert = @"server-cert.pem"; string fileKey = @"server-key.pem"; StringBuilder buff = new StringBuilder(1024); StringBuilder reply = new StringBuilder("Hello, this is the wolfSSL C# wrapper"); wolfssl.Init(); Console.WriteLine("Calling ctx Init from wolfSSL"); ctx = wolfssl.CTX_new(wolfssl.useTLSv1_2_server()); if (ctx == IntPtr.Zero) { Console.WriteLine("Error creating ctx structure"); return; } Console.WriteLine("Finished init of ctx .... now load in cert and key"); if (!File.Exists(fileCert) || !File.Exists(fileKey)) { Console.WriteLine("Could not find cert or key file"); wolfssl.CTX_free(ctx); return; } if (wolfssl.CTX_use_certificate_file(ctx, fileCert, wolfssl.SSL_FILETYPE_PEM) != wolfssl.SUCCESS) { Console.WriteLine("Error in setting cert file"); wolfssl.CTX_free(ctx); return; } if (wolfssl.CTX_use_PrivateKey_file(ctx, fileKey, wolfssl.SSL_FILETYPE_PEM) != wolfssl.SUCCESS) { Console.WriteLine("Error in setting key file"); wolfssl.CTX_free(ctx); return; } wolfssl.CTX_set_verify(ctx, wolfssl.SSL_VERIFY_PEER, verify_cb); /* Set using custom IO callbacks * delegate memory is allocated when calling SetIO**** function and freed with ctx free */ wolfssl.SetIORecv(ctx, new wolfssl.CallbackIORecv_delegate(wolfSSLCbIORecv)); wolfssl.SetIOSend(ctx, new wolfssl.CallbackIOSend_delegate(wolfSSLCbIOSend)); /* set up TCP socket */ IPAddress ip = IPAddress.Parse("0.0.0.0"); //bind to any TcpListener tcp = new TcpListener(ip, 11111); tcp.Start(); Console.WriteLine("Started TCP and waiting for a connection"); fd = tcp.AcceptSocket(); ssl = wolfssl.new_ssl(ctx); Console.WriteLine("Connection made wolfSSL_accept "); if (wolfssl.set_fd(ssl, fd) != wolfssl.SUCCESS) { /* get and print out the error */ Console.WriteLine(wolfssl.get_error(ssl)); tcp.Stop(); clean(ssl, ctx); return; } if (wolfssl.accept(ssl) != wolfssl.SUCCESS) { /* get and print out the error */ Console.WriteLine(wolfssl.get_error(ssl)); tcp.Stop(); clean(ssl, ctx); return; } /* print out results of TLS/SSL accept */ Console.WriteLine("SSL version is " + wolfssl.get_version(ssl)); Console.WriteLine("SSL cipher suite is " + wolfssl.get_current_cipher(ssl)); /* read and print out the message then reply */ if (wolfssl.read(ssl, buff, 1023) < 0) { Console.WriteLine("Error in read"); tcp.Stop(); clean(ssl, ctx); return; } Console.WriteLine(buff); if (wolfssl.write(ssl, reply, reply.Length) != reply.Length) { Console.WriteLine("Error in write"); tcp.Stop(); clean(ssl, ctx); return; } wolfssl.shutdown(ssl); fd.Close(); tcp.Stop(); clean(ssl, ctx); }
public static void Main(string[] args) { IntPtr ctx; IntPtr ssl; /* These paths should be changed according to use */ string fileCert = @"server-cert.pem"; string fileKey = @"server-key.pem"; StringBuilder dhparam = new StringBuilder("dh2048.pem"); wolfssl.psk_delegate psk_cb = new wolfssl.psk_delegate(my_psk_server_cb); StringBuilder buff = new StringBuilder(1024); StringBuilder reply = new StringBuilder("Hello, this is the wolfSSL C# wrapper"); wolfssl.Init(); Console.WriteLine("Calling ctx Init from wolfSSL"); ctx = wolfssl.CTX_dtls_new(wolfssl.useDTLSv1_2_server()); if (ctx == IntPtr.Zero) { Console.WriteLine("Error creating ctx structure"); return; } Console.WriteLine("Finished init of ctx .... now load in cert and key"); if (!File.Exists(fileCert) || !File.Exists(fileKey)) { Console.WriteLine("Could not find cert or key file"); wolfssl.CTX_free(ctx); return; } if (wolfssl.CTX_use_certificate_file(ctx, fileCert, wolfssl.SSL_FILETYPE_PEM) != wolfssl.SUCCESS) { Console.WriteLine("Error setting cert file"); wolfssl.CTX_free(ctx); return; } if (wolfssl.CTX_use_PrivateKey_file(ctx, fileKey, wolfssl.SSL_FILETYPE_PEM) != wolfssl.SUCCESS) { Console.WriteLine("Error setting key file"); wolfssl.CTX_free(ctx); return; } /* Test psk use with DHE */ StringBuilder hint = new StringBuilder("cyassl server"); if (wolfssl.CTX_use_psk_identity_hint(ctx, hint) != wolfssl.SUCCESS) { Console.WriteLine("Error setting hint"); wolfssl.CTX_free(ctx); return; } wolfssl.CTX_set_psk_server_callback(ctx, psk_cb); short minDhKey = 128; wolfssl.CTX_SetMinDhKey_Sz(ctx, minDhKey); Console.Write("Setting cipher suite to "); StringBuilder set_cipher = new StringBuilder("DHE-PSK-AES128-CBC-SHA256"); Console.WriteLine(set_cipher); if (wolfssl.CTX_set_cipher_list(ctx, set_cipher) != wolfssl.SUCCESS) { Console.WriteLine("Failed to set cipher suite"); wolfssl.CTX_free(ctx); return; } IPAddress ip = IPAddress.Parse("0.0.0.0"); UdpClient udp = new UdpClient(11111); IPEndPoint ep = new IPEndPoint(ip, 11111); Console.WriteLine("Started UDP and waiting for a connection"); ssl = wolfssl.new_ssl(ctx); if (ssl == IntPtr.Zero) { Console.WriteLine("Error creating ssl object"); udp.Close(); wolfssl.CTX_free(ctx); return; } if (wolfssl.SetTmpDH_file(ssl, dhparam, wolfssl.SSL_FILETYPE_PEM) != wolfssl.SUCCESS) { Console.WriteLine("Error in setting dhparam"); Console.WriteLine(wolfssl.get_error(ssl)); udp.Close(); clean(ssl, ctx); return; } if (wolfssl.set_dtls_fd(ssl, udp, ep) != wolfssl.SUCCESS) { Console.WriteLine(wolfssl.get_error(ssl)); udp.Close(); clean(ssl, ctx); return; } if (wolfssl.accept(ssl) != wolfssl.SUCCESS) { Console.WriteLine(wolfssl.get_error(ssl)); udp.Close(); clean(ssl, ctx); return; } /* print out results of TLS/SSL accept */ Console.WriteLine("SSL version is " + wolfssl.get_version(ssl)); Console.WriteLine("SSL cipher suite is " + wolfssl.get_current_cipher(ssl)); /* get connection information and print ip - port */ wolfssl.DTLS_con con = wolfssl.get_dtls_fd(ssl); Console.Write("Connected to ip "); Console.Write(con.ep.Address.ToString()); Console.Write(" on port "); Console.WriteLine(con.ep.Port.ToString()); /* read information sent and send a reply */ if (wolfssl.read(ssl, buff, 1023) < 0) { Console.WriteLine("Error reading message"); Console.WriteLine(wolfssl.get_error(ssl)); udp.Close(); clean(ssl, ctx); return; } Console.WriteLine(buff); if (wolfssl.write(ssl, reply, reply.Length) != reply.Length) { Console.WriteLine("Error writing message"); Console.WriteLine(wolfssl.get_error(ssl)); udp.Close(); clean(ssl, ctx); return; } Console.WriteLine("At the end freeing stuff"); wolfssl.shutdown(ssl); udp.Close(); clean(ssl, ctx); }