@@ -42,41 +42,43 @@ public class wolfssl
4242 */
4343#if WindowsCE
4444 /// <summary>
45- /// Convert unicode string to ASCII
45+ /// Convert MBCS (8-bit single/multi byte) to Wide Char/Unicode (16-bit) character set
4646 /// </summary>
47- public static string UnicodeToAscii ( string msg )
47+ public static string MultiByteToWideChar ( string msg )
4848 {
4949 if ( msg == null )
5050 return null ;
51- /* Convert Unicode to Bytes */
51+ /* Convert to Byte Array */
5252 byte [ ] bytes = Encoding . Unicode . GetBytes ( ( string ) msg . ToString ( ) ) ;
53- /* Convert to ASCII */
53+ /* Convert to String */
5454 string ret = Encoding . ASCII . GetString ( bytes , 0 , bytes . Length ) ;
55- /* odd length unicode might have extra null terminator, so remove */
56- return ret . Replace ( "\0 " , "" ) ;
55+ /* Remove possible extra null terminator */
56+ int len = 0 ;
57+ while ( len < ret . Length && ret [ len ] != 0 ) len ++ ;
58+ return ret . Substring ( 0 , len ) ;
5759 }
5860
5961 /// <summary>
60- /// Convert string to Unicode
62+ /// Convert Unicode/Wide Char (16-bit) to MBCS (8-bit single/multi byte) character set
6163 /// </summary>
62- public static string AsciiToUnicode ( string msg )
64+ public static string WideCharToMultiByte ( string msg )
6365 {
6466 if ( msg == null )
6567 return null ;
66- /* Get length and round up to even unicode */
68+ /* Get length and round up to even for multibyte / unicode */
6769 int msgLen = msg . Length ;
6870 msgLen = ( ( msgLen + 1 ) & ~ 1 ) ;
6971 byte [ ] bytes = new byte [ msgLen ] ;
70- /* Convert Ascii to Bytes */
72+ /* Convert to Byte Array */
7173 byte [ ] msgBytes = Encoding . ASCII . GetBytes ( ( string ) msg . ToString ( ) ) ;
7274 msgBytes . CopyTo ( bytes , 0 ) ;
73- /* Convert to Unicode */
75+ /* Convert to String */
7476 return Encoding . Unicode . GetString ( bytes , 0 , bytes . Length ) ;
7577 }
7678
7779 /// <summary>
78- /// WinCE version of Marshal for Unicode or Multi-byte pointer to
79- /// ASCII string
80+ /// WinCE version of Marshal for Multi-byte pointer to ASCII string
81+ /// Similar conversion used in MultiByteToWideChar, but input is IntPtr
8082 /// </summary>
8183 public static string PtrToStringAnsi ( IntPtr ptr )
8284 {
@@ -570,7 +572,7 @@ public void free()
570572 */
571573#if WindowsCE
572574 [ DllImport ( wolfssl_dll ) ]
573- private extern static IntPtr wolfSSL_ERR_error_string ( uint err , StringBuilder errOut ) ;
575+ private extern static IntPtr wolfSSL_ERR_reason_error_string ( uint err ) ;
574576 [ DllImport ( wolfssl_dll ) ]
575577 private extern static int wolfSSL_get_error ( IntPtr ssl , int err ) ;
576578 public delegate void loggingCb ( int lvl , string msg ) ;
@@ -582,7 +584,7 @@ public void free()
582584 private extern static int wolfSSL_SetLoggingCb ( loggingCb vc ) ;
583585#else
584586 [ DllImport ( wolfssl_dll , CallingConvention = CallingConvention . Cdecl , CharSet = CharSet . Ansi ) ]
585- private extern static IntPtr wolfSSL_ERR_error_string ( uint err , StringBuilder errOut ) ;
587+ private extern static IntPtr wolfSSL_ERR_reason_error_string ( uint err ) ;
586588 [ DllImport ( wolfssl_dll , CallingConvention = CallingConvention . Cdecl ) ]
587589 private extern static int wolfSSL_get_error ( IntPtr ssl , int err ) ;
588590 [ UnmanagedFunctionPointer ( CallingConvention . Cdecl ) ]
@@ -1579,7 +1581,7 @@ public static int CTX_use_psk_identity_hint(IntPtr ctx, StringBuilder hint)
15791581 }
15801582
15811583 #if WindowsCE
1582- return wolfSSL_CTX_use_psk_identity_hint ( local_ctx , wolfssl . AsciiToUnicode ( hint ) ) ;
1584+ return wolfSSL_CTX_use_psk_identity_hint ( local_ctx , wolfssl . WideCharToMultiByte ( hint ) ) ;
15831585 #else
15841586 return wolfSSL_CTX_use_psk_identity_hint ( local_ctx , hint ) ;
15851587 #endif
@@ -1870,7 +1872,7 @@ public static string get_ciphers()
18701872 return null ;
18711873
18721874 #if WindowsCE
1873- return wolfssl . UnicodeToAscii ( ciphers ) ;
1875+ return wolfssl . MultiByteToWideChar ( ciphers ) ;
18741876 #else
18751877 return ciphers . ToString ( ) ;
18761878 #endif
@@ -2141,7 +2143,7 @@ public static int CTX_set_cipher_list(IntPtr ctx, StringBuilder list)
21412143 }
21422144
21432145 #if WindowsCE
2144- return wolfSSL_CTX_set_cipher_list ( local_ctx , wolfssl . AsciiToUnicode ( list ) ) ;
2146+ return wolfSSL_CTX_set_cipher_list ( local_ctx , wolfssl . WideCharToMultiByte ( list ) ) ;
21452147 #else
21462148 return wolfSSL_CTX_set_cipher_list ( local_ctx , list ) ;
21472149 #endif
@@ -2176,7 +2178,7 @@ public static int set_cipher_list(IntPtr ssl, StringBuilder list)
21762178 }
21772179
21782180 #if WindowsCE
2179- return wolfSSL_set_cipher_list ( sslCtx , wolfssl . AsciiToUnicode ( list ) ) ;
2181+ return wolfSSL_set_cipher_list ( sslCtx , wolfssl . WideCharToMultiByte ( list ) ) ;
21802182 #else
21812183 return wolfSSL_set_cipher_list ( sslCtx , list ) ;
21822184 #endif
@@ -2235,25 +2237,17 @@ public static string get_error(IntPtr ssl)
22352237
22362238 try
22372239 {
2238- int err ;
2239- StringBuilder err_name ;
2240- StringBuilder ret ;
2241-
22422240 IntPtr sslCtx = unwrap_ssl ( ssl ) ;
22432241 if ( sslCtx == IntPtr . Zero )
22442242 {
22452243 log ( ERROR_LOG , "wolfssl get_error error" ) ;
22462244 return null ;
22472245 }
22482246
2249- /* wolfSSL max error length is 80 */
2250- ret = new StringBuilder ( ' ' , 100 ) ;
2251- err = wolfSSL_get_error ( sslCtx , 0 ) ;
2252- err_name = new StringBuilder ( new String ( ' ' , 80 ) ) ;
2253- wolfSSL_ERR_error_string ( ( uint ) err , err_name ) ;
2254- ret . Append ( "Error " + err + " " + err_name . ToString ( ) ) ;
2255-
2256- return ret . ToString ( ) ;
2247+ int err = wolfSSL_get_error ( sslCtx , 0 ) ;
2248+ IntPtr err_ptr = wolfSSL_ERR_reason_error_string ( ( uint ) err ) ;
2249+ string err_str = wolfssl . PtrToStringAnsi ( err_ptr ) ;
2250+ return "Error " + err + " " + err_str ;
22572251 }
22582252 catch ( Exception e )
22592253 {
@@ -2282,7 +2276,7 @@ public static int CTX_use_certificate_file(IntPtr ctx, string fileCert, int type
22822276 }
22832277
22842278 #if WindowsCE
2285- return wolfSSL_CTX_use_certificate_file ( local_ctx , wolfssl . AsciiToUnicode ( fileCert ) , type ) ;
2279+ return wolfSSL_CTX_use_certificate_file ( local_ctx , wolfssl . WideCharToMultiByte ( fileCert ) , type ) ;
22862280 #else
22872281 return wolfSSL_CTX_use_certificate_file ( local_ctx , fileCert , type ) ;
22882282 #endif
@@ -2314,7 +2308,7 @@ public static int CTX_load_verify_locations(IntPtr ctx, string fileCert, string
23142308 }
23152309
23162310 #if WindowsCE
2317- return wolfSSL_CTX_load_verify_locations ( local_ctx , wolfssl . AsciiToUnicode ( fileCert ) , wolfssl . AsciiToUnicode ( path ) ) ;
2311+ return wolfSSL_CTX_load_verify_locations ( local_ctx , wolfssl . WideCharToMultiByte ( fileCert ) , wolfssl . WideCharToMultiByte ( path ) ) ;
23182312 #else
23192313 return wolfSSL_CTX_load_verify_locations ( local_ctx , fileCert , path ) ;
23202314 #endif
@@ -2345,7 +2339,7 @@ public static int CTX_use_PrivateKey_file(IntPtr ctx, string fileKey, int type)
23452339 }
23462340
23472341 #if WindowsCE
2348- return wolfSSL_CTX_use_PrivateKey_file ( local_ctx , wolfssl . AsciiToUnicode ( fileKey ) , type ) ;
2342+ return wolfSSL_CTX_use_PrivateKey_file ( local_ctx , wolfssl . WideCharToMultiByte ( fileKey ) , type ) ;
23492343 #else
23502344 return wolfSSL_CTX_use_PrivateKey_file ( local_ctx , fileKey , type ) ;
23512345 #endif
@@ -2381,7 +2375,7 @@ public static int SetTmpDH_file(IntPtr ssl, StringBuilder dhparam, int file_type
23812375 }
23822376
23832377 #if WindowsCE
2384- return wolfSSL_SetTmpDH_file ( sslCtx , wolfssl . AsciiToUnicode ( dhparam ) , file_type ) ;
2378+ return wolfSSL_SetTmpDH_file ( sslCtx , wolfssl . WideCharToMultiByte ( dhparam ) , file_type ) ;
23852379 #else
23862380 return wolfSSL_SetTmpDH_file ( sslCtx , dhparam , file_type ) ;
23872381 #endif
@@ -2416,7 +2410,7 @@ public static int CTX_SetTmpDH_file(IntPtr ctx, StringBuilder dhparam, int file_
24162410 }
24172411
24182412 #if WindowsCE
2419- return wolfSSL_CTX_SetTmpDH_file ( local_ctx , wolfssl . AsciiToUnicode ( dhparam ) , file_type ) ;
2413+ return wolfSSL_CTX_SetTmpDH_file ( local_ctx , wolfssl . WideCharToMultiByte ( dhparam ) , file_type ) ;
24202414 #else
24212415 return wolfSSL_CTX_SetTmpDH_file ( local_ctx , dhparam , file_type ) ;
24222416 #endif
0 commit comments