Skip to content

Commit

Permalink
- Platform specific function to correctly set the path for the certif…
Browse files Browse the repository at this point in the history
…icates;

- Updated all the examples with it;
  • Loading branch information
gasbytes committed Jun 5, 2024
1 parent 6cb97a7 commit 2ab709c
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 52 deletions.
17 changes: 14 additions & 3 deletions wrapper/CSharp/wolfSSL-DTLS-PSK-Server/wolfSSL-DTLS-PSK-Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,14 @@ public static void Main(string[] args)
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);

Expand All @@ -106,6 +111,12 @@ public static void Main(string[] args)
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)
{
Expand Down
17 changes: 14 additions & 3 deletions wrapper/CSharp/wolfSSL-DTLS-Server/wolfSSL-DTLS-Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,14 @@ public static void Main(string[] args)
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");
Expand All @@ -87,6 +92,12 @@ public static void Main(string[] args)
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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,17 @@ static void Main(string[] args)
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");
Expand All @@ -242,6 +247,12 @@ static void Main(string[] args)
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");
Expand Down
27 changes: 10 additions & 17 deletions wrapper/CSharp/wolfSSL-TLS-Client/wolfSSL-TLS-Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,6 @@ private static int haveSNI(string[] args)
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;
Expand All @@ -98,14 +85,14 @@ public static void Main(string[] args)
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");

Expand All @@ -131,6 +118,12 @@ public static void Main(string[] args)
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)
{
Expand Down
12 changes: 11 additions & 1 deletion wrapper/CSharp/wolfSSL-TLS-PSK-Client/wolfSSL-TLS-PSK-Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,11 @@ public static void Main(string[] args)

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");
Expand Down Expand Up @@ -157,6 +161,12 @@ public static void Main(string[] args)
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)
Expand Down
17 changes: 14 additions & 3 deletions wrapper/CSharp/wolfSSL-TLS-PSK-Server/wolfSSL-TLS-PSK-Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,14 @@ public static void Main(string[] args)
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");
Expand All @@ -105,6 +110,12 @@ public static void Main(string[] args)
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");
Expand Down
38 changes: 19 additions & 19 deletions wrapper/CSharp/wolfSSL-TLS-Server/wolfSSL-TLS-Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,6 @@ public static int my_sni_server_cb(IntPtr ssl, IntPtr ret, IntPtr exArg) {
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;
Expand All @@ -101,15 +88,15 @@ public static void Main(string[] args)
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");

Expand All @@ -134,6 +121,12 @@ public static void Main(string[] args)
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");
Expand Down Expand Up @@ -197,7 +190,14 @@ public static void Main(string[] args)
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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,14 @@ public static void Main(string[] args)
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);
Expand All @@ -140,6 +145,12 @@ public static void Main(string[] args)
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");
Expand Down
20 changes: 20 additions & 0 deletions wrapper/CSharp/wolfSSL_CSharp/wolfSSL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,26 @@ private static IntPtr unwrap_ssl(IntPtr ssl)
}
}

/// <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>
/// Call back to allow receiving TLS information
Expand Down

0 comments on commit 2ab709c

Please sign in to comment.