wolfSSL_SNI_GetRequest working, fixing up wolfSSL_SNI_GetFromBuffer

pull/7692/head
gasbytes 2024-06-27 15:05:02 +02:00
parent 474b8a0673
commit 6dd43caae9
2 changed files with 58 additions and 0 deletions

View File

@ -155,6 +155,7 @@ public class wolfSSL_TLS_CSHarp
Console.WriteLine("Started TCP and waiting for a connection");
fd = tcp.AcceptSocket();
ssl = wolfssl.new_ssl(ctx);
if (ssl == IntPtr.Zero)
{
@ -208,6 +209,16 @@ public class wolfSSL_TLS_CSHarp
return;
}
/* get and print sni used by the client */
if (haveSNI(args)) {
IntPtr data = IntPtr.Zero;
ushort size = wolfssl.SNI_GetRequest(ssl, 0, ref data);
string dataStr = Marshal.PtrToStringAnsi(data);
Console.WriteLine("(SNI_GetRequest) Size of SNI used by client: " + size);
Console.WriteLine("(SNI_GetRequest) SNI used by client: " + dataStr);
}
/* print out results of TLS/SSL accept */
Console.WriteLine("SSL version is " + wolfssl.get_version(ssl));
Console.WriteLine("SSL cipher suite is " + wolfssl.get_current_cipher(ssl));
@ -222,6 +233,26 @@ public class wolfSSL_TLS_CSHarp
}
Console.WriteLine(buff);
/* get and print sni used by the client and also their message */
if (haveSNI(args)) {
IntPtr result = Marshal.AllocHGlobal(32);
IntPtr inOutSz = Marshal.AllocHGlobal(sizeof(int));
Marshal.WriteInt32(inOutSz, 32);
int ret = wolfssl.SNI_GetFromBuffer(buff, 1024, 0, result, inOutSz);
if (ret != wolfssl.SUCCESS) {
Console.WriteLine("Error on reading SNI from buffer, ret value = " + ret);
tcp.Stop();
clean(ssl, ctx);
return;
}
string dataStr = Marshal.PtrToStringAnsi(result);
Console.WriteLine("(SNI_GetFromBuffer) SNI used by client: " + dataStr);
}
if (wolfssl.write(ssl, reply, reply.Length) != reply.Length)
{
Console.WriteLine("Error in write");

View File

@ -330,6 +330,10 @@ namespace wolfSSL.CSharp {
private extern static int wolfSSL_CTX_UseSNI(IntPtr ctx, byte type, IntPtr data, ushort size);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wolfSSL_UseSNI(IntPtr ssl, byte type, IntPtr data, ushort size);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static ushort wolfSSL_SNI_GetRequest(IntPtr ssl, byte type, ref IntPtr data);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wolfSSL_SNI_GetFromBuffer(StringBuilder clientHello, uint helloSz, byte type, IntPtr sni, IntPtr inOutSz);
/********************************
* SSL Structure
@ -1200,6 +1204,29 @@ namespace wolfSSL.CSharp {
}
}
public static ushort SNI_GetRequest(IntPtr ssl, byte type, ref IntPtr data)
{
try {
GCHandle gch = GCHandle.FromIntPtr(ssl);
ssl_handle handles = (ssl_handle)gch.Target;
return wolfSSL_SNI_GetRequest(handles.get_ssl(), type, ref data);
} catch (Exception e) {
log(ERROR_LOG, "wolfssl sni get request error: " + e.ToString());
return ushort.MaxValue;
}
}
public static int SNI_GetFromBuffer(StringBuilder clientHello, uint helloSz, byte type, IntPtr sni, IntPtr inOutSz)
{
try {
return wolfSSL_SNI_GetFromBuffer(clientHello, helloSz, type, sni, inOutSz);
} catch(Exception e) {
log(ERROR_LOG, "wolfssl sni get from buffer error: " + e.ToString());
return FAILURE;
}
}
/// <summary>
/// Set identity hint to use
/// </summary>