Skip to content

Commit

Permalink
added callback example: setting sni cb & arg server side, and passing…
Browse files Browse the repository at this point in the history
… the name client side via -S flag
  • Loading branch information
gasbytes committed Jun 3, 2024
1 parent c325de9 commit c04c768
Showing 1 changed file with 43 additions and 16 deletions.
59 changes: 43 additions & 16 deletions wrapper/CSharp/wolfSSL-TLS-Server/wolfSSL-TLS-Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,27 @@ private static bool haveSNI(string[] args)
}
}


/// <summary>
/// Example of a SNI function call back
/// </summary>
/// <param name="ssl">pointer to ssl structure</param>
/// <param name="ret">alert code</param>
/// <param name="exArg">context arg, can be set with the function wolfssl.CTX_set_servername_arg</param>
/// <returns></returns>
public static int my_sni_server_cb(IntPtr ssl, IntPtr ret, IntPtr exArg) {
/* Trivial callback just for testing */
Console.WriteLine("my sni server callback");

return wolfssl.SUCCESS;
}

public static void Main(string[] args)
{
IntPtr ctx;
IntPtr ssl;
Socket fd;
IntPtr sniHostName;
IntPtr arg_sni;

/* These paths should be changed for use */
string fileCert = @"server-cert.pem";
Expand Down Expand Up @@ -118,21 +131,6 @@ public static void Main(string[] args)
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);
Console.WriteLine("Ciphers : " + ciphers.ToString());
Expand All @@ -155,6 +153,34 @@ public static void Main(string[] args)
return;
}

if (haveSNI(args))
{
string sniHostNameString = args[1].Trim();
sniHostName = Marshal.StringToHGlobalAnsi(sniHostNameString);

ushort size = (ushort)sniHostNameString.Length;

// Allocating memory and setting SNI arg
int test_value = 32;
arg_sni = Marshal.AllocHGlobal(sizeof(int));
Marshal.WriteInt32(arg_sni, test_value);
if (wolfssl.CTX_set_servername_arg(ctx, arg_sni) == wolfssl.FAILURE) {
Console.WriteLine("wolfssl.CTX_set_servername_arg failed");
wolfssl.CTX_free(ctx);
return;
}

// Setting SNI delegate
wolfssl.sni_delegate sni_cb = new wolfssl.sni_delegate(my_sni_server_cb);
wolfssl.CTX_set_servername_callback(ctx, sni_cb);

if (wolfssl.CTX_set_tlsext_servername_callback(ssl, sni_cb) == wolfssl.FAILURE) {
Console.WriteLine("wolfssl.CTX_set_tlsext_servername_callback failed");
wolfssl.CTX_free(ctx);
return;
}
}

Console.WriteLine("Connection made wolfSSL_accept ");
if (wolfssl.set_fd(ssl, fd) != wolfssl.SUCCESS)
{
Expand Down Expand Up @@ -201,6 +227,7 @@ public static void Main(string[] args)
wolfssl.shutdown(ssl);
fd.Close();
tcp.Stop();

clean(ssl, ctx);
}
}

0 comments on commit c04c768

Please sign in to comment.