Skip to content

Commit 6f567b5

Browse files
committed
completed the examples
1 parent 15ac366 commit 6f567b5

3 files changed

Lines changed: 74 additions & 8 deletions

File tree

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

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
2020
*/
2121

22-
2322
using System;
2423
using System.Runtime.InteropServices;
2524
using System.Text;
@@ -60,11 +59,32 @@ private static int myVerify(int preverify, IntPtr x509_ctx)
6059
return preverify;
6160
}
6261

62+
/// <summary>
63+
/// Checks if the SNI option was enabled via command line.
64+
/// Must be enabled with ./configure --enable-sni when configuring
65+
/// wolfSSL.
66+
/// <param name="args">Parameters passed via command line</param>
67+
/// </summary>
68+
private static bool haveSNI(string[] args)
69+
{
70+
if (args != null && args.Length == 2 && args[0] == "-S")
71+
{
72+
Console.WriteLine("SNI IS: ON");
73+
return true;
74+
}
75+
else {
76+
Console.WriteLine("SNI IS: OFF");
77+
return false;
78+
}
79+
}
80+
81+
6382
public static void Main(string[] args)
6483
{
6584
IntPtr ctx;
6685
IntPtr ssl;
6786
Socket tcp;
87+
IntPtr sniHostName;
6888

6989
/* These paths should be changed for use */
7090
string caCert = @"ca-cert.pem";
@@ -78,7 +98,6 @@ public static void Main(string[] args)
7898

7999
wolfssl.Init();
80100

81-
82101
Console.WriteLine("Calling ctx Init from wolfSSL");
83102
ctx = wolfssl.CTX_new(wolfssl.usev23_client());
84103
if (ctx == IntPtr.Zero)
@@ -88,19 +107,34 @@ public static void Main(string[] args)
88107
}
89108
Console.WriteLine("Finished init of ctx .... now load in CA");
90109

91-
92110
if (!File.Exists(caCert))
93111
{
94112
Console.WriteLine("Could not find CA cert file");
95113
wolfssl.CTX_free(ctx);
96114
return;
97115
}
98116

99-
100117
if (wolfssl.CTX_load_verify_locations(ctx, caCert, null)
101118
!= wolfssl.SUCCESS)
102119
{
103120
Console.WriteLine("Error loading CA cert");
121+
wolfssl.CTX_free(ctx);
122+
return;
123+
}
124+
125+
if (haveSNI(args))
126+
{
127+
string sniHostNameString = args[1].Trim();
128+
sniHostName = Marshal.StringToHGlobalAnsi(sniHostNameString);
129+
130+
ushort size = (ushort)sniHostNameString.Length;
131+
132+
if (wolfssl.CTX_UseSNI(ctx, (byte)wolfssl.WOLFSSL_SNI_HOST_NAME, sniHostName, size) != wolfssl.SUCCESS)
133+
{
134+
Console.WriteLine("UseSNI failed");
135+
wolfssl.CTX_free(ctx);
136+
return;
137+
}
104138
}
105139

106140
StringBuilder ciphers = new StringBuilder(new String(' ', 4096));

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

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@
1919
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
2020
*/
2121

22-
23-
24-
2522
using System;
2623
using System.Runtime.InteropServices;
2724
using System.Text;
@@ -50,6 +47,26 @@ private static void clean(IntPtr ssl, IntPtr ctx)
5047
wolfssl.Cleanup();
5148
}
5249

50+
/// <summary>
51+
/// Checks if the SNI option was enabled via command line.
52+
/// Must be enabled with ./configure --enable-sni when configuring
53+
/// wolfSSL.
54+
/// <param name="args">Parameters passed via command line</param>
55+
/// </summary>
56+
private static bool haveSNI(string[] args)
57+
{
58+
if (args != null && args.Length == 2 && args[0] == "-S")
59+
{
60+
Console.WriteLine("SNI IS: ON");
61+
return true;
62+
}
63+
else {
64+
Console.WriteLine("SNI IS: OFF");
65+
return false;
66+
}
67+
}
68+
69+
5370

5471
public static void Main(string[] args)
5572
{
@@ -70,7 +87,6 @@ public static void Main(string[] args)
7087

7188
wolfssl.Init();
7289

73-
7490
Console.WriteLine("Calling ctx Init from wolfSSL");
7591
ctx = wolfssl.CTX_new(wolfssl.usev23_server());
7692
if (ctx == IntPtr.Zero)
@@ -101,6 +117,20 @@ public static void Main(string[] args)
101117
return;
102118
}
103119

120+
if (haveSNI(args))
121+
{
122+
string sniHostNameString = args[1].Trim();
123+
sniHostName = Marshal.StringToHGlobalAnsi(sniHostNameString);
124+
125+
ushort size = (ushort)sniHostNameString.Length;
126+
127+
if (wolfssl.CTX_UseSNI(ctx, (byte)wolfssl.WOLFSSL_SNI_HOST_NAME, sniHostName, size) != wolfssl.SUCCESS)
128+
{
129+
Console.WriteLine("UseSNI failed");
130+
wolfssl.CTX_free(ctx);
131+
return;
132+
}
133+
}
104134

105135
StringBuilder ciphers = new StringBuilder(new String(' ', 4096));
106136
wolfssl.get_ciphers(ciphers, 4096);

wrapper/CSharp/wolfSSL_CSharp/wolfSSL.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,8 @@ public void free()
459459

460460
public static readonly int SUCCESS = 1;
461461
public static readonly int FAILURE = 0;
462+
public static readonly int WOLFSSL_SNI_HOST_NAME = 0;
463+
public static readonly int WOLFSSL_SNI_HOST_NAME_OUTER = 0;
462464

463465

464466
private static IntPtr unwrap_ctx(IntPtr ctx)

0 commit comments

Comments
 (0)