mirror of https://github.com/wolfSSL/wolfssl.git
Merge pull request #8799 from dgarske/csharp_wince_unicode
Fix issue with CSharp and Windows CE with conversion of ASCII and Unicodepull/8815/merge
commit
ecb8cb744e
|
@ -42,33 +42,43 @@ namespace wolfSSL.CSharp
|
||||||
*/
|
*/
|
||||||
#if WindowsCE
|
#if WindowsCE
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Convert unicode string to ASCII
|
/// Convert MBCS (8-bit single/multi byte) to Wide Char/Unicode (16-bit) character set
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static string UnicodeToAscii(string msg)
|
public static string MultiByteToWideChar(string msg)
|
||||||
{
|
{
|
||||||
if (msg == null)
|
if (msg == null)
|
||||||
return null;
|
return null;
|
||||||
/* Convert Unicode to Bytes */
|
/* Convert to Byte Array */
|
||||||
byte[] bytes = Encoding.Unicode.GetBytes((string)msg.ToString());
|
byte[] bytes = Encoding.Unicode.GetBytes((string)msg.ToString());
|
||||||
/* Convert to ASCII */
|
/* Convert to String */
|
||||||
return Encoding.ASCII.GetString(bytes, 0, bytes.Length);
|
string ret = Encoding.ASCII.GetString(bytes, 0, bytes.Length);
|
||||||
|
/* Remove possible extra null terminator */
|
||||||
|
int len = 0;
|
||||||
|
while (len < ret.Length && ret[len] != 0) len++;
|
||||||
|
return ret.Substring(0, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Convert string to Unicode
|
/// Convert Unicode/Wide Char (16-bit) to MBCS (8-bit single/multi byte) character set
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static string AsciiToUnicode(string msg)
|
public static string WideCharToMultiByte(string msg)
|
||||||
{
|
{
|
||||||
if (msg == null)
|
if (msg == null)
|
||||||
return null;
|
return null;
|
||||||
/* Convert ASCII to Bytes */
|
/* Get length and round up to even for multibyte / unicode */
|
||||||
byte[] bytes = Encoding.ASCII.GetBytes((string)msg.ToString());
|
int msgLen = msg.Length;
|
||||||
/* Convert to Unicode */
|
msgLen = ((msgLen + 1) & ~1);
|
||||||
|
byte[] bytes = new byte[msgLen];
|
||||||
|
/* Convert to Byte Array */
|
||||||
|
byte[] msgBytes = Encoding.ASCII.GetBytes((string)msg.ToString());
|
||||||
|
msgBytes.CopyTo(bytes, 0);
|
||||||
|
/* Convert to String */
|
||||||
return Encoding.Unicode.GetString(bytes, 0, bytes.Length);
|
return Encoding.Unicode.GetString(bytes, 0, bytes.Length);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// WinCE version of Marshal for Unicode or Multi-byte pointer to ASCII string
|
/// WinCE version of Marshal for Multi-byte pointer to ASCII string
|
||||||
|
/// Similar conversion used in MultiByteToWideChar, but input is IntPtr
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static string PtrToStringAnsi(IntPtr ptr)
|
public static string PtrToStringAnsi(IntPtr ptr)
|
||||||
{
|
{
|
||||||
|
@ -562,7 +572,7 @@ namespace wolfSSL.CSharp
|
||||||
*/
|
*/
|
||||||
#if WindowsCE
|
#if WindowsCE
|
||||||
[DllImport(wolfssl_dll)]
|
[DllImport(wolfssl_dll)]
|
||||||
private extern static IntPtr wolfSSL_ERR_error_string(uint err, StringBuilder errOut);
|
private extern static IntPtr wolfSSL_ERR_reason_error_string(uint err);
|
||||||
[DllImport(wolfssl_dll)]
|
[DllImport(wolfssl_dll)]
|
||||||
private extern static int wolfSSL_get_error(IntPtr ssl, int err);
|
private extern static int wolfSSL_get_error(IntPtr ssl, int err);
|
||||||
public delegate void loggingCb(int lvl, string msg);
|
public delegate void loggingCb(int lvl, string msg);
|
||||||
|
@ -574,7 +584,7 @@ namespace wolfSSL.CSharp
|
||||||
private extern static int wolfSSL_SetLoggingCb(loggingCb vc);
|
private extern static int wolfSSL_SetLoggingCb(loggingCb vc);
|
||||||
#else
|
#else
|
||||||
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
|
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
|
||||||
private extern static IntPtr wolfSSL_ERR_error_string(uint err, StringBuilder errOut);
|
private extern static IntPtr wolfSSL_ERR_reason_error_string(uint err);
|
||||||
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
|
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
|
||||||
private extern static int wolfSSL_get_error(IntPtr ssl, int err);
|
private extern static int wolfSSL_get_error(IntPtr ssl, int err);
|
||||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
||||||
|
@ -1571,7 +1581,7 @@ namespace wolfSSL.CSharp
|
||||||
}
|
}
|
||||||
|
|
||||||
#if WindowsCE
|
#if WindowsCE
|
||||||
return wolfSSL_CTX_use_psk_identity_hint(local_ctx, wolfssl.AsciiToUnicode(hint));
|
return wolfSSL_CTX_use_psk_identity_hint(local_ctx, wolfssl.WideCharToMultiByte(hint));
|
||||||
#else
|
#else
|
||||||
return wolfSSL_CTX_use_psk_identity_hint(local_ctx, hint);
|
return wolfSSL_CTX_use_psk_identity_hint(local_ctx, hint);
|
||||||
#endif
|
#endif
|
||||||
|
@ -1862,7 +1872,7 @@ namespace wolfSSL.CSharp
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
#if WindowsCE
|
#if WindowsCE
|
||||||
return wolfssl.UnicodeToAscii(ciphers);
|
return wolfssl.MultiByteToWideChar(ciphers);
|
||||||
#else
|
#else
|
||||||
return ciphers.ToString();
|
return ciphers.ToString();
|
||||||
#endif
|
#endif
|
||||||
|
@ -2133,7 +2143,7 @@ namespace wolfSSL.CSharp
|
||||||
}
|
}
|
||||||
|
|
||||||
#if WindowsCE
|
#if WindowsCE
|
||||||
return wolfSSL_CTX_set_cipher_list(local_ctx, wolfssl.AsciiToUnicode(list));
|
return wolfSSL_CTX_set_cipher_list(local_ctx, wolfssl.WideCharToMultiByte(list));
|
||||||
#else
|
#else
|
||||||
return wolfSSL_CTX_set_cipher_list(local_ctx, list);
|
return wolfSSL_CTX_set_cipher_list(local_ctx, list);
|
||||||
#endif
|
#endif
|
||||||
|
@ -2168,7 +2178,7 @@ namespace wolfSSL.CSharp
|
||||||
}
|
}
|
||||||
|
|
||||||
#if WindowsCE
|
#if WindowsCE
|
||||||
return wolfSSL_set_cipher_list(sslCtx, wolfssl.AsciiToUnicode(list));
|
return wolfSSL_set_cipher_list(sslCtx, wolfssl.WideCharToMultiByte(list));
|
||||||
#else
|
#else
|
||||||
return wolfSSL_set_cipher_list(sslCtx, list);
|
return wolfSSL_set_cipher_list(sslCtx, list);
|
||||||
#endif
|
#endif
|
||||||
|
@ -2227,10 +2237,6 @@ namespace wolfSSL.CSharp
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
int err;
|
|
||||||
StringBuilder err_name;
|
|
||||||
StringBuilder ret;
|
|
||||||
|
|
||||||
IntPtr sslCtx = unwrap_ssl(ssl);
|
IntPtr sslCtx = unwrap_ssl(ssl);
|
||||||
if (sslCtx == IntPtr.Zero)
|
if (sslCtx == IntPtr.Zero)
|
||||||
{
|
{
|
||||||
|
@ -2238,14 +2244,10 @@ namespace wolfSSL.CSharp
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* wolfSSL max error length is 80 */
|
int err = wolfSSL_get_error(sslCtx, 0);
|
||||||
ret = new StringBuilder(' ', 100);
|
IntPtr err_ptr = wolfSSL_ERR_reason_error_string((uint)err);
|
||||||
err = wolfSSL_get_error(sslCtx, 0);
|
string err_str = wolfssl.PtrToStringAnsi(err_ptr);
|
||||||
err_name = new StringBuilder(new String(' ', 80));
|
return "Error " + err + " " + err_str;
|
||||||
wolfSSL_ERR_error_string((uint)err, err_name);
|
|
||||||
ret.Append("Error " + err + " " + err_name.ToString());
|
|
||||||
|
|
||||||
return ret.ToString();
|
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
|
@ -2274,7 +2276,7 @@ namespace wolfSSL.CSharp
|
||||||
}
|
}
|
||||||
|
|
||||||
#if WindowsCE
|
#if WindowsCE
|
||||||
return wolfSSL_CTX_use_certificate_file(local_ctx, wolfssl.AsciiToUnicode(fileCert), type);
|
return wolfSSL_CTX_use_certificate_file(local_ctx, wolfssl.WideCharToMultiByte(fileCert), type);
|
||||||
#else
|
#else
|
||||||
return wolfSSL_CTX_use_certificate_file(local_ctx, fileCert, type);
|
return wolfSSL_CTX_use_certificate_file(local_ctx, fileCert, type);
|
||||||
#endif
|
#endif
|
||||||
|
@ -2306,7 +2308,7 @@ namespace wolfSSL.CSharp
|
||||||
}
|
}
|
||||||
|
|
||||||
#if WindowsCE
|
#if WindowsCE
|
||||||
return wolfSSL_CTX_load_verify_locations(local_ctx, wolfssl.AsciiToUnicode(fileCert), wolfssl.AsciiToUnicode(path));
|
return wolfSSL_CTX_load_verify_locations(local_ctx, wolfssl.WideCharToMultiByte(fileCert), wolfssl.WideCharToMultiByte(path));
|
||||||
#else
|
#else
|
||||||
return wolfSSL_CTX_load_verify_locations(local_ctx, fileCert, path);
|
return wolfSSL_CTX_load_verify_locations(local_ctx, fileCert, path);
|
||||||
#endif
|
#endif
|
||||||
|
@ -2337,7 +2339,7 @@ namespace wolfSSL.CSharp
|
||||||
}
|
}
|
||||||
|
|
||||||
#if WindowsCE
|
#if WindowsCE
|
||||||
return wolfSSL_CTX_use_PrivateKey_file(local_ctx, wolfssl.AsciiToUnicode(fileKey), type);
|
return wolfSSL_CTX_use_PrivateKey_file(local_ctx, wolfssl.WideCharToMultiByte(fileKey), type);
|
||||||
#else
|
#else
|
||||||
return wolfSSL_CTX_use_PrivateKey_file(local_ctx, fileKey, type);
|
return wolfSSL_CTX_use_PrivateKey_file(local_ctx, fileKey, type);
|
||||||
#endif
|
#endif
|
||||||
|
@ -2373,7 +2375,7 @@ namespace wolfSSL.CSharp
|
||||||
}
|
}
|
||||||
|
|
||||||
#if WindowsCE
|
#if WindowsCE
|
||||||
return wolfSSL_SetTmpDH_file(sslCtx, wolfssl.AsciiToUnicode(dhparam), file_type);
|
return wolfSSL_SetTmpDH_file(sslCtx, wolfssl.WideCharToMultiByte(dhparam), file_type);
|
||||||
#else
|
#else
|
||||||
return wolfSSL_SetTmpDH_file(sslCtx, dhparam, file_type);
|
return wolfSSL_SetTmpDH_file(sslCtx, dhparam, file_type);
|
||||||
#endif
|
#endif
|
||||||
|
@ -2408,7 +2410,7 @@ namespace wolfSSL.CSharp
|
||||||
}
|
}
|
||||||
|
|
||||||
#if WindowsCE
|
#if WindowsCE
|
||||||
return wolfSSL_CTX_SetTmpDH_file(local_ctx, wolfssl.AsciiToUnicode(dhparam), file_type);
|
return wolfSSL_CTX_SetTmpDH_file(local_ctx, wolfssl.WideCharToMultiByte(dhparam), file_type);
|
||||||
#else
|
#else
|
||||||
return wolfSSL_CTX_SetTmpDH_file(local_ctx, dhparam, file_type);
|
return wolfSSL_CTX_SetTmpDH_file(local_ctx, dhparam, file_type);
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue