diff --git a/wrapper/CSharp/wolfSSL-TLS-Server/wolfSSL-TLS-Server.cs b/wrapper/CSharp/wolfSSL-TLS-Server/wolfSSL-TLS-Server.cs index 7552777ab..5253a5bfa 100644 --- a/wrapper/CSharp/wolfSSL-TLS-Server/wolfSSL-TLS-Server.cs +++ b/wrapper/CSharp/wolfSSL-TLS-Server/wolfSSL-TLS-Server.cs @@ -155,6 +155,7 @@ public class wolfSSL_TLS_CSHarp Console.WriteLine("Started TCP and waiting for a connection"); fd = tcp.AcceptSocket(); + ssl = wolfssl.new_ssl(ctx); if (ssl == IntPtr.Zero) { @@ -208,6 +209,16 @@ public class wolfSSL_TLS_CSHarp return; } + /* get and print sni used by the client */ + if (haveSNI(args)) { + IntPtr data = IntPtr.Zero; + + ushort size = wolfssl.SNI_GetRequest(ssl, 0, ref data); + string dataStr = Marshal.PtrToStringAnsi(data); + Console.WriteLine("(SNI_GetRequest) Size of SNI used by client: " + size); + Console.WriteLine("(SNI_GetRequest) SNI used by client: " + dataStr); + } + /* 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)); @@ -222,6 +233,26 @@ public class wolfSSL_TLS_CSHarp } Console.WriteLine(buff); + /* get and print sni used by the client and also their message */ + if (haveSNI(args)) { + IntPtr result = Marshal.AllocHGlobal(32); + IntPtr inOutSz = Marshal.AllocHGlobal(sizeof(int)); + Marshal.WriteInt32(inOutSz, 32); + + int ret = wolfssl.SNI_GetFromBuffer(buff, 1024, 0, result, inOutSz); + + if (ret != wolfssl.SUCCESS) { + Console.WriteLine("Error on reading SNI from buffer, ret value = " + ret); + tcp.Stop(); + clean(ssl, ctx); + return; + } + + string dataStr = Marshal.PtrToStringAnsi(result); + Console.WriteLine("(SNI_GetFromBuffer) SNI used by client: " + dataStr); + } + + if (wolfssl.write(ssl, reply, reply.Length) != reply.Length) { Console.WriteLine("Error in write"); diff --git a/wrapper/CSharp/wolfSSL_CSharp/wolfSSL.cs b/wrapper/CSharp/wolfSSL_CSharp/wolfSSL.cs index 7b7ec1e23..8895c48f5 100644 --- a/wrapper/CSharp/wolfSSL_CSharp/wolfSSL.cs +++ b/wrapper/CSharp/wolfSSL_CSharp/wolfSSL.cs @@ -330,6 +330,10 @@ namespace wolfSSL.CSharp { private extern static int wolfSSL_CTX_UseSNI(IntPtr ctx, byte type, IntPtr data, ushort size); [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] private extern static int wolfSSL_UseSNI(IntPtr ssl, byte type, IntPtr data, ushort size); + [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] + private extern static ushort wolfSSL_SNI_GetRequest(IntPtr ssl, byte type, ref IntPtr data); + [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] + private extern static int wolfSSL_SNI_GetFromBuffer(StringBuilder clientHello, uint helloSz, byte type, IntPtr sni, IntPtr inOutSz); /******************************** * SSL Structure @@ -1200,6 +1204,29 @@ namespace wolfSSL.CSharp { } } + public static ushort SNI_GetRequest(IntPtr ssl, byte type, ref IntPtr data) + { + try { + GCHandle gch = GCHandle.FromIntPtr(ssl); + ssl_handle handles = (ssl_handle)gch.Target; + + return wolfSSL_SNI_GetRequest(handles.get_ssl(), type, ref data); + } catch (Exception e) { + log(ERROR_LOG, "wolfssl sni get request error: " + e.ToString()); + return ushort.MaxValue; + } + } + + public static int SNI_GetFromBuffer(StringBuilder clientHello, uint helloSz, byte type, IntPtr sni, IntPtr inOutSz) + { + try { + return wolfSSL_SNI_GetFromBuffer(clientHello, helloSz, type, sni, inOutSz); + } catch(Exception e) { + log(ERROR_LOG, "wolfssl sni get from buffer error: " + e.ToString()); + return FAILURE; + } + } + /// /// Set identity hint to use ///