Skip to content

Commit ecb8cb7

Browse files
Merge pull request #8799 from dgarske/csharp_wince_unicode
Fix issue with CSharp and Windows CE with conversion of ASCII and Unicode
2 parents 30490f9 + 18aab1a commit ecb8cb7

1 file changed

Lines changed: 36 additions & 34 deletions

File tree

wrapper/CSharp/wolfSSL_CSharp/wolfSSL.cs

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -42,33 +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 */
54-
return Encoding.ASCII.GetString(bytes, 0, bytes.Length);
53+
/* Convert to String */
54+
string ret = Encoding.ASCII.GetString(bytes, 0, bytes.Length);
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);
5559
}
5660

5761
/// <summary>
58-
/// Convert string to Unicode
62+
/// Convert Unicode/Wide Char (16-bit) to MBCS (8-bit single/multi byte) character set
5963
/// </summary>
60-
public static string AsciiToUnicode(string msg)
64+
public static string WideCharToMultiByte(string msg)
6165
{
6266
if (msg == null)
6367
return null;
64-
/* Convert ASCII to Bytes */
65-
byte[] bytes = Encoding.ASCII.GetBytes((string)msg.ToString());
66-
/* Convert to Unicode */
68+
/* Get length and round up to even for multibyte / unicode */
69+
int msgLen = msg.Length;
70+
msgLen = ((msgLen + 1) & ~1);
71+
byte[] bytes = new byte[msgLen];
72+
/* Convert to Byte Array */
73+
byte[] msgBytes = Encoding.ASCII.GetBytes((string)msg.ToString());
74+
msgBytes.CopyTo(bytes, 0);
75+
/* Convert to String */
6776
return Encoding.Unicode.GetString(bytes, 0, bytes.Length);
6877
}
6978

7079
/// <summary>
71-
/// WinCE version of Marshal for Unicode or Multi-byte pointer to ASCII string
80+
/// WinCE version of Marshal for Multi-byte pointer to ASCII string
81+
/// Similar conversion used in MultiByteToWideChar, but input is IntPtr
7282
/// </summary>
7383
public static string PtrToStringAnsi(IntPtr ptr)
7484
{
@@ -562,7 +572,7 @@ public void free()
562572
*/
563573
#if WindowsCE
564574
[DllImport(wolfssl_dll)]
565-
private extern static IntPtr wolfSSL_ERR_error_string(uint err, StringBuilder errOut);
575+
private extern static IntPtr wolfSSL_ERR_reason_error_string(uint err);
566576
[DllImport(wolfssl_dll)]
567577
private extern static int wolfSSL_get_error(IntPtr ssl, int err);
568578
public delegate void loggingCb(int lvl, string msg);
@@ -574,7 +584,7 @@ public void free()
574584
private extern static int wolfSSL_SetLoggingCb(loggingCb vc);
575585
#else
576586
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
577-
private extern static IntPtr wolfSSL_ERR_error_string(uint err, StringBuilder errOut);
587+
private extern static IntPtr wolfSSL_ERR_reason_error_string(uint err);
578588
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
579589
private extern static int wolfSSL_get_error(IntPtr ssl, int err);
580590
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
@@ -1571,7 +1581,7 @@ public static int CTX_use_psk_identity_hint(IntPtr ctx, StringBuilder hint)
15711581
}
15721582

15731583
#if WindowsCE
1574-
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));
15751585
#else
15761586
return wolfSSL_CTX_use_psk_identity_hint(local_ctx, hint);
15771587
#endif
@@ -1862,7 +1872,7 @@ public static string get_ciphers()
18621872
return null;
18631873

18641874
#if WindowsCE
1865-
return wolfssl.UnicodeToAscii(ciphers);
1875+
return wolfssl.MultiByteToWideChar(ciphers);
18661876
#else
18671877
return ciphers.ToString();
18681878
#endif
@@ -2133,7 +2143,7 @@ public static int CTX_set_cipher_list(IntPtr ctx, StringBuilder list)
21332143
}
21342144

21352145
#if WindowsCE
2136-
return wolfSSL_CTX_set_cipher_list(local_ctx, wolfssl.AsciiToUnicode(list));
2146+
return wolfSSL_CTX_set_cipher_list(local_ctx, wolfssl.WideCharToMultiByte(list));
21372147
#else
21382148
return wolfSSL_CTX_set_cipher_list(local_ctx, list);
21392149
#endif
@@ -2168,7 +2178,7 @@ public static int set_cipher_list(IntPtr ssl, StringBuilder list)
21682178
}
21692179

21702180
#if WindowsCE
2171-
return wolfSSL_set_cipher_list(sslCtx, wolfssl.AsciiToUnicode(list));
2181+
return wolfSSL_set_cipher_list(sslCtx, wolfssl.WideCharToMultiByte(list));
21722182
#else
21732183
return wolfSSL_set_cipher_list(sslCtx, list);
21742184
#endif
@@ -2227,25 +2237,17 @@ public static string get_error(IntPtr ssl)
22272237

22282238
try
22292239
{
2230-
int err;
2231-
StringBuilder err_name;
2232-
StringBuilder ret;
2233-
22342240
IntPtr sslCtx = unwrap_ssl(ssl);
22352241
if (sslCtx == IntPtr.Zero)
22362242
{
22372243
log(ERROR_LOG, "wolfssl get_error error");
22382244
return null;
22392245
}
22402246

2241-
/* wolfSSL max error length is 80 */
2242-
ret = new StringBuilder(' ', 100);
2243-
err = wolfSSL_get_error(sslCtx, 0);
2244-
err_name = new StringBuilder(new String(' ', 80));
2245-
wolfSSL_ERR_error_string((uint)err, err_name);
2246-
ret.Append("Error " + err + " " + err_name.ToString());
2247-
2248-
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;
22492251
}
22502252
catch (Exception e)
22512253
{
@@ -2274,7 +2276,7 @@ public static int CTX_use_certificate_file(IntPtr ctx, string fileCert, int type
22742276
}
22752277

22762278
#if WindowsCE
2277-
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);
22782280
#else
22792281
return wolfSSL_CTX_use_certificate_file(local_ctx, fileCert, type);
22802282
#endif
@@ -2306,7 +2308,7 @@ public static int CTX_load_verify_locations(IntPtr ctx, string fileCert, string
23062308
}
23072309

23082310
#if WindowsCE
2309-
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));
23102312
#else
23112313
return wolfSSL_CTX_load_verify_locations(local_ctx, fileCert, path);
23122314
#endif
@@ -2337,7 +2339,7 @@ public static int CTX_use_PrivateKey_file(IntPtr ctx, string fileKey, int type)
23372339
}
23382340

23392341
#if WindowsCE
2340-
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);
23412343
#else
23422344
return wolfSSL_CTX_use_PrivateKey_file(local_ctx, fileKey, type);
23432345
#endif
@@ -2373,7 +2375,7 @@ public static int SetTmpDH_file(IntPtr ssl, StringBuilder dhparam, int file_type
23732375
}
23742376

23752377
#if WindowsCE
2376-
return wolfSSL_SetTmpDH_file(sslCtx, wolfssl.AsciiToUnicode(dhparam), file_type);
2378+
return wolfSSL_SetTmpDH_file(sslCtx, wolfssl.WideCharToMultiByte(dhparam), file_type);
23772379
#else
23782380
return wolfSSL_SetTmpDH_file(sslCtx, dhparam, file_type);
23792381
#endif
@@ -2408,7 +2410,7 @@ public static int CTX_SetTmpDH_file(IntPtr ctx, StringBuilder dhparam, int file_
24082410
}
24092411

24102412
#if WindowsCE
2411-
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);
24122414
#else
24132415
return wolfSSL_CTX_SetTmpDH_file(local_ctx, dhparam, file_type);
24142416
#endif

0 commit comments

Comments
 (0)