- Platform specific function to correctly set the path for the certificates;

- Updated all the examples with it;
pull/7610/head
gasbytes 2024-06-05 13:28:30 +02:00
parent 6cb97a7262
commit 2ab709c89a
9 changed files with 130 additions and 52 deletions

View File

@ -78,9 +78,14 @@ public class wolfSSL_DTLS_PSK_Server
IntPtr ssl; IntPtr ssl;
/* These paths should be changed according to use */ /* These paths should be changed according to use */
string fileCert = @"server-cert.pem"; string fileCert = wolfssl.setPath("server-cert.pem");
string fileKey = @"server-key.pem"; string fileKey = wolfssl.setPath("server-key.pem");
StringBuilder dhparam = new StringBuilder("dh2048.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); wolfssl.psk_delegate psk_cb = new wolfssl.psk_delegate(my_psk_server_cb);
@ -106,6 +111,12 @@ public class wolfSSL_DTLS_PSK_Server
return; 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) if (wolfssl.CTX_use_certificate_file(ctx, fileCert, wolfssl.SSL_FILETYPE_PEM) != wolfssl.SUCCESS)
{ {

View File

@ -58,9 +58,14 @@ public class wolfSSL_DTLS_Server
IntPtr ssl; IntPtr ssl;
/* These paths should be changed for use */ /* These paths should be changed for use */
string fileCert = @"server-cert.pem"; string fileCert = wolfssl.setPath("server-cert.pem");
string fileKey = @"server-key.pem"; string fileKey = wolfssl.setPath(@"server-key.pem");
StringBuilder dhparam = new StringBuilder("dh2048.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 buff = new StringBuilder(1024);
StringBuilder reply = new StringBuilder("Hello, this is the wolfSSL C# wrapper"); StringBuilder reply = new StringBuilder("Hello, this is the wolfSSL C# wrapper");
@ -87,6 +92,12 @@ public class wolfSSL_DTLS_Server
return; 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) if (wolfssl.CTX_use_certificate_file(ctx, fileCert, wolfssl.SSL_FILETYPE_PEM) != wolfssl.SUCCESS)
{ {

View File

@ -214,12 +214,17 @@ class wolfSSL_Example_IOCallbacks
IntPtr ssl; IntPtr ssl;
Socket fd; 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); wolfssl.CallbackVerify_delegate verify_cb = new wolfssl.CallbackVerify_delegate(my_verify_cb);
/* These paths should be changed according to use */ /* These paths should be changed according to use */
string fileCert = @"server-cert.pem"; string fileCert = wolfssl.setPath("server-cert.pem");
string fileKey = @"server-key.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 buff = new StringBuilder(1024);
StringBuilder reply = new StringBuilder("Hello, this is the wolfSSL C# wrapper"); StringBuilder reply = new StringBuilder("Hello, this is the wolfSSL C# wrapper");
@ -242,6 +247,12 @@ class wolfSSL_Example_IOCallbacks
return; 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) if (wolfssl.CTX_use_certificate_file(ctx, fileCert, wolfssl.SSL_FILETYPE_PEM) != wolfssl.SUCCESS)
{ {
Console.WriteLine("Error in setting cert file"); Console.WriteLine("Error in setting cert file");

View File

@ -77,19 +77,6 @@ public class wolfSSL_TLS_Client
return -1; 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) public static void Main(string[] args)
{ {
IntPtr ctx; IntPtr ctx;
@ -98,14 +85,14 @@ public class wolfSSL_TLS_Client
IntPtr sniHostName; IntPtr sniHostName;
/* These paths should be changed for use */ /* These paths should be changed for use */
string caCert = setPath(); string caCert = wolfssl.setPath("ca-cert.pem");
if (caCert == "") { StringBuilder dhparam = new StringBuilder(wolfssl.setPath("dh2048.pem"));
if (caCert == "" || dhparam.Length == 0) {
Console.WriteLine("Platform not supported."); Console.WriteLine("Platform not supported.");
return; return;
} }
StringBuilder dhparam = new StringBuilder("dh2048.pem");
StringBuilder buff = new StringBuilder(1024); StringBuilder buff = new StringBuilder(1024);
StringBuilder reply = new StringBuilder("Hello, this is the wolfSSL C# wrapper"); StringBuilder reply = new StringBuilder("Hello, this is the wolfSSL C# wrapper");
@ -131,6 +118,12 @@ public class wolfSSL_TLS_Client
return; 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) if (wolfssl.CTX_load_verify_locations(ctx, caCert, null)
!= wolfssl.SUCCESS) != wolfssl.SUCCESS)
{ {

View File

@ -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); 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 buff = new StringBuilder(1024);
StringBuilder reply = new StringBuilder("Hello, this is the wolfSSL C# client psk wrapper"); StringBuilder reply = new StringBuilder("Hello, this is the wolfSSL C# client psk wrapper");
@ -157,6 +161,12 @@ public class wolfSSL_TLS_PSK_Client
return; 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); wolfssl.SetTmpDH_file(ssl, dhparam, wolfssl.SSL_FILETYPE_PEM);
if (wolfssl.connect(ssl) != wolfssl.SUCCESS) if (wolfssl.connect(ssl) != wolfssl.SUCCESS)

View File

@ -80,9 +80,14 @@ public class wolfSSL_TLS_PSK_Server
wolfssl.psk_delegate psk_cb = new wolfssl.psk_delegate(my_psk_server_cb); wolfssl.psk_delegate psk_cb = new wolfssl.psk_delegate(my_psk_server_cb);
/* These paths should be changed according to use */ /* These paths should be changed according to use */
string fileCert = @"server-cert.pem"; string fileCert = wolfssl.setPath("server-cert.pem");
string fileKey = @"server-key.pem"; string fileKey = wolfssl.setPath("server-key.pem");
StringBuilder dhparam = new StringBuilder("dh2048.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 buff = new StringBuilder(1024);
StringBuilder reply = new StringBuilder("Hello, this is the wolfSSL C# wrapper"); StringBuilder reply = new StringBuilder("Hello, this is the wolfSSL C# wrapper");
@ -105,6 +110,12 @@ public class wolfSSL_TLS_PSK_Server
return; 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) if (wolfssl.CTX_use_certificate_file(ctx, fileCert, wolfssl.SSL_FILETYPE_PEM) != wolfssl.SUCCESS)
{ {
Console.WriteLine("Error in setting cert file"); Console.WriteLine("Error in setting cert file");

View File

@ -80,19 +80,6 @@ public class wolfSSL_TLS_CSHarp
return 0; 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) public static void Main(string[] args)
{ {
IntPtr ctx; IntPtr ctx;
@ -101,15 +88,15 @@ public class wolfSSL_TLS_CSHarp
IntPtr arg_sni; IntPtr arg_sni;
/* These paths should be changed for use */ /* These paths should be changed for use */
string fileCert = setPath("server-cert.pem"); string fileCert = wolfssl.setPath("server-cert.pem");
string fileKey = setPath("server-key.pem"); string fileKey = wolfssl.setPath("server-key.pem");
if (fileCert == "" || fileKey == "") { StringBuilder dh2048Pem = new StringBuilder(wolfssl.setPath("dh2048.pem"));
if (fileCert == "" || fileKey == "" || dh2048Pem.Length == 0) {
Console.WriteLine("Platform not supported."); Console.WriteLine("Platform not supported.");
return; return;
} }
StringBuilder dhparam = new StringBuilder("dh2048.pem");
StringBuilder buff = new StringBuilder(1024); StringBuilder buff = new StringBuilder(1024);
StringBuilder reply = new StringBuilder("Hello, this is the wolfSSL C# wrapper"); StringBuilder reply = new StringBuilder("Hello, this is the wolfSSL C# wrapper");
@ -134,6 +121,12 @@ public class wolfSSL_TLS_CSHarp
return; 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) if (wolfssl.CTX_use_certificate_file(ctx, fileCert, wolfssl.SSL_FILETYPE_PEM) != wolfssl.SUCCESS)
{ {
Console.WriteLine("Error in setting cert file"); Console.WriteLine("Error in setting cert file");
@ -197,7 +190,14 @@ public class wolfSSL_TLS_CSHarp
return; 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) if (wolfssl.accept(ssl) != wolfssl.SUCCESS)
{ {

View File

@ -116,9 +116,14 @@ public class wolfSSL_TLS_ServerThreaded
IntPtr ctx; IntPtr ctx;
/* These paths should be changed for use */ /* These paths should be changed for use */
string fileCert = @"server-cert.pem"; string fileCert = wolfssl.setPath("server-cert.pem");
string fileKey = @"server-key.pem"; string fileKey = wolfssl.setPath("server-key.pem");
StringBuilder dhparam = new StringBuilder("dh2048.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 */ /* example of function used for setting logging */
wolfssl.SetLogging(standard_log); wolfssl.SetLogging(standard_log);
@ -140,6 +145,12 @@ public class wolfSSL_TLS_ServerThreaded
return; 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) if (wolfssl.CTX_use_certificate_file(ctx, fileCert, wolfssl.SSL_FILETYPE_PEM) != wolfssl.SUCCESS)
{ {
Console.WriteLine("Error in setting cert file"); Console.WriteLine("Error in setting cert file");

View File

@ -485,6 +485,26 @@ namespace wolfSSL.CSharp {
} }
} }
/// <summary>
/// Utility function used to access the certificates
/// based on the platform.
/// <returns>return the platform specific path to the certificate</returns>
/// </summary>
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 "";
}
}
/// <summary> /// <summary>
/// Call back to allow receiving TLS information /// Call back to allow receiving TLS information