Skip to content

Commit c04c768

Browse files
committed
added callback example: setting sni cb & arg server side, and passing the name client side via -S flag
1 parent c325de9 commit c04c768

1 file changed

Lines changed: 43 additions & 16 deletions

File tree

wrapper/CSharp/wolfSSL-TLS-Server/wolfSSL-TLS-Server.cs

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,27 @@ private static bool haveSNI(string[] args)
6666
}
6767
}
6868

69-
69+
/// <summary>
70+
/// Example of a SNI function call back
71+
/// </summary>
72+
/// <param name="ssl">pointer to ssl structure</param>
73+
/// <param name="ret">alert code</param>
74+
/// <param name="exArg">context arg, can be set with the function wolfssl.CTX_set_servername_arg</param>
75+
/// <returns></returns>
76+
public static int my_sni_server_cb(IntPtr ssl, IntPtr ret, IntPtr exArg) {
77+
/* Trivial callback just for testing */
78+
Console.WriteLine("my sni server callback");
79+
80+
return wolfssl.SUCCESS;
81+
}
7082

7183
public static void Main(string[] args)
7284
{
7385
IntPtr ctx;
7486
IntPtr ssl;
7587
Socket fd;
7688
IntPtr sniHostName;
89+
IntPtr arg_sni;
7790

7891
/* These paths should be changed for use */
7992
string fileCert = @"server-cert.pem";
@@ -118,21 +131,6 @@ public static void Main(string[] args)
118131
return;
119132
}
120133

121-
if (haveSNI(args))
122-
{
123-
string sniHostNameString = args[1].Trim();
124-
sniHostName = Marshal.StringToHGlobalAnsi(sniHostNameString);
125-
126-
ushort size = (ushort)sniHostNameString.Length;
127-
128-
if (wolfssl.CTX_UseSNI(ctx, (byte)wolfssl.WOLFSSL_SNI_HOST_NAME, sniHostName, size) != wolfssl.SUCCESS)
129-
{
130-
Console.WriteLine("UseSNI failed");
131-
wolfssl.CTX_free(ctx);
132-
return;
133-
}
134-
}
135-
136134
StringBuilder ciphers = new StringBuilder(new String(' ', 4096));
137135
wolfssl.get_ciphers(ciphers, 4096);
138136
Console.WriteLine("Ciphers : " + ciphers.ToString());
@@ -155,6 +153,34 @@ public static void Main(string[] args)
155153
return;
156154
}
157155

156+
if (haveSNI(args))
157+
{
158+
string sniHostNameString = args[1].Trim();
159+
sniHostName = Marshal.StringToHGlobalAnsi(sniHostNameString);
160+
161+
ushort size = (ushort)sniHostNameString.Length;
162+
163+
// Allocating memory and setting SNI arg
164+
int test_value = 32;
165+
arg_sni = Marshal.AllocHGlobal(sizeof(int));
166+
Marshal.WriteInt32(arg_sni, test_value);
167+
if (wolfssl.CTX_set_servername_arg(ctx, arg_sni) == wolfssl.FAILURE) {
168+
Console.WriteLine("wolfssl.CTX_set_servername_arg failed");
169+
wolfssl.CTX_free(ctx);
170+
return;
171+
}
172+
173+
// Setting SNI delegate
174+
wolfssl.sni_delegate sni_cb = new wolfssl.sni_delegate(my_sni_server_cb);
175+
wolfssl.CTX_set_servername_callback(ctx, sni_cb);
176+
177+
if (wolfssl.CTX_set_tlsext_servername_callback(ssl, sni_cb) == wolfssl.FAILURE) {
178+
Console.WriteLine("wolfssl.CTX_set_tlsext_servername_callback failed");
179+
wolfssl.CTX_free(ctx);
180+
return;
181+
}
182+
}
183+
158184
Console.WriteLine("Connection made wolfSSL_accept ");
159185
if (wolfssl.set_fd(ssl, fd) != wolfssl.SUCCESS)
160186
{
@@ -201,6 +227,7 @@ public static void Main(string[] args)
201227
wolfssl.shutdown(ssl);
202228
fd.Close();
203229
tcp.Stop();
230+
204231
clean(ssl, ctx);
205232
}
206233
}

0 commit comments

Comments
 (0)