Skip to content

Commit 6dd43ca

Browse files
committed
wolfSSL_SNI_GetRequest working, fixing up wolfSSL_SNI_GetFromBuffer
1 parent 474b8a0 commit 6dd43ca

2 files changed

Lines changed: 58 additions & 0 deletions

File tree

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ public static void Main(string[] args)
155155

156156
Console.WriteLine("Started TCP and waiting for a connection");
157157
fd = tcp.AcceptSocket();
158+
158159
ssl = wolfssl.new_ssl(ctx);
159160
if (ssl == IntPtr.Zero)
160161
{
@@ -208,6 +209,16 @@ public static void Main(string[] args)
208209
return;
209210
}
210211

212+
/* get and print sni used by the client */
213+
if (haveSNI(args)) {
214+
IntPtr data = IntPtr.Zero;
215+
216+
ushort size = wolfssl.SNI_GetRequest(ssl, 0, ref data);
217+
string dataStr = Marshal.PtrToStringAnsi(data);
218+
Console.WriteLine("(SNI_GetRequest) Size of SNI used by client: " + size);
219+
Console.WriteLine("(SNI_GetRequest) SNI used by client: " + dataStr);
220+
}
221+
211222
/* print out results of TLS/SSL accept */
212223
Console.WriteLine("SSL version is " + wolfssl.get_version(ssl));
213224
Console.WriteLine("SSL cipher suite is " + wolfssl.get_current_cipher(ssl));
@@ -222,6 +233,26 @@ public static void Main(string[] args)
222233
}
223234
Console.WriteLine(buff);
224235

236+
/* get and print sni used by the client and also their message */
237+
if (haveSNI(args)) {
238+
IntPtr result = Marshal.AllocHGlobal(32);
239+
IntPtr inOutSz = Marshal.AllocHGlobal(sizeof(int));
240+
Marshal.WriteInt32(inOutSz, 32);
241+
242+
int ret = wolfssl.SNI_GetFromBuffer(buff, 1024, 0, result, inOutSz);
243+
244+
if (ret != wolfssl.SUCCESS) {
245+
Console.WriteLine("Error on reading SNI from buffer, ret value = " + ret);
246+
tcp.Stop();
247+
clean(ssl, ctx);
248+
return;
249+
}
250+
251+
string dataStr = Marshal.PtrToStringAnsi(result);
252+
Console.WriteLine("(SNI_GetFromBuffer) SNI used by client: " + dataStr);
253+
}
254+
255+
225256
if (wolfssl.write(ssl, reply, reply.Length) != reply.Length)
226257
{
227258
Console.WriteLine("Error in write");

wrapper/CSharp/wolfSSL_CSharp/wolfSSL.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,10 @@ public void free()
330330
private extern static int wolfSSL_CTX_UseSNI(IntPtr ctx, byte type, IntPtr data, ushort size);
331331
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
332332
private extern static int wolfSSL_UseSNI(IntPtr ssl, byte type, IntPtr data, ushort size);
333+
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
334+
private extern static ushort wolfSSL_SNI_GetRequest(IntPtr ssl, byte type, ref IntPtr data);
335+
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
336+
private extern static int wolfSSL_SNI_GetFromBuffer(StringBuilder clientHello, uint helloSz, byte type, IntPtr sni, IntPtr inOutSz);
333337

334338
/********************************
335339
* SSL Structure
@@ -1200,6 +1204,29 @@ public static int UseSNI(IntPtr ssl, byte type, IntPtr data, ushort size)
12001204
}
12011205
}
12021206

1207+
public static ushort SNI_GetRequest(IntPtr ssl, byte type, ref IntPtr data)
1208+
{
1209+
try {
1210+
GCHandle gch = GCHandle.FromIntPtr(ssl);
1211+
ssl_handle handles = (ssl_handle)gch.Target;
1212+
1213+
return wolfSSL_SNI_GetRequest(handles.get_ssl(), type, ref data);
1214+
} catch (Exception e) {
1215+
log(ERROR_LOG, "wolfssl sni get request error: " + e.ToString());
1216+
return ushort.MaxValue;
1217+
}
1218+
}
1219+
1220+
public static int SNI_GetFromBuffer(StringBuilder clientHello, uint helloSz, byte type, IntPtr sni, IntPtr inOutSz)
1221+
{
1222+
try {
1223+
return wolfSSL_SNI_GetFromBuffer(clientHello, helloSz, type, sni, inOutSz);
1224+
} catch(Exception e) {
1225+
log(ERROR_LOG, "wolfssl sni get from buffer error: " + e.ToString());
1226+
return FAILURE;
1227+
}
1228+
}
1229+
12031230
/// <summary>
12041231
/// Set identity hint to use
12051232
/// </summary>

0 commit comments

Comments
 (0)