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,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 ) ) ;
0 commit comments