From 2ab709c89a260f178e4f9e0e2639cd62940eff53 Mon Sep 17 00:00:00 2001 From: gasbytes Date: Wed, 5 Jun 2024 13:28:30 +0200 Subject: [PATCH] - Platform specific function to correctly set the path for the certificates; - Updated all the examples with it; --- .../wolfSSL-DTLS-PSK-Server.cs | 17 +++++++-- .../wolfSSL-DTLS-Server.cs | 17 +++++++-- .../wolfSSL-Example-IOCallbacks.cs | 17 +++++++-- .../wolfSSL-TLS-Client/wolfSSL-TLS-Client.cs | 27 +++++-------- .../wolfSSL-TLS-PSK-Client.cs | 12 +++++- .../wolfSSL-TLS-PSK-Server.cs | 17 +++++++-- .../wolfSSL-TLS-Server/wolfSSL-TLS-Server.cs | 38 +++++++++---------- .../wolfSSL-TLS-ServerThreaded.cs | 17 +++++++-- wrapper/CSharp/wolfSSL_CSharp/wolfSSL.cs | 20 ++++++++++ 9 files changed, 130 insertions(+), 52 deletions(-) diff --git a/wrapper/CSharp/wolfSSL-DTLS-PSK-Server/wolfSSL-DTLS-PSK-Server.cs b/wrapper/CSharp/wolfSSL-DTLS-PSK-Server/wolfSSL-DTLS-PSK-Server.cs index 6aa9aa542..f1753282b 100644 --- a/wrapper/CSharp/wolfSSL-DTLS-PSK-Server/wolfSSL-DTLS-PSK-Server.cs +++ b/wrapper/CSharp/wolfSSL-DTLS-PSK-Server/wolfSSL-DTLS-PSK-Server.cs @@ -78,9 +78,14 @@ public class wolfSSL_DTLS_PSK_Server 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"); + string fileCert = wolfssl.setPath("server-cert.pem"); + string fileKey = wolfssl.setPath("server-key.pem"); + StringBuilder dhparam = new StringBuilder(wolfssl.setPath("dh2048.pem")); + + if (fileCert == "" || fileKey == "" || dhparam.Length == 0) { + Console.WriteLine("Platform not supported"); + return; + } wolfssl.psk_delegate psk_cb = new wolfssl.psk_delegate(my_psk_server_cb); @@ -106,6 +111,12 @@ public class wolfSSL_DTLS_PSK_Server return; } + if (!File.Exists(dhparam.ToString())) { + Console.WriteLine("Could not find dh file"); + wolfssl.CTX_free(ctx); + return; + } + if (wolfssl.CTX_use_certificate_file(ctx, fileCert, wolfssl.SSL_FILETYPE_PEM) != wolfssl.SUCCESS) { diff --git a/wrapper/CSharp/wolfSSL-DTLS-Server/wolfSSL-DTLS-Server.cs b/wrapper/CSharp/wolfSSL-DTLS-Server/wolfSSL-DTLS-Server.cs index fcbfe6922..5e10a9a93 100644 --- a/wrapper/CSharp/wolfSSL-DTLS-Server/wolfSSL-DTLS-Server.cs +++ b/wrapper/CSharp/wolfSSL-DTLS-Server/wolfSSL-DTLS-Server.cs @@ -58,9 +58,14 @@ public class wolfSSL_DTLS_Server IntPtr ssl; /* These paths should be changed for use */ - string fileCert = @"server-cert.pem"; - string fileKey = @"server-key.pem"; - StringBuilder dhparam = new StringBuilder("dh2048.pem"); + string fileCert = wolfssl.setPath("server-cert.pem"); + string fileKey = wolfssl.setPath(@"server-key.pem"); + StringBuilder dhparam = new StringBuilder(wolfssl.setPath("dh2048.pem")); + + if (fileCert == "" || fileKey == "" || dhparam.Length == 0) { + Console.WriteLine("Platform not supported"); + return; + } StringBuilder buff = new StringBuilder(1024); StringBuilder reply = new StringBuilder("Hello, this is the wolfSSL C# wrapper"); @@ -87,6 +92,12 @@ public class wolfSSL_DTLS_Server return; } + if (!File.Exists(dhparam.ToString())) { + Console.WriteLine("Could not find dh file"); + wolfssl.CTX_free(ctx); + return; + } + if (wolfssl.CTX_use_certificate_file(ctx, fileCert, wolfssl.SSL_FILETYPE_PEM) != wolfssl.SUCCESS) { diff --git a/wrapper/CSharp/wolfSSL-Example-IOCallbacks/wolfSSL-Example-IOCallbacks.cs b/wrapper/CSharp/wolfSSL-Example-IOCallbacks/wolfSSL-Example-IOCallbacks.cs index ac91a9795..77218fd0a 100644 --- a/wrapper/CSharp/wolfSSL-Example-IOCallbacks/wolfSSL-Example-IOCallbacks.cs +++ b/wrapper/CSharp/wolfSSL-Example-IOCallbacks/wolfSSL-Example-IOCallbacks.cs @@ -214,12 +214,17 @@ class wolfSSL_Example_IOCallbacks 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"; + string fileCert = wolfssl.setPath("server-cert.pem"); + string fileKey = wolfssl.setPath("server-key.pem"); + StringBuilder dhparam = new StringBuilder(wolfssl.setPath("dh2048.pem")); + + if (fileCert == "" || fileKey == "" || dhparam.Length == 0) { + Console.WriteLine("Platform not supported"); + return; + } StringBuilder buff = new StringBuilder(1024); StringBuilder reply = new StringBuilder("Hello, this is the wolfSSL C# wrapper"); @@ -242,6 +247,12 @@ class wolfSSL_Example_IOCallbacks return; } + if (!File.Exists(dhparam.ToString())) { + Console.WriteLine("Could not find dh 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"); diff --git a/wrapper/CSharp/wolfSSL-TLS-Client/wolfSSL-TLS-Client.cs b/wrapper/CSharp/wolfSSL-TLS-Client/wolfSSL-TLS-Client.cs index fde1026bc..7cf4c71f4 100644 --- a/wrapper/CSharp/wolfSSL-TLS-Client/wolfSSL-TLS-Client.cs +++ b/wrapper/CSharp/wolfSSL-TLS-Client/wolfSSL-TLS-Client.cs @@ -77,19 +77,6 @@ public class wolfSSL_TLS_Client return -1; } - public static string setPath() { - if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) - { - return @"../../certs/ca-cert.pem"; - } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - { - return @"../../../../certs/ca-cert.pem"; - } else - { - return ""; - } - } - public static void Main(string[] args) { IntPtr ctx; @@ -98,14 +85,14 @@ public class wolfSSL_TLS_Client IntPtr sniHostName; /* These paths should be changed for use */ - string caCert = setPath(); - if (caCert == "") { + string caCert = wolfssl.setPath("ca-cert.pem"); + StringBuilder dhparam = new StringBuilder(wolfssl.setPath("dh2048.pem")); + + if (caCert == "" || dhparam.Length == 0) { Console.WriteLine("Platform not supported."); return; } - StringBuilder dhparam = new StringBuilder("dh2048.pem"); - StringBuilder buff = new StringBuilder(1024); StringBuilder reply = new StringBuilder("Hello, this is the wolfSSL C# wrapper"); @@ -131,6 +118,12 @@ public class wolfSSL_TLS_Client return; } + if (!File.Exists(dhparam.ToString())) { + Console.WriteLine("Could not find dh file"); + wolfssl.CTX_free(ctx); + return; + } + if (wolfssl.CTX_load_verify_locations(ctx, caCert, null) != wolfssl.SUCCESS) { diff --git a/wrapper/CSharp/wolfSSL-TLS-PSK-Client/wolfSSL-TLS-PSK-Client.cs b/wrapper/CSharp/wolfSSL-TLS-PSK-Client/wolfSSL-TLS-PSK-Client.cs index cdc3ef7ca..0f70d72d4 100644 --- a/wrapper/CSharp/wolfSSL-TLS-PSK-Client/wolfSSL-TLS-PSK-Client.cs +++ b/wrapper/CSharp/wolfSSL-TLS-PSK-Client/wolfSSL-TLS-PSK-Client.cs @@ -82,7 +82,11 @@ public class wolfSSL_TLS_PSK_Client wolfssl.psk_client_delegate psk_cb = new wolfssl.psk_client_delegate(my_psk_client_cb); - StringBuilder dhparam = new StringBuilder("dh2048.pem"); + StringBuilder dhparam = new StringBuilder(wolfssl.setPath("dh2048.pem")); + if (dhparam.Length == 0) { + Console.WriteLine("Platform not supported"); + return; + } StringBuilder buff = new StringBuilder(1024); StringBuilder reply = new StringBuilder("Hello, this is the wolfSSL C# client psk wrapper"); @@ -157,6 +161,12 @@ public class wolfSSL_TLS_PSK_Client return; } + if (!File.Exists(dhparam.ToString())) { + Console.WriteLine("Could not find dh file"); + wolfssl.CTX_free(ctx); + return; + } + wolfssl.SetTmpDH_file(ssl, dhparam, wolfssl.SSL_FILETYPE_PEM); if (wolfssl.connect(ssl) != wolfssl.SUCCESS) diff --git a/wrapper/CSharp/wolfSSL-TLS-PSK-Server/wolfSSL-TLS-PSK-Server.cs b/wrapper/CSharp/wolfSSL-TLS-PSK-Server/wolfSSL-TLS-PSK-Server.cs index a46dbd594..a16bb8732 100644 --- a/wrapper/CSharp/wolfSSL-TLS-PSK-Server/wolfSSL-TLS-PSK-Server.cs +++ b/wrapper/CSharp/wolfSSL-TLS-PSK-Server/wolfSSL-TLS-PSK-Server.cs @@ -80,9 +80,14 @@ public class wolfSSL_TLS_PSK_Server 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"); + string fileCert = wolfssl.setPath("server-cert.pem"); + string fileKey = wolfssl.setPath("server-key.pem"); + StringBuilder dhparam = new StringBuilder(wolfssl.setPath("dh2048.pem")); + + if (fileCert == "" || fileKey == "" || dhparam.Length == 0) { + Console.WriteLine("Platform not supported"); + return; + } StringBuilder buff = new StringBuilder(1024); StringBuilder reply = new StringBuilder("Hello, this is the wolfSSL C# wrapper"); @@ -105,6 +110,12 @@ public class wolfSSL_TLS_PSK_Server return; } + if (!File.Exists(dhparam.ToString())) { + Console.WriteLine("Could not find dh 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"); diff --git a/wrapper/CSharp/wolfSSL-TLS-Server/wolfSSL-TLS-Server.cs b/wrapper/CSharp/wolfSSL-TLS-Server/wolfSSL-TLS-Server.cs index c273f6331..2479bb762 100644 --- a/wrapper/CSharp/wolfSSL-TLS-Server/wolfSSL-TLS-Server.cs +++ b/wrapper/CSharp/wolfSSL-TLS-Server/wolfSSL-TLS-Server.cs @@ -80,19 +80,6 @@ public class wolfSSL_TLS_CSHarp return 0; } - public static string setPath(string file) { - if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) - { - return @"../../certs/" + file; - } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - { - return @"../../../../certs/" + file; - } else - { - return ""; - } - } - public static void Main(string[] args) { IntPtr ctx; @@ -101,15 +88,15 @@ public class wolfSSL_TLS_CSHarp IntPtr arg_sni; /* These paths should be changed for use */ - string fileCert = setPath("server-cert.pem"); - string fileKey = setPath("server-key.pem"); - if (fileCert == "" || fileKey == "") { + string fileCert = wolfssl.setPath("server-cert.pem"); + string fileKey = wolfssl.setPath("server-key.pem"); + StringBuilder dh2048Pem = new StringBuilder(wolfssl.setPath("dh2048.pem")); + + if (fileCert == "" || fileKey == "" || dh2048Pem.Length == 0) { Console.WriteLine("Platform not supported."); return; } - StringBuilder dhparam = new StringBuilder("dh2048.pem"); - StringBuilder buff = new StringBuilder(1024); StringBuilder reply = new StringBuilder("Hello, this is the wolfSSL C# wrapper"); @@ -134,6 +121,12 @@ public class wolfSSL_TLS_CSHarp return; } + if (!File.Exists(dhparam.ToString())) { + Console.WriteLine("Could not find dh 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"); @@ -197,7 +190,14 @@ public class wolfSSL_TLS_CSHarp return; } - wolfssl.SetTmpDH_file(ssl, dhparam, wolfssl.SSL_FILETYPE_PEM); + if (wolfssl.SetTmpDH_file(ssl, dh2048Pem, wolfssl.SSL_FILETYPE_PEM) != wolfssl.SUCCESS) + { + Console.WriteLine("Error in setting dh2048Pem"); + Console.WriteLine(wolfssl.get_error(ssl)); + tcp.Stop(); + clean(ssl, ctx); + return; + } if (wolfssl.accept(ssl) != wolfssl.SUCCESS) { diff --git a/wrapper/CSharp/wolfSSL-TLS-ServerThreaded/wolfSSL-TLS-ServerThreaded.cs b/wrapper/CSharp/wolfSSL-TLS-ServerThreaded/wolfSSL-TLS-ServerThreaded.cs index e83784e1f..6cd6982db 100644 --- a/wrapper/CSharp/wolfSSL-TLS-ServerThreaded/wolfSSL-TLS-ServerThreaded.cs +++ b/wrapper/CSharp/wolfSSL-TLS-ServerThreaded/wolfSSL-TLS-ServerThreaded.cs @@ -116,9 +116,14 @@ public class wolfSSL_TLS_ServerThreaded IntPtr ctx; /* These paths should be changed for use */ - string fileCert = @"server-cert.pem"; - string fileKey = @"server-key.pem"; - StringBuilder dhparam = new StringBuilder("dh2048.pem"); + string fileCert = wolfssl.setPath("server-cert.pem"); + string fileKey = wolfssl.setPath("server-key.pem"); + StringBuilder dhparam = new StringBuilder(wolfssl.setPath("dh2048.pem")); + + if (fileCert == "" || fileKey == "" || dhparam.Length == 0) { + Console.WriteLine("Platform not supported"); + return; + } /* example of function used for setting logging */ wolfssl.SetLogging(standard_log); @@ -140,6 +145,12 @@ public class wolfSSL_TLS_ServerThreaded return; } + if (!File.Exists(dhparam.ToString())) { + Console.WriteLine("Could not find dh 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"); diff --git a/wrapper/CSharp/wolfSSL_CSharp/wolfSSL.cs b/wrapper/CSharp/wolfSSL_CSharp/wolfSSL.cs index 3f9d9a17e..7b7ec1e23 100644 --- a/wrapper/CSharp/wolfSSL_CSharp/wolfSSL.cs +++ b/wrapper/CSharp/wolfSSL_CSharp/wolfSSL.cs @@ -485,6 +485,26 @@ namespace wolfSSL.CSharp { } } + /// + /// Utility function used to access the certificates + /// based on the platform. + /// return the platform specific path to the certificate + /// + public static string setPath(string file) { + if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + { + Console.WriteLine("Linux - " + file); + return @"../../certs/" + file; + } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + Console.WriteLine("Windows - " + file); + return @"../../../../certs/" + file; + } else + { + return ""; + } + } + /// /// Call back to allow receiving TLS information