From 6f567b58bc0e0bb85beff074f69be50199f90b82 Mon Sep 17 00:00:00 2001 From: gasbytes Date: Sun, 2 Jun 2024 00:01:51 +0200 Subject: [PATCH] completed the examples --- .../wolfSSL-TLS-Client/wolfSSL-TLS-Client.cs | 42 +++++++++++++++++-- .../wolfSSL-TLS-Server/wolfSSL-TLS-Server.cs | 38 +++++++++++++++-- wrapper/CSharp/wolfSSL_CSharp/wolfSSL.cs | 2 + 3 files changed, 74 insertions(+), 8 deletions(-) diff --git a/wrapper/CSharp/wolfSSL-TLS-Client/wolfSSL-TLS-Client.cs b/wrapper/CSharp/wolfSSL-TLS-Client/wolfSSL-TLS-Client.cs index a12c5f599..5a018d85a 100644 --- a/wrapper/CSharp/wolfSSL-TLS-Client/wolfSSL-TLS-Client.cs +++ b/wrapper/CSharp/wolfSSL-TLS-Client/wolfSSL-TLS-Client.cs @@ -19,7 +19,6 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA */ - using System; using System.Runtime.InteropServices; using System.Text; @@ -60,11 +59,32 @@ public class wolfSSL_TLS_Client return preverify; } + /// + /// Checks if the SNI option was enabled via command line. + /// Must be enabled with ./configure --enable-sni when configuring + /// wolfSSL. + /// Parameters passed via command line + /// + private static bool haveSNI(string[] args) + { + if (args != null && args.Length == 2 && args[0] == "-S") + { + Console.WriteLine("SNI IS: ON"); + return true; + } + else { + Console.WriteLine("SNI IS: OFF"); + return false; + } + } + + public static void Main(string[] args) { IntPtr ctx; IntPtr ssl; Socket tcp; + IntPtr sniHostName; /* These paths should be changed for use */ string caCert = @"ca-cert.pem"; @@ -78,7 +98,6 @@ public class wolfSSL_TLS_Client wolfssl.Init(); - Console.WriteLine("Calling ctx Init from wolfSSL"); ctx = wolfssl.CTX_new(wolfssl.usev23_client()); if (ctx == IntPtr.Zero) @@ -88,7 +107,6 @@ public class wolfSSL_TLS_Client } Console.WriteLine("Finished init of ctx .... now load in CA"); - if (!File.Exists(caCert)) { Console.WriteLine("Could not find CA cert file"); @@ -96,11 +114,27 @@ public class wolfSSL_TLS_Client return; } - if (wolfssl.CTX_load_verify_locations(ctx, caCert, null) != wolfssl.SUCCESS) { Console.WriteLine("Error loading CA cert"); + wolfssl.CTX_free(ctx); + return; + } + + if (haveSNI(args)) + { + string sniHostNameString = args[1].Trim(); + sniHostName = Marshal.StringToHGlobalAnsi(sniHostNameString); + + ushort size = (ushort)sniHostNameString.Length; + + if (wolfssl.CTX_UseSNI(ctx, (byte)wolfssl.WOLFSSL_SNI_HOST_NAME, sniHostName, size) != wolfssl.SUCCESS) + { + Console.WriteLine("UseSNI failed"); + wolfssl.CTX_free(ctx); + return; + } } StringBuilder ciphers = new StringBuilder(new String(' ', 4096)); diff --git a/wrapper/CSharp/wolfSSL-TLS-Server/wolfSSL-TLS-Server.cs b/wrapper/CSharp/wolfSSL-TLS-Server/wolfSSL-TLS-Server.cs index 12217dc07..140d4d1f2 100644 --- a/wrapper/CSharp/wolfSSL-TLS-Server/wolfSSL-TLS-Server.cs +++ b/wrapper/CSharp/wolfSSL-TLS-Server/wolfSSL-TLS-Server.cs @@ -19,9 +19,6 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA */ - - - using System; using System.Runtime.InteropServices; using System.Text; @@ -50,6 +47,26 @@ public class wolfSSL_TLS_CSHarp wolfssl.Cleanup(); } + /// + /// Checks if the SNI option was enabled via command line. + /// Must be enabled with ./configure --enable-sni when configuring + /// wolfSSL. + /// Parameters passed via command line + /// + private static bool haveSNI(string[] args) + { + if (args != null && args.Length == 2 && args[0] == "-S") + { + Console.WriteLine("SNI IS: ON"); + return true; + } + else { + Console.WriteLine("SNI IS: OFF"); + return false; + } + } + + public static void Main(string[] args) { @@ -70,7 +87,6 @@ public class wolfSSL_TLS_CSHarp wolfssl.Init(); - Console.WriteLine("Calling ctx Init from wolfSSL"); ctx = wolfssl.CTX_new(wolfssl.usev23_server()); if (ctx == IntPtr.Zero) @@ -101,6 +117,20 @@ public class wolfSSL_TLS_CSHarp return; } + if (haveSNI(args)) + { + string sniHostNameString = args[1].Trim(); + sniHostName = Marshal.StringToHGlobalAnsi(sniHostNameString); + + ushort size = (ushort)sniHostNameString.Length; + + if (wolfssl.CTX_UseSNI(ctx, (byte)wolfssl.WOLFSSL_SNI_HOST_NAME, sniHostName, size) != wolfssl.SUCCESS) + { + Console.WriteLine("UseSNI failed"); + wolfssl.CTX_free(ctx); + return; + } + } StringBuilder ciphers = new StringBuilder(new String(' ', 4096)); wolfssl.get_ciphers(ciphers, 4096); diff --git a/wrapper/CSharp/wolfSSL_CSharp/wolfSSL.cs b/wrapper/CSharp/wolfSSL_CSharp/wolfSSL.cs index 3e78da76e..8377419c4 100644 --- a/wrapper/CSharp/wolfSSL_CSharp/wolfSSL.cs +++ b/wrapper/CSharp/wolfSSL_CSharp/wolfSSL.cs @@ -459,6 +459,8 @@ namespace wolfSSL.CSharp { public static readonly int SUCCESS = 1; public static readonly int FAILURE = 0; + public static readonly int WOLFSSL_SNI_HOST_NAME = 0; + public static readonly int WOLFSSL_SNI_HOST_NAME_OUTER = 0; private static IntPtr unwrap_ctx(IntPtr ctx)