1919 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
2020 */
2121
22-
2322using System ;
2423using System . Runtime . InteropServices ;
2524using System . Text ;
@@ -60,15 +59,39 @@ 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 int haveSNI ( string [ ] args )
69+ {
70+ for ( int i = 0 ; i < args . Length ; i ++ ) {
71+ if ( args [ i ] == "-S" ) {
72+ Console . WriteLine ( "SNI IS ON" ) ;
73+ return i + 1 ;
74+ }
75+ }
76+ Console . WriteLine ( "SNI IS OFF" ) ;
77+ return - 1 ;
78+ }
79+
6380 public static void Main ( string [ ] args )
6481 {
6582 IntPtr ctx ;
6683 IntPtr ssl ;
6784 Socket tcp ;
85+ IntPtr sniHostName ;
6886
6987 /* These paths should be changed for use */
70- string caCert = @"ca-cert.pem" ;
71- StringBuilder dhparam = new StringBuilder ( "dh2048.pem" ) ;
88+ string caCert = wolfssl . setPath ( "ca-cert.pem" ) ;
89+ StringBuilder dhparam = new StringBuilder ( wolfssl . setPath ( "dh2048.pem" ) ) ;
90+
91+ if ( caCert == "" || dhparam . Length == 0 ) {
92+ Console . WriteLine ( "Platform not supported." ) ;
93+ return ;
94+ }
7295
7396 StringBuilder buff = new StringBuilder ( 1024 ) ;
7497 StringBuilder reply = new StringBuilder ( "Hello, this is the wolfSSL C# wrapper" ) ;
@@ -78,7 +101,6 @@ public static void Main(string[] args)
78101
79102 wolfssl . Init ( ) ;
80103
81-
82104 Console . WriteLine ( "Calling ctx Init from wolfSSL" ) ;
83105 ctx = wolfssl . CTX_new ( wolfssl . usev23_client ( ) ) ;
84106 if ( ctx == IntPtr . Zero )
@@ -96,11 +118,34 @@ public static void Main(string[] args)
96118 return ;
97119 }
98120
121+ if ( ! File . Exists ( dhparam . ToString ( ) ) ) {
122+ Console . WriteLine ( "Could not find dh file" ) ;
123+ wolfssl . CTX_free ( ctx ) ;
124+ return ;
125+ }
99126
100127 if ( wolfssl . CTX_load_verify_locations ( ctx , caCert , null )
101128 != wolfssl . SUCCESS )
102129 {
103130 Console . WriteLine ( "Error loading CA cert" ) ;
131+ wolfssl . CTX_free ( ctx ) ;
132+ return ;
133+ }
134+
135+ int sniArg = haveSNI ( args ) ;
136+ if ( sniArg >= 0 )
137+ {
138+ string sniHostNameString = args [ sniArg ] . Trim ( ) ;
139+ sniHostName = Marshal . StringToHGlobalAnsi ( sniHostNameString ) ;
140+
141+ ushort size = ( ushort ) sniHostNameString . Length ;
142+
143+ if ( wolfssl . CTX_UseSNI ( ctx , ( byte ) wolfssl . WOLFSSL_SNI_HOST_NAME , sniHostName , size ) != wolfssl . SUCCESS )
144+ {
145+ Console . WriteLine ( "UseSNI failed" ) ;
146+ wolfssl . CTX_free ( ctx ) ;
147+ return ;
148+ }
104149 }
105150
106151 StringBuilder ciphers = new StringBuilder ( new String ( ' ' , 4096 ) ) ;
0 commit comments