diff --git a/.wolfssl_known_macro_extras b/.wolfssl_known_macro_extras index 9c87962f2..7947ebef3 100644 --- a/.wolfssl_known_macro_extras +++ b/.wolfssl_known_macro_extras @@ -518,6 +518,7 @@ USE_STSAFE_VERBOSE USE_TLSV13 USE_WOLF_STRNSTR USS_API +WindowsCE WC_AESXTS_STREAM_NO_REQUEST_ACCOUNTING WC_AES_BS_WORD_SIZE WC_AES_GCM_DEC_AUTH_EARLY diff --git a/configure.ac b/configure.ac index 0740c0c29..1a25b53d4 100644 --- a/configure.ac +++ b/configure.ac @@ -4321,7 +4321,8 @@ fi if test "$ENABLED_ECC" != "no" then - AM_CFLAGS="$AM_CFLAGS -DHAVE_ECC -DTFM_ECC256" + AM_CFLAGS="$AM_CFLAGS -DHAVE_ECC" + if test "$ENABLED_ECC_SHAMIR" = "yes" && test "$ENABLED_LOWRESOURCE" = "no" then AM_CFLAGS="$AM_CFLAGS -DECC_SHAMIR" @@ -4332,9 +4333,14 @@ then AM_CFLAGS="$AM_CFLAGS -DWC_ECC_NONBLOCK" fi - if test "$ENABLED_LOWRESOURCE" = "yes" && test "$ENABLED_FASTMATH" = "yes" + if test "$ENABLED_FASTMATH" = "yes" then - AM_CFLAGS="$AM_CFLAGS -DALT_ECC_SIZE" + if test "$ENABLED_LOWRESOURCE" = "yes" + then + AM_CFLAGS="$AM_CFLAGS -DALT_ECC_SIZE" + else + AM_CFLAGS="$AM_CFLAGS -DTFM_ECC256" + fi fi ENABLED_CERTS=yes diff --git a/wolfssl/wolfcrypt/curve25519.h b/wolfssl/wolfcrypt/curve25519.h index 79fb6d9af..dc294ecc8 100644 --- a/wolfssl/wolfcrypt/curve25519.h +++ b/wolfssl/wolfcrypt/curve25519.h @@ -165,7 +165,6 @@ curve25519_key* wc_curve25519_new(void* heap, int devId, int *result_code); WOLFSSL_API int wc_curve25519_delete(curve25519_key* key, curve25519_key** key_p); #endif -WOLFSSL_API /* raw key helpers */ WOLFSSL_API diff --git a/wolfssl/wolfcrypt/ed25519.h b/wolfssl/wolfcrypt/ed25519.h index 8dc0fc118..f7367b547 100644 --- a/wolfssl/wolfcrypt/ed25519.h +++ b/wolfssl/wolfcrypt/ed25519.h @@ -187,7 +187,6 @@ ed25519_key* wc_ed25519_new(void* heap, int devId, int *result_code); WOLFSSL_API int wc_ed25519_delete(ed25519_key* key, ed25519_key** key_p); #endif -WOLFSSL_API #ifdef HAVE_ED25519_KEY_IMPORT WOLFSSL_API diff --git a/wolfssl/wolfcrypt/logging.h b/wolfssl/wolfcrypt/logging.h index f34f34152..49de70147 100644 --- a/wolfssl/wolfcrypt/logging.h +++ b/wolfssl/wolfcrypt/logging.h @@ -174,7 +174,7 @@ WOLFSSL_API void wolfSSL_SetLoggingPrefix(const char* prefix); #define WOLFSSL_STUB(m) \ WOLFSSL_MSG(WOLFSSL_LOG_CAT(wolfSSL Stub, m, not implemented)) WOLFSSL_API int WOLFSSL_IS_DEBUG_ON(void); -#if defined(XVSNPRINTF) +#if defined(XVSNPRINTF) && !defined(NO_WOLFSSL_MSG_EX) WOLFSSL_API void WOLFSSL_MSG_EX(const char* fmt, ...); #define HAVE_WOLFSSL_MSG_EX #else diff --git a/wrapper/CSharp/wolfCrypt-Test/wolfCrypt-Test.cs b/wrapper/CSharp/wolfCrypt-Test/wolfCrypt-Test.cs index 920015080..fb9f39ebb 100644 --- a/wrapper/CSharp/wolfCrypt-Test/wolfCrypt-Test.cs +++ b/wrapper/CSharp/wolfCrypt-Test/wolfCrypt-Test.cs @@ -548,7 +548,7 @@ public class wolfCrypt_Test_CSharp IntPtr keyB = IntPtr.Zero; IntPtr publicKeyA = IntPtr.Zero; IntPtr publicKeyB = IntPtr.Zero; - byte[] derKey; + byte[] rawPub, rawPrivate, derKey; Console.WriteLine("\nStarting Curve25519 shared secret test..."); @@ -569,6 +569,14 @@ public class wolfCrypt_Test_CSharp } Console.WriteLine("Curve25519 Key generation test passed."); + + /* Export Public Key A private and public to raw format */ + wolfcrypt.Curve25519ExportKeyRaw(keyA, out rawPrivate, out rawPub); + /* Export Public Key B public to raw format */ + rawPub = wolfcrypt.Curve25519ExportPublicKey(keyB); + /* rawPub / rawPrivate - not used */ + + /* Export Public Key B to DER format */ Console.WriteLine("Exporting Public Key B to DER format..."); ret = wolfcrypt.Curve25519ExportPublicKeyToDer(keyB, out derKey, true); diff --git a/wrapper/CSharp/wolfSSL_CSharp/X509.cs b/wrapper/CSharp/wolfSSL_CSharp/X509.cs index 60800f0b2..279fa79ee 100644 --- a/wrapper/CSharp/wolfSSL_CSharp/X509.cs +++ b/wrapper/CSharp/wolfSSL_CSharp/X509.cs @@ -1,4 +1,25 @@ -using System; +/* X509.cs + * + * Copyright (C) 2006-2025 wolfSSL Inc. + * + * This file is part of wolfSSL. + * + * wolfSSL is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * wolfSSL is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA + */ + +using System; using System.Runtime.InteropServices; using System.Text; using System.Threading; @@ -9,6 +30,23 @@ namespace wolfSSL.CSharp { private const string wolfssl_dll = "wolfssl.dll"; +#if WindowsCE + [DllImport(wolfssl_dll)] + private extern static int wolfSSL_X509_get_pubkey_buffer(IntPtr x509, IntPtr buf, IntPtr bufSz); + [DllImport(wolfssl_dll)] + private extern static IntPtr wolfSSL_X509_get_der(IntPtr x509, IntPtr bufSz); + [DllImport(wolfssl_dll)] + private extern static void wolfSSL_X509_free(IntPtr x509); + [DllImport(wolfssl_dll)] + private extern static int wc_DerToPem(IntPtr der, int derSz, IntPtr pem, int pemSz, int type); + + [DllImport(wolfssl_dll)] + private extern static IntPtr wolfSSL_X509_get_name_oneline(IntPtr x509Name, IntPtr buf, int bufSz); + [DllImport(wolfssl_dll)] + private extern static IntPtr wolfSSL_X509_get_subject_name(IntPtr x509); + [DllImport(wolfssl_dll)] + private extern static IntPtr wolfSSL_X509_get_issuer_name(IntPtr x509); +#else [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] private extern static int wolfSSL_X509_get_pubkey_buffer(IntPtr x509, IntPtr buf, IntPtr bufSz); [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] @@ -25,6 +63,7 @@ namespace wolfSSL.CSharp private extern static IntPtr wolfSSL_X509_get_subject_name(IntPtr x509); [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] private extern static IntPtr wolfSSL_X509_get_issuer_name(IntPtr x509); +#endif private IntPtr x509; private int type; @@ -51,11 +90,12 @@ namespace wolfSSL.CSharp this.x509 = x509; ret = wolfSSL_X509_get_name_oneline( wolfSSL_X509_get_issuer_name(this.x509), IntPtr.Zero, 0); - this.Issuer = Marshal.PtrToStringAnsi(ret); + this.Issuer = wolfssl.PtrToStringAnsi(ret); ret = wolfSSL_X509_get_name_oneline( wolfSSL_X509_get_subject_name(this.x509), IntPtr.Zero, 0); - this.Subject = Marshal.PtrToStringAnsi(ret); + this.Subject = wolfssl.PtrToStringAnsi(ret); + this.isDynamic = isDynamic; } diff --git a/wrapper/CSharp/wolfSSL_CSharp/wolfCrypt.cs b/wrapper/CSharp/wolfSSL_CSharp/wolfCrypt.cs index c5249d1f3..8951fcb73 100644 --- a/wrapper/CSharp/wolfSSL_CSharp/wolfCrypt.cs +++ b/wrapper/CSharp/wolfSSL_CSharp/wolfCrypt.cs @@ -33,26 +33,66 @@ namespace wolfSSL.CSharp /******************************** * Init wolfSSL library */ +#if WindowsCE + [DllImport(wolfssl_dll)] + private extern static int wolfCrypt_Init(); + [DllImport(wolfssl_dll)] + private extern static int wolfCrypt_Cleanup(); +#else [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] private extern static int wolfCrypt_Init(); [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] private extern static int wolfCrypt_Cleanup(); +#endif /******************************** * Random */ +#if WindowsCE + [DllImport(wolfssl_dll)] + private extern static IntPtr wc_rng_new(IntPtr nonce, UInt32 nonceSz, IntPtr heap); + [DllImport(wolfssl_dll)] + private extern static void wc_rng_free(IntPtr rng); + [DllImport(wolfssl_dll)] + private extern static int wc_RNG_GenerateBlock(IntPtr rng, IntPtr output, UInt32 sz); +#else [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] private extern static IntPtr wc_rng_new(IntPtr nonce, UInt32 nonceSz, IntPtr heap); [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] private extern static void wc_rng_free(IntPtr rng); [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] private extern static int wc_RNG_GenerateBlock(IntPtr rng, IntPtr output, UInt32 sz); +#endif /******************************** * ECC */ +#if WindowsCE + [DllImport(wolfssl_dll)] + private extern static IntPtr wc_ecc_key_new(IntPtr heap); + [DllImport(wolfssl_dll)] + private extern static void wc_ecc_key_free(IntPtr key); + [DllImport(wolfssl_dll)] + private extern static int wc_ecc_set_rng(IntPtr key, IntPtr rng); + [DllImport(wolfssl_dll)] + private extern static int wc_ecc_make_key_ex(IntPtr rng, int keysize, IntPtr key, int curve_id); + [DllImport(wolfssl_dll)] + private extern static int wc_ecc_sign_hash(IntPtr hashPtr, uint hashlen, IntPtr sigPtr, IntPtr siglen, IntPtr rng, IntPtr key); + [DllImport(wolfssl_dll)] + private extern static int wc_ecc_verify_hash(IntPtr sigPtr, uint siglen, IntPtr hashPtr, uint hashlen, IntPtr res, IntPtr key); + + /* ASN.1 DER format */ + [DllImport(wolfssl_dll)] + private extern static int wc_EccPrivateKeyDecode(IntPtr keyBuf, IntPtr idx, IntPtr key, uint keyBufSz); + [DllImport(wolfssl_dll)] + private static extern int wc_EccPublicKeyDecode(byte[] input, ref uint inOutIdx, IntPtr key, uint inSz); + [DllImport(wolfssl_dll)] + private static extern int wc_EccPrivateKeyToDer(IntPtr key, byte[] output, uint inLen); + [DllImport(wolfssl_dll)] + private static extern int wc_EccPublicKeyToDer(IntPtr key, byte[] output, uint inLen, int with_AlgCurve); +#else [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] private extern static IntPtr wc_ecc_key_new(IntPtr heap); [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] @@ -75,11 +115,40 @@ namespace wolfSSL.CSharp private static extern int wc_EccPrivateKeyToDer(IntPtr key, byte[] output, uint inLen); [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] private static extern int wc_EccPublicKeyToDer(IntPtr key, byte[] output, uint inLen, int with_AlgCurve); +#endif /******************************** * ECIES */ +#if WindowsCE + [DllImport(wolfssl_dll)] + private extern static IntPtr wc_ecc_ctx_new(int flags, IntPtr rng); + [DllImport(wolfssl_dll)] + private extern static IntPtr wc_ecc_ctx_new_ex(int flags, IntPtr rng, IntPtr heap); + [DllImport(wolfssl_dll)] + private extern static void wc_ecc_ctx_free(IntPtr ctx); + [DllImport(wolfssl_dll)] + private extern static int wc_ecc_ctx_reset(IntPtr ctx, IntPtr rng); + [DllImport(wolfssl_dll)] + private extern static int wc_ecc_ctx_set_algo(IntPtr ctx, byte encAlgo, byte kdfAlgo, byte macAlgo); + [DllImport(wolfssl_dll)] + private extern static IntPtr wc_ecc_ctx_get_own_salt(IntPtr ctx); + [DllImport(wolfssl_dll)] + private extern static int wc_ecc_ctx_set_peer_salt(IntPtr ctx, IntPtr salt); + [DllImport(wolfssl_dll)] + private extern static int wc_ecc_ctx_set_own_salt(IntPtr ctx, IntPtr salt, uint sz); + [DllImport(wolfssl_dll)] + private extern static int wc_ecc_ctx_set_kdf_salt(IntPtr ctx, IntPtr salt, uint sz); + [DllImport(wolfssl_dll)] + private extern static int wc_ecc_ctx_set_info(IntPtr ctx, IntPtr info, int sz); + [DllImport(wolfssl_dll)] + private extern static int wc_ecc_encrypt(IntPtr privKey, IntPtr pubKey, IntPtr msg, uint msgSz, IntPtr outBuffer, IntPtr outSz, IntPtr ctx); + [DllImport(wolfssl_dll)] + private extern static int wc_ecc_encrypt_ex(IntPtr privKey, IntPtr pubKey, IntPtr msg, uint msgSz, IntPtr outBuffer, IntPtr outSz, IntPtr ctx, int compressed); + [DllImport(wolfssl_dll)] + private extern static int wc_ecc_decrypt(IntPtr privKey, IntPtr pubKey, IntPtr msg, uint msgSz, IntPtr outBuffer, IntPtr outSz, IntPtr ctx); +#else [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] private extern static IntPtr wc_ecc_ctx_new(int flags, IntPtr rng); [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] @@ -106,18 +175,58 @@ namespace wolfSSL.CSharp private extern static int wc_ecc_encrypt_ex(IntPtr privKey, IntPtr pubKey, IntPtr msg, uint msgSz, IntPtr outBuffer, IntPtr outSz, IntPtr ctx, int compressed); [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] private extern static int wc_ecc_decrypt(IntPtr privKey, IntPtr pubKey, IntPtr msg, uint msgSz, IntPtr outBuffer, IntPtr outSz, IntPtr ctx); +#endif + /******************************** * ECDHE */ +#if WindowsCE + [DllImport(wolfssl_dll)] + private extern static int wc_ecc_shared_secret(IntPtr privateKey, IntPtr publicKey, byte[] outSharedSecret, ref int outlen); +#else [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] private extern static int wc_ecc_shared_secret(IntPtr privateKey, IntPtr publicKey, byte[] outSharedSecret, ref int outlen); +#endif /******************************** * RSA */ +#if WindowsCE + [DllImport(wolfssl_dll)] + private static extern IntPtr wc_NewRsaKey(IntPtr heap, int devId, IntPtr result_code); + [DllImport(wolfssl_dll)] + private static extern int wc_DeleteRsaKey(IntPtr key, IntPtr key_p); + [DllImport(wolfssl_dll)] + private extern static int wc_InitRsaKey(IntPtr key, IntPtr heap); + [DllImport(wolfssl_dll)] + private extern static void wc_FreeRsaKey(IntPtr key); + [DllImport(wolfssl_dll)] + private extern static int wc_MakeRsaKey(IntPtr key, int keysize, Int32 exponent, IntPtr rng); + [DllImport(wolfssl_dll)] + private extern static int wc_RsaSSL_Sign(IntPtr hashPtr, int hashLen, IntPtr sigPtr, int sigLen, IntPtr key, IntPtr rng); + [DllImport(wolfssl_dll)] + private extern static int wc_RsaSSL_Verify(IntPtr sigPtr, int sigLen, IntPtr hashPtr, int hashLen, IntPtr key); + + /* ASN.1 DER format */ + [DllImport(wolfssl_dll)] + private extern static int wc_RsaPublicEncrypt(IntPtr inPtr, int inLen, IntPtr outPtr, int outLen, IntPtr key); + [DllImport(wolfssl_dll)] + private extern static int wc_RsaPrivateDecrypt(IntPtr inPtr, int inLen, IntPtr outPtr, int outLen, IntPtr key); + [DllImport(wolfssl_dll)] + private extern static int wc_RsaPrivateKeyDecode(IntPtr keyBuf, IntPtr idx, IntPtr key, uint keyBufSz); + [DllImport(wolfssl_dll)] + private extern static int wc_RsaPublicKeyDecode(IntPtr keyBuf, IntPtr idx, IntPtr key, uint keyBufSz); + + [DllImport(wolfssl_dll)] + private extern static int wc_RsaPSS_Sign(IntPtr hashPtr, int hashLen, IntPtr sigPtr, int sigLen, int hashType, IntPtr rng, IntPtr key); + [DllImport(wolfssl_dll)] + private extern static int wc_RsaPSS_Verify(IntPtr sigPtr, int sigLen, IntPtr hashPtr, int hashLen, int hashType, IntPtr key); + [DllImport(wolfssl_dll)] + private extern static int wc_RsaPSS_CheckPadding(IntPtr sigPtr, int sigLen, int hashType, IntPtr key); +#else [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr wc_NewRsaKey(IntPtr heap, int devId, IntPtr result_code); [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] @@ -149,11 +258,52 @@ namespace wolfSSL.CSharp private extern static int wc_RsaPSS_Verify(IntPtr sigPtr, int sigLen, IntPtr hashPtr, int hashLen, int hashType, IntPtr key); [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] private extern static int wc_RsaPSS_CheckPadding(IntPtr sigPtr, int sigLen, int hashType, IntPtr key); +#endif /******************************** * ED25519 */ +#if WindowsCE + [DllImport(wolfssl_dll)] + private static extern IntPtr wc_ed25519_new(IntPtr heap, int devId, IntPtr result_code); + [DllImport(wolfssl_dll)] + private static extern int wc_ed25519_delete(IntPtr key, IntPtr key_p); + [DllImport(wolfssl_dll)] + private static extern int wc_ed25519_init(IntPtr key); + [DllImport(wolfssl_dll)] + private static extern void wc_ed25519_free(IntPtr key); + [DllImport(wolfssl_dll)] + private static extern int wc_ed25519_make_key(IntPtr rng, int keysize, IntPtr key); + [DllImport(wolfssl_dll)] + private static extern int wc_ed25519_sign_msg(IntPtr inMsg, uint inlen, IntPtr outMsg, ref uint outlen, IntPtr key); + [DllImport(wolfssl_dll)] + private static extern int wc_ed25519_verify_msg(IntPtr sig, uint siglen, IntPtr msg, uint msgLen, ref int ret, IntPtr key); + + /* ASN.1 DER format */ + [DllImport(wolfssl_dll)] + private static extern int wc_Ed25519PrivateKeyDecode(byte[] input, ref uint inOutIdx, IntPtr key, uint inSz); + [DllImport(wolfssl_dll)] + private static extern int wc_Ed25519PublicKeyDecode(byte[] input, ref uint inOutIdx, IntPtr key, uint inSz); + [DllImport(wolfssl_dll)] + private static extern int wc_Ed25519KeyToDer(IntPtr key, byte[] output, uint inLen); + [DllImport(wolfssl_dll)] + private static extern int wc_Ed25519PrivateKeyToDer(IntPtr key, byte[] output, uint inLen); + [DllImport(wolfssl_dll)] + private static extern int wc_Ed25519PublicKeyToDer(IntPtr key, byte[] output, uint inLen, int withAlg); + + /* RAW format */ + [DllImport(wolfssl_dll)] + private static extern int wc_ed25519_make_public(IntPtr key, IntPtr pubKey, uint pubKeySz); + [DllImport(wolfssl_dll)] + private static extern int wc_ed25519_import_public(IntPtr inMsg, uint inLen, IntPtr key); + [DllImport(wolfssl_dll)] + private static extern int wc_ed25519_export_public(IntPtr key, IntPtr outMsg, ref uint outLen); + [DllImport(wolfssl_dll)] + private static extern int wc_ed25519_export_private(IntPtr key, IntPtr outMsg, ref uint outLen); + [DllImport(wolfssl_dll)] + private static extern int wc_ed25519_size(IntPtr key); +#else [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr wc_ed25519_new(IntPtr heap, int devId, IntPtr result_code); [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] @@ -192,11 +342,52 @@ namespace wolfSSL.CSharp private static extern int wc_ed25519_export_private(IntPtr key, IntPtr outMsg, ref uint outLen); [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] private static extern int wc_ed25519_size(IntPtr key); +#endif /******************************** * Curve25519 */ +#if WindowsCE + [DllImport(wolfssl_dll)] + private static extern IntPtr wc_curve25519_new(IntPtr heap, int devId, IntPtr result_code); + [DllImport(wolfssl_dll)] + private static extern int wc_curve25519_delete(IntPtr key, IntPtr key_p); + [DllImport(wolfssl_dll)] + private extern static int wc_curve25519_init(IntPtr key); + [DllImport(wolfssl_dll)] + private extern static void wc_curve25519_free(IntPtr key); + [DllImport(wolfssl_dll)] + private extern static int wc_curve25519_make_key(IntPtr rng, int keysize, IntPtr key); + [DllImport(wolfssl_dll)] + private extern static int wc_curve25519_shared_secret(IntPtr privateKey, IntPtr publicKey, byte[] outSharedSecret, ref int outlen); + + /* ASN.1 DER format */ + [DllImport(wolfssl_dll)] + private static extern int wc_Curve25519PrivateKeyDecode(byte[] input, ref uint inOutIdx, IntPtr key, uint inSz); + [DllImport(wolfssl_dll)] + private static extern int wc_Curve25519PublicKeyDecode(byte[] input, ref uint inOutIdx, IntPtr key, uint inSz); + [DllImport(wolfssl_dll)] + private static extern int wc_Curve25519PrivateKeyToDer(IntPtr key, byte[] output, uint inLen); + [DllImport(wolfssl_dll)] + private static extern int wc_Curve25519PublicKeyToDer(IntPtr key, byte[] output, uint inLen, int withAlg); + + /* RAW format */ + [DllImport(wolfssl_dll)] + private extern static int wc_curve25519_import_private(IntPtr privKey, int privKeySz, IntPtr key); + [DllImport(wolfssl_dll)] + private static extern int wc_curve25519_export_public(IntPtr key, byte[] outBuffer, ref uint outLen); + [DllImport(wolfssl_dll)] + private extern static int wc_curve25519_import_public(IntPtr pubKey, int pubKeySz, IntPtr key); + [DllImport(wolfssl_dll)] + private extern static int wc_curve25519_export_public(IntPtr key, IntPtr outPubKey, ref int outlen); + [DllImport(wolfssl_dll)] + private static extern int wc_curve25519_export_key_raw(IntPtr key, byte[] priv, ref uint privSz, byte[] pub, ref uint pubSz); + [DllImport(wolfssl_dll)] + private extern static int wc_curve25519_import_private_raw(IntPtr privKey, IntPtr pubKey, IntPtr key); + [DllImport(wolfssl_dll)] + private extern static int wc_curve25519_export_private_raw(IntPtr key, IntPtr outPrivKey, IntPtr outPubKey); +#else [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr wc_curve25519_new(IntPtr heap, int devId, IntPtr result_code); [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] @@ -235,11 +426,30 @@ namespace wolfSSL.CSharp private extern static int wc_curve25519_import_private_raw(IntPtr privKey, IntPtr pubKey, IntPtr key); [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] private extern static int wc_curve25519_export_private_raw(IntPtr key, IntPtr outPrivKey, IntPtr outPubKey); +#endif /******************************** * AES-GCM */ +#if WindowsCE + [DllImport(wolfssl_dll)] + private extern static IntPtr wc_AesNew(IntPtr heap, int devId, IntPtr result_code); + [DllImport(wolfssl_dll)] + private extern static int wc_AesDelete(IntPtr aes, IntPtr aes_p); + [DllImport(wolfssl_dll)] + private extern static int wc_AesFree(IntPtr aes); + [DllImport(wolfssl_dll)] + private extern static int wc_AesInit(IntPtr aes, IntPtr heap, int devId); + [DllImport(wolfssl_dll)] + private extern static int wc_AesGcmInit(IntPtr aes, IntPtr key, uint len, IntPtr iv, uint ivSz); + [DllImport(wolfssl_dll)] + private extern static int wc_AesGcmSetKey(IntPtr aes, IntPtr key, uint len); + [DllImport(wolfssl_dll)] + private extern static int wc_AesGcmEncrypt(IntPtr aes, IntPtr output, IntPtr input, uint sz, IntPtr iv, uint ivSz, IntPtr authTag, uint authTagSz, IntPtr authIn, uint authInSz); + [DllImport(wolfssl_dll)] + private extern static int wc_AesGcmDecrypt(IntPtr aes, IntPtr output, IntPtr input, uint sz, IntPtr iv, uint ivSz, IntPtr authTag, uint authTagSz, IntPtr authIn, uint authInSz); +#else [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] private extern static IntPtr wc_AesNew(IntPtr heap, int devId, IntPtr result_code); [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] @@ -256,11 +466,28 @@ namespace wolfSSL.CSharp private extern static int wc_AesGcmEncrypt(IntPtr aes, IntPtr output, IntPtr input, uint sz, IntPtr iv, uint ivSz, IntPtr authTag, uint authTagSz, IntPtr authIn, uint authInSz); [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] private extern static int wc_AesGcmDecrypt(IntPtr aes, IntPtr output, IntPtr input, uint sz, IntPtr iv, uint ivSz, IntPtr authTag, uint authTagSz, IntPtr authIn, uint authInSz); +#endif /******************************** * HASH */ +#if WindowsCE + [DllImport(wolfssl_dll)] + private extern static IntPtr wc_HashNew(uint hashType, IntPtr heap, int devId, IntPtr result_code); + [DllImport(wolfssl_dll)] + private extern static int wc_HashDelete(IntPtr hash, IntPtr hash_p); + [DllImport(wolfssl_dll)] + private extern static int wc_HashInit(IntPtr hash, uint hashType); + [DllImport(wolfssl_dll)] + private extern static int wc_HashUpdate(IntPtr hash, uint hashType, IntPtr data, uint dataSz); + [DllImport(wolfssl_dll)] + private extern static int wc_HashFinal(IntPtr hash, uint hashType, IntPtr output); + [DllImport(wolfssl_dll)] + private extern static int wc_HashFree(IntPtr hash, uint hashType); + [DllImport(wolfssl_dll)] + private extern static int wc_HashGetDigestSize(uint hashType); +#else [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] private extern static IntPtr wc_HashNew(uint hashType, IntPtr heap, int devId, IntPtr result_code); [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] @@ -275,15 +502,21 @@ namespace wolfSSL.CSharp private extern static int wc_HashFree(IntPtr hash, uint hashType); [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] private extern static int wc_HashGetDigestSize(uint hashType); +#endif /******************************** * Logging */ +#if WindowsCE + [DllImport(wolfssl_dll)] + private extern static IntPtr wc_GetErrorString(int error); + public delegate void loggingCb(int lvl, string msg); +#else [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] private extern static IntPtr wc_GetErrorString(int error); - public delegate void loggingCb(int lvl, StringBuilder msg); +#endif private static loggingCb internal_log; /// @@ -291,6 +524,15 @@ namespace wolfSSL.CSharp /// /// Level of log message /// Message to log +#if WindowsCE + private static void log(int lvl, string msg) + { + /* if log is not set then print nothing */ + if (internal_log == null) + return; + internal_log(lvl, msg); + } +#else private static void log(int lvl, string msg) { /* if log is not set then print nothing */ @@ -299,6 +541,7 @@ namespace wolfSSL.CSharp StringBuilder ptr = new StringBuilder(msg); internal_log(lvl, ptr); } +#endif /******************************** @@ -312,7 +555,7 @@ namespace wolfSSL.CSharp public static readonly int OTHER_LOG = 4; public static readonly int INVALID_DEVID = -2; public static readonly int ECC_MAX_SIG_SIZE = 141; /* ECC max sig size */ - public static readonly int ECC_KEY_SIZE = 32; /* ECC key size */ + public static readonly int ECC_KEY_SIZE = 32; /* ECC key size */ public static readonly int MAX_ECIES_TEST_SZ = 200; /* ECIES max sig size */ public static readonly int ED25519_SIG_SIZE = 64; /* ED25519 pub + priv */ public static readonly int ED25519_KEY_SIZE = 32; /* Private key only */ @@ -1645,7 +1888,7 @@ namespace wolfSSL.CSharp if (inMsgPtr != IntPtr.Zero) Marshal.FreeHGlobal(inMsgPtr); if (outMsgPtr != IntPtr.Zero) Marshal.FreeHGlobal(outMsgPtr); } - + return ret; } @@ -1900,11 +2143,11 @@ namespace wolfSSL.CSharp **********************************************************************/ /// - /// Initialize an ED25519 key. - /// - /// Buffer to receive the initialized key - /// 0 on success, otherwise an error code - public static int Ed25519InitKey(out IntPtr key) + /// Initialize an ED25519 key. + /// + /// Buffer to receive the initialized key + /// 0 on success, otherwise an error code + public static int Ed25519InitKey(out IntPtr key) { key = IntPtr.Zero; try @@ -1966,7 +2209,7 @@ namespace wolfSSL.CSharp } catch (Exception ex) { - Console.WriteLine($"Exception in EdImportPublic: {ex.Message}"); + Console.WriteLine("Exception in EdImportPublic: " + ex.Message); return EXCEPTION_E; } @@ -2425,15 +2668,17 @@ namespace wolfSSL.CSharp return publicKey; } + /// /// Export both private and public keys from a Curve25519 key structure /// /// Curve25519 key structure - /// A tuple containing the private key and public key as byte arrays - public static (byte[] privateKey, byte[] publicKey) Curve25519ExportKeyRaw(IntPtr key) + /// returned raw private key as byte array + /// returned raw public key as byte array + public static void Curve25519ExportKeyRaw(IntPtr key, out byte[] privateKey, out byte[] publicKey) { - byte[] privateKey = new byte[ED25519_KEY_SIZE]; - byte[] publicKey = new byte[ED25519_PUB_KEY_SIZE]; + privateKey = new byte[ED25519_KEY_SIZE]; + publicKey = new byte[ED25519_PUB_KEY_SIZE]; uint privSize = (uint)privateKey.Length; uint pubSize = (uint)publicKey.Length; int ret = wc_curve25519_export_key_raw(key, privateKey, ref privSize, publicKey, ref pubSize); @@ -2441,7 +2686,7 @@ namespace wolfSSL.CSharp { throw new Exception("Failed to export Curve25519 keys. Error code: " + ret); } - return (privateKey, publicKey); + return; } /* END RAW Curve25519 */ @@ -2470,9 +2715,9 @@ namespace wolfSSL.CSharp } } - catch (Exception e) + catch (Exception ex) { - Console.WriteLine($"AES context creation failed: {e.Message}"); + Console.WriteLine("AES context creation failed: " + ex.Message); } return aesPtr; @@ -2498,7 +2743,7 @@ namespace wolfSSL.CSharp ret = wc_AesGcmSetKey(aes, keyPtr, (uint)key.Length); if (ret != 0) { - throw new Exception($"AES-GCM initialization failed with error code {ret}"); + throw new Exception("AES-GCM initialization failed with error code ret = " + ret.ToString()); } } finally @@ -2534,7 +2779,7 @@ namespace wolfSSL.CSharp ret = wc_AesGcmInit(aes, keyPtr, (uint)key.Length, ivPtr, (uint)iv.Length); if (ret != 0) { - throw new Exception($"AES-GCM initialization failed with error code {ret}"); + throw new Exception("AES-GCM initialization failed with error code ret = " + ret.ToString()); } } finally @@ -2557,7 +2802,7 @@ namespace wolfSSL.CSharp /// Buffer to receive the authentication tag /// 0 on success, otherwise an error code public static int AesGcmEncrypt(IntPtr aes, byte[] iv, byte[] plaintext, - byte[] ciphertext, byte[] authTag, byte[] addAuth = null) + byte[] ciphertext, byte[] authTag, byte[] addAuth) { int ret; IntPtr ivPtr = IntPtr.Zero; @@ -2613,6 +2858,11 @@ namespace wolfSSL.CSharp return ret; } + public static int AesGcmEncrypt(IntPtr aes, byte[] iv, byte[] plaintext, + byte[] ciphertext, byte[] authTag) + { + return AesGcmEncrypt(aes, iv, plaintext, ciphertext, null); + } /// /// Decrypt data using AES-GCM @@ -2624,7 +2874,7 @@ namespace wolfSSL.CSharp /// Authentication tag for verification /// 0 on success, otherwise an error code public static int AesGcmDecrypt(IntPtr aes, byte[] iv, byte[] ciphertext, - byte[] plaintext, byte[] authTag, byte[] addAuth = null) + byte[] plaintext, byte[] authTag, byte[] addAuth) { int ret; IntPtr ivPtr = IntPtr.Zero; @@ -2680,6 +2930,11 @@ namespace wolfSSL.CSharp return ret; } + public static int AesGcmDecrypt(IntPtr aes, byte[] iv, byte[] ciphertext, + byte[] plaintext, byte[] authTag) + { + return AesGcmDecrypt(aes, iv, ciphertext, plaintext, authTag, null); + } /// /// Free AES-GCM context @@ -2747,7 +3002,7 @@ namespace wolfSSL.CSharp ret = wc_HashInit(hash, hashType); if (ret != 0) { - throw new Exception($"Failed to initialize hash context. Error code: {ret}"); + throw new Exception("Failed to initialize hash context. Error code: ret = " + ret.ToString()); } } catch (Exception e) @@ -2791,7 +3046,7 @@ namespace wolfSSL.CSharp ret = wc_HashUpdate(hash, hashType, dataPtr, (uint)data.Length); if (ret != 0) { - throw new Exception($"Failed to update hash. Error code: {ret}"); + throw new Exception("Failed to update hash. Error code: ret = " + ret.ToString()); } } catch (Exception e) @@ -2837,7 +3092,7 @@ namespace wolfSSL.CSharp ret = wc_HashFinal(hash, hashType, outputPtr); if (ret != 0) { - throw new Exception($"Failed to finalize hash. Error code: {ret}"); + throw new Exception("Failed to finalize hash. Error code: ret = " + ret.ToString()); } Marshal.Copy(outputPtr, output, 0, hashSize); @@ -2877,7 +3132,7 @@ namespace wolfSSL.CSharp hash = IntPtr.Zero; if (ret != 0) { - throw new Exception($"Failed to free hash context. Error code: {ret}"); + throw new Exception("Failed to free hash context. Error code: ret = " + ret.ToString()); } } catch (Exception e) @@ -2938,7 +3193,7 @@ namespace wolfSSL.CSharp try { IntPtr errStr = wc_GetErrorString(error); - return Marshal.PtrToStringAnsi(errStr); + return wolfssl.PtrToStringAnsi(errStr); } catch (Exception e) { diff --git a/wrapper/CSharp/wolfSSL_CSharp/wolfSSL.cs b/wrapper/CSharp/wolfSSL_CSharp/wolfSSL.cs index a493e722c..0153b8ab0 100644 --- a/wrapper/CSharp/wolfSSL_CSharp/wolfSSL.cs +++ b/wrapper/CSharp/wolfSSL_CSharp/wolfSSL.cs @@ -28,7 +28,8 @@ using System.IO; using System.Net; using System.Net.Sockets; -namespace wolfSSL.CSharp { +namespace wolfSSL.CSharp +{ public class wolfssl { private const string wolfssl_dll = "wolfssl.dll"; @@ -36,6 +37,64 @@ namespace wolfSSL.CSharp { /* wait for 6 seconds default on TCP socket state poll if timeout not set */ private const int WC_WAIT = 6000000; + /******************************** + * Utility String Conversion functions + */ +#if WindowsCE + /// + /// Convert unicode string to ASCII + /// + public static string UnicodeToAscii(string msg) + { + if (msg == null) + return null; + /* Convert Unicode to Bytes */ + byte[] bytes = Encoding.Unicode.GetBytes((string)msg.ToString()); + /* Convert to ASCII */ + return Encoding.ASCII.GetString(bytes, 0, bytes.Length); + } + + /// + /// Convert string to Unicode + /// + public static string AsciiToUnicode(string msg) + { + if (msg == null) + return null; + /* Convert ASCII to Bytes */ + byte[] bytes = Encoding.ASCII.GetBytes((string)msg.ToString()); + /* Convert to Unicode */ + return Encoding.Unicode.GetString(bytes, 0, bytes.Length); + } + + /// + /// WinCE version of Marshal for Unicode or Multi-byte pointer to ASCII string + /// + public static string PtrToStringAnsi(IntPtr ptr) + { + if (ptr == IntPtr.Zero) + return null; + + /* Estimate string length */ + int len = 0; + while (Marshal.ReadByte(ptr, len) != 0) len++; + + if (len == 0) + return string.Empty; + + byte[] buffer = new byte[len]; + Marshal.Copy(ptr, buffer, 0, len); + + return Encoding.ASCII.GetString(buffer, 0, len); + } +#else + public static string PtrToStringAnsi(IntPtr ptr) + { + return Marshal.PtrToStringAnsi(ptr); + } +#endif + + /******************************** * Class for DTLS connections */ @@ -236,15 +295,39 @@ namespace wolfSSL.CSharp { /******************************** * Init wolfSSL library */ +#if WindowsCE + [DllImport(wolfssl_dll)] + private extern static int wolfSSL_Init(); + [DllImport(wolfssl_dll)] + private extern static int wolfSSL_Cleanup(); +#else [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] private extern static int wolfSSL_Init(); [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] private extern static int wolfSSL_Cleanup(); - +#endif /******************************** * Methods of connection */ +#if WindowsCE + [DllImport(wolfssl_dll)] + private extern static IntPtr wolfTLSv1_2_server_method(); + [DllImport(wolfssl_dll)] + private extern static IntPtr wolfTLSv1_3_server_method(); + [DllImport(wolfssl_dll)] + private extern static IntPtr wolfSSLv23_server_method(); + [DllImport(wolfssl_dll)] + private extern static IntPtr wolfTLSv1_2_client_method(); + [DllImport(wolfssl_dll)] + private extern static IntPtr wolfTLSv1_3_client_method(); + [DllImport(wolfssl_dll)] + private extern static IntPtr wolfSSLv23_client_method(); + [DllImport(wolfssl_dll)] + private extern static IntPtr wolfDTLSv1_2_server_method(); + [DllImport(wolfssl_dll)] + private extern static IntPtr wolfDTLSv1_2_client_method(); +#else [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] private extern static IntPtr wolfTLSv1_2_server_method(); [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] @@ -261,11 +344,28 @@ namespace wolfSSL.CSharp { private extern static IntPtr wolfDTLSv1_2_server_method(); [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] private extern static IntPtr wolfDTLSv1_2_client_method(); +#endif /******************************** - * Call backs + * Callbacks */ +#if WindowsCE + public delegate int CallbackIORecv_delegate(IntPtr ssl, IntPtr buf, int sz, IntPtr ctx); + [DllImport(wolfssl_dll)] + private extern static int wolfSSL_CTX_SetIORecv(IntPtr ctx, CallbackIORecv_delegate recv); + [DllImport(wolfssl_dll)] + private extern static int wolfSSL_SetIOReadCtx(IntPtr ssl, IntPtr rctx); + [DllImport(wolfssl_dll)] + private extern static IntPtr wolfSSL_GetIOReadCtx(IntPtr ssl); + public delegate int CallbackIOSend_delegate(IntPtr ssl, IntPtr buf, int sz, IntPtr ctx); + [DllImport(wolfssl_dll)] + private extern static int wolfSSL_CTX_SetIOSend(IntPtr ctx, CallbackIOSend_delegate send); + [DllImport(wolfssl_dll)] + private extern static int wolfSSL_SetIOWriteCtx(IntPtr ssl, IntPtr wctx); + [DllImport(wolfssl_dll)] + private extern static IntPtr wolfSSL_GetIOWriteCtx(IntPtr ssl); +#else [UnmanagedFunctionPointer(CallingConvention.Cdecl)] public delegate int CallbackIORecv_delegate(IntPtr ssl, IntPtr buf, int sz, IntPtr ctx); [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] @@ -283,11 +383,26 @@ namespace wolfSSL.CSharp { private extern static int wolfSSL_SetIOWriteCtx(IntPtr ssl, IntPtr wctx); [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] private extern static IntPtr wolfSSL_GetIOWriteCtx(IntPtr ssl); +#endif /******************************** * CTX structure */ +#if WindowsCE + [DllImport(wolfssl_dll)] + private extern static IntPtr wolfSSL_CTX_new(IntPtr method); + [DllImport(wolfssl_dll)] + private extern static int wolfSSL_CTX_use_certificate_file(IntPtr ctx, string file, int type); + [DllImport(wolfssl_dll)] + private extern static int wolfSSL_CTX_load_verify_locations(IntPtr ctx, string file, string path); + [DllImport(wolfssl_dll)] + private extern static int wolfSSL_CTX_load_verify_locations_ex(IntPtr ctx, string file, string path, int flags); + [DllImport(wolfssl_dll)] + private extern static int wolfSSL_CTX_use_PrivateKey_file(IntPtr ctx, string file, int type); + [DllImport(wolfssl_dll)] + private extern static void wolfSSL_CTX_free(IntPtr ctx); +#else [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] private extern static IntPtr wolfSSL_CTX_new(IntPtr method); [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] @@ -295,15 +410,29 @@ namespace wolfSSL.CSharp { [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] private extern static int wolfSSL_CTX_load_verify_locations(IntPtr ctx, string file, string path); [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] + private extern static int wolfSSL_CTX_load_verify_locations_ex(IntPtr ctx, string file, string path, int flags); + [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] private extern static int wolfSSL_CTX_use_PrivateKey_file(IntPtr ctx, string file, int type); [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] private extern static void wolfSSL_CTX_free(IntPtr ctx); - +#endif /******************************** * PSK */ +#if WindowsCE + public delegate uint psk_delegate(IntPtr ssl, string identity, IntPtr key, uint max_sz); + public delegate uint psk_client_delegate(IntPtr ssl, string hint, IntPtr identity, uint id_max_len, IntPtr key, uint max_sz); + [DllImport(wolfssl_dll)] + private extern static void wolfSSL_set_psk_server_callback(IntPtr ssl, psk_delegate psk_cb); + [DllImport(wolfssl_dll)] + private extern static void wolfSSL_CTX_set_psk_server_callback(IntPtr ctx, psk_delegate psk_cb); + [DllImport(wolfssl_dll)] + private extern static void wolfSSL_CTX_set_psk_client_callback(IntPtr ctx, psk_client_delegate psk_cb); + [DllImport(wolfssl_dll)] + private extern static int wolfSSL_CTX_use_psk_identity_hint(IntPtr ctx, string identity); +#else [UnmanagedFunctionPointer(CallingConvention.Cdecl)] public delegate uint psk_delegate(IntPtr ssl, string identity, IntPtr key, uint max_sz); [UnmanagedFunctionPointer(CallingConvention.Cdecl)] @@ -316,10 +445,26 @@ namespace wolfSSL.CSharp { private extern static void wolfSSL_CTX_set_psk_client_callback(IntPtr ctx, psk_client_delegate psk_cb); [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] private extern static int wolfSSL_CTX_use_psk_identity_hint(IntPtr ctx, StringBuilder identity); +#endif /******************************** * SNI */ +#if WindowsCE + public delegate int sni_delegate(IntPtr ssl, IntPtr ret, IntPtr exArg); + [DllImport(wolfssl_dll)] + private extern static void wolfSSL_CTX_set_servername_callback(IntPtr ctx, sni_delegate sni_cb); + [DllImport(wolfssl_dll)] + private extern static int wolfSSL_CTX_set_servername_arg(IntPtr ctx, IntPtr arg); + [DllImport(wolfssl_dll)] + private extern static int wolfSSL_CTX_UseSNI(IntPtr ctx, byte type, IntPtr data, ushort size); + [DllImport(wolfssl_dll)] + private extern static int wolfSSL_UseSNI(IntPtr ssl, byte type, IntPtr data, ushort size); + [DllImport(wolfssl_dll)] + private extern static ushort wolfSSL_SNI_GetRequest(IntPtr ssl, byte type, ref IntPtr data); + [DllImport(wolfssl_dll)] + private extern static int wolfSSL_SNI_GetFromBuffer(byte[] clientHello, uint helloSz, byte type, IntPtr sni, IntPtr inOutSz); +#else [UnmanagedFunctionPointer(CallingConvention.Cdecl)] public delegate int sni_delegate(IntPtr ssl, IntPtr ret, IntPtr exArg); [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] @@ -334,10 +479,27 @@ namespace wolfSSL.CSharp { private extern static ushort wolfSSL_SNI_GetRequest(IntPtr ssl, byte type, ref IntPtr data); [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] private extern static int wolfSSL_SNI_GetFromBuffer(byte[] clientHello, uint helloSz, byte type, IntPtr sni, IntPtr inOutSz); +#endif /******************************** * SSL Structure */ +#if WindowsCE + [DllImport(wolfssl_dll)] + private extern static IntPtr wolfSSL_new(IntPtr ctx); + [DllImport(wolfssl_dll)] + private extern static int wolfSSL_accept(IntPtr ssl); + [DllImport(wolfssl_dll)] + private extern static int wolfSSL_connect(IntPtr ssl); + [DllImport(wolfssl_dll)] + private extern static int wolfSSL_read(IntPtr ssl, IntPtr buf, int sz); + [DllImport(wolfssl_dll)] + private extern static int wolfSSL_write(IntPtr ssl, IntPtr buf, int sz); + [DllImport(wolfssl_dll)] + private extern static int wolfSSL_shutdown(IntPtr ssl); + [DllImport(wolfssl_dll)] + private extern static void wolfSSL_free(IntPtr ssl); +#else [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] private extern static IntPtr wolfSSL_new(IntPtr ctx); [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] @@ -352,11 +514,30 @@ namespace wolfSSL.CSharp { private extern static int wolfSSL_shutdown(IntPtr ssl); [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] private extern static void wolfSSL_free(IntPtr ssl); - +#endif /******************************** * Cipher lists */ +#if WindowsCE + /* only supports full name from cipher_name[] delimited by : */ + [DllImport(wolfssl_dll)] + private extern static int wolfSSL_CTX_set_cipher_list(IntPtr ctx, string ciphers); + [DllImport(wolfssl_dll)] + private extern static int wolfSSL_set_cipher_list(IntPtr ssl, string ciphers); + [DllImport(wolfssl_dll)] + private extern static int wolfSSL_get_ciphers(string ciphers, int sz); + [DllImport(wolfssl_dll)] + private extern static IntPtr wolfSSL_get_cipher(IntPtr ssl); + [DllImport(wolfssl_dll)] + private extern static IntPtr wolfSSL_CIPHER_get_name(IntPtr cipher); + [DllImport(wolfssl_dll)] + private extern static IntPtr wolfSSL_get_current_cipher(IntPtr ssl); + [DllImport(wolfssl_dll)] + private extern static IntPtr wolfSSL_get_version(IntPtr ssl); + [DllImport(wolfssl_dll)] + private extern static IntPtr wolfSSL_get_cipher_list(IntPtr ssl); +#else /* only supports full name from cipher_name[] delimited by : */ [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] private extern static int wolfSSL_CTX_set_cipher_list(IntPtr ctx, StringBuilder ciphers); @@ -374,27 +555,50 @@ namespace wolfSSL.CSharp { private extern static IntPtr wolfSSL_get_version(IntPtr ssl); [DllImport(wolfssl_dll, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)] private extern static IntPtr wolfSSL_get_cipher_list(IntPtr ssl); - +#endif /******************************** * Error logging */ +#if WindowsCE + [DllImport(wolfssl_dll)] + private extern static IntPtr wolfSSL_ERR_error_string(uint err, StringBuilder errOut); + [DllImport(wolfssl_dll)] + private extern static int wolfSSL_get_error(IntPtr ssl, int err); + public delegate void loggingCb(int lvl, string msg); + [DllImport(wolfssl_dll)] + private extern static void wolfSSL_Debugging_ON(); + [DllImport(wolfssl_dll)] + private extern static void wolfSSL_Debugging_OFF(); + [DllImport(wolfssl_dll)] + private extern static int wolfSSL_SetLoggingCb(loggingCb vc); +#else [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] private extern static IntPtr wolfSSL_ERR_error_string(uint err, StringBuilder errOut); [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] private extern static int wolfSSL_get_error(IntPtr ssl, int err); [UnmanagedFunctionPointer(CallingConvention.Cdecl)] public delegate void loggingCb(int lvl, StringBuilder msg); - private static loggingCb internal_log; [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] private extern static void wolfSSL_Debugging_ON(); [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] private extern static void wolfSSL_Debugging_OFF(); - + [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] + private extern static int wolfSSL_SetLoggingCb(loggingCb vc); +#endif + private static loggingCb internal_log; /******************************** * DH */ +#if WindowsCE + [DllImport(wolfssl_dll)] + private extern static int wolfSSL_CTX_SetMinDhKey_Sz(IntPtr ctx, short size); + [DllImport(wolfssl_dll)] + private extern static int wolfSSL_SetTmpDH_file(IntPtr ssl, string dhParam, int type); + [DllImport(wolfssl_dll)] + private extern static int wolfSSL_CTX_SetTmpDH_file(IntPtr ctx, string dhParam, int type); +#else [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] private extern static int wolfSSL_CTX_SetMinDhKey_Sz(IntPtr ctx, short size); [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] @@ -402,21 +606,44 @@ namespace wolfSSL.CSharp { [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] private extern static int wolfSSL_CTX_SetTmpDH_file(IntPtr ctx, StringBuilder dhParam, int type); +#endif /******************************** * Verify Callback */ +#if WindowsCE + public delegate int CallbackVerify_delegate(int ret, IntPtr x509_ctx); + [DllImport(wolfssl_dll)] + private extern static void wolfSSL_CTX_set_verify(IntPtr ctx, int mode, CallbackVerify_delegate vc); + [DllImport(wolfssl_dll)] + private extern static void wolfSSL_set_verify(IntPtr ssl, int mode, CallbackVerify_delegate vc); +#else [UnmanagedFunctionPointer(CallingConvention.Cdecl)] public delegate int CallbackVerify_delegate(int ret, IntPtr x509_ctx); [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] private extern static void wolfSSL_CTX_set_verify(IntPtr ctx, int mode, CallbackVerify_delegate vc); [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] private extern static void wolfSSL_set_verify(IntPtr ssl, int mode, CallbackVerify_delegate vc); +#endif /******************************** * X509 Store */ +#if WindowsCE + [DllImport(wolfssl_dll)] + private extern static IntPtr wolfSSL_X509_STORE_CTX_get_current_cert(IntPtr x509Ctx); + [DllImport(wolfssl_dll)] + private extern static int wolfSSL_X509_STORE_CTX_get_error(IntPtr sk); + [DllImport(wolfssl_dll)] + private extern static IntPtr wolfSSL_X509_STORE_GetCerts(IntPtr x509Ctx); + [DllImport(wolfssl_dll)] + private extern static int wolfSSL_sk_X509_num(IntPtr sk); + [DllImport(wolfssl_dll)] + private extern static void wolfSSL_sk_X509_free(IntPtr sk); + [DllImport(wolfssl_dll)] + private extern static IntPtr wolfSSL_sk_X509_pop(IntPtr sk); +#else [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] private extern static IntPtr wolfSSL_X509_STORE_CTX_get_current_cert(IntPtr x509Ctx); [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] @@ -429,6 +656,7 @@ namespace wolfSSL.CSharp { private extern static void wolfSSL_sk_X509_free(IntPtr sk); [DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)] private extern static IntPtr wolfSSL_sk_X509_pop(IntPtr sk); +#endif /******************************** @@ -463,11 +691,23 @@ namespace wolfSSL.CSharp { public static readonly int FAILURE = 0; public static readonly int WOLFSSL_SNI_HOST_NAME = 0; + public static readonly int WOLFSSL_LOAD_FLAG_NONE = 0x00000000; + public static readonly int WOLFSSL_LOAD_FLAG_IGNORE_ERR = 0x00000001; + public static readonly int WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY = 0x00000002; + public static readonly int WOLFSSL_LOAD_FLAG_PEM_CA_ONLY = 0x00000004; + public static readonly int WOLFSSL_LOAD_FLAG_IGNORE_BAD_PATH_ERR = 0x00000008; + public static readonly int WOLFSSL_LOAD_FLAG_IGNORE_ZEROFILE = 0x00000010; + public static readonly int WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS = WOLFSSL_LOAD_FLAG_NONE; + private static IntPtr unwrap_ctx(IntPtr ctx) { try { +#if WindowsCE + GCHandle gch = (GCHandle)ctx; +#else GCHandle gch = GCHandle.FromIntPtr(ctx); +#endif ctx_handle handles = (ctx_handle)gch.Target; return handles.get_ctx(); } catch (Exception e) @@ -479,7 +719,11 @@ namespace wolfSSL.CSharp { private static IntPtr unwrap_ssl(IntPtr ssl) { try { +#if WindowsCE + GCHandle gch = (GCHandle)ssl; +#else GCHandle gch = GCHandle.FromIntPtr(ssl); +#endif ssl_handle handles = (ssl_handle)gch.Target; return handles.get_ssl(); } catch (Exception e) @@ -495,11 +739,21 @@ namespace wolfSSL.CSharp { /// return the platform specific path to the certificate /// public static string setPath(string file) { - if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + PlatformID platform = Environment.OSVersion.Platform; + +#if !WindowsCE + if (platform == PlatformID.Unix || + platform == PlatformID.MacOSX) { Console.WriteLine("Linux - " + file); return @"../../certs/" + file; - } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + } + else +#endif + if (platform == PlatformID.Win32NT || + platform == PlatformID.Win32Windows || + platform == PlatformID.Win32S || + platform == PlatformID.WinCE) { Console.WriteLine("Windows - " + file); return @"../../../../certs/" + file; @@ -530,15 +784,22 @@ namespace wolfSSL.CSharp { try { - System.Runtime.InteropServices.GCHandle gch; - gch = GCHandle.FromIntPtr(ctx); +#if WindowsCE + GCHandle gch = (GCHandle)ctx; +#else + GCHandle gch = GCHandle.FromIntPtr(ctx); +#endif Socket con = (System.Net.Sockets.Socket)gch.Target; Byte[] msg = new Byte[sz]; amtRecv = con.Receive(msg, msg.Length, 0); if (amtRecv == 0) { /* No data received so check for a response to see if connection is still open */ +#if WindowsCE + if (con.Poll(WC_WAIT, SelectMode.SelectRead)) +#else if (con.Poll((con.ReceiveTimeout > 0) ? con.ReceiveTimeout : WC_WAIT, SelectMode.SelectRead)) +#endif { log(ERROR_LOG, "socket connection issue, suspected connection termination."); return wolfssl.CBIO_ERR_CONN_CLOSE; @@ -574,16 +835,22 @@ namespace wolfSSL.CSharp { try { - System.Runtime.InteropServices.GCHandle gch; - gch = GCHandle.FromIntPtr(ctx); - +#if WindowsCE + GCHandle gch = (GCHandle)ctx; +#else + GCHandle gch = GCHandle.FromIntPtr(ctx); +#endif Socket con = (System.Net.Sockets.Socket)gch.Target; Byte[] msg = new Byte[sz]; Marshal.Copy(buf, msg, 0, sz); if (con.Send(msg, 0, msg.Length, SocketFlags.None) == 0 && sz != 0) { /* no data sent and msg size is larger then 0, check for lost connection */ +#if WindowsCE + if (con.Poll(WC_WAIT, SelectMode.SelectWrite)) +#else if (con.Poll((con.SendTimeout > 0) ? con.SendTimeout : WC_WAIT, SelectMode.SelectWrite)) +#endif { log(ERROR_LOG, "socket connection issue, suspect connection termination"); return wolfssl.CBIO_ERR_CONN_CLOSE; @@ -617,9 +884,11 @@ namespace wolfSSL.CSharp { try { - System.Runtime.InteropServices.GCHandle gch; - gch = GCHandle.FromIntPtr(ctx); - +#if WindowsCE + GCHandle gch = (GCHandle)ctx; +#else + GCHandle gch = GCHandle.FromIntPtr(ctx); +#endif DTLS_con con = (DTLS_con)gch.Target; Byte[] msg = new Byte[sz]; @@ -653,8 +922,11 @@ namespace wolfSSL.CSharp { try { - System.Runtime.InteropServices.GCHandle gch; - gch = GCHandle.FromIntPtr(ctx); +#if WindowsCE + GCHandle gch = (GCHandle)ctx; +#else + GCHandle gch = GCHandle.FromIntPtr(ctx); +#endif DTLS_con con = (DTLS_con)gch.Target; Byte[] msg = con.udp.Receive(ref con.ep); @@ -706,7 +978,11 @@ namespace wolfSSL.CSharp { } /* keep memory pinned to be able to reference by address */ +#if WindowsCE + return (IntPtr)GCHandle.Alloc(io, GCHandleType.Pinned); +#else return GCHandle.ToIntPtr(GCHandle.Alloc(io, GCHandleType.Pinned)); +#endif } catch (Exception e) { @@ -790,7 +1066,8 @@ namespace wolfSSL.CSharp { int ret; byte[] msg; - buf.Clear(); /* Clear incoming buffer */ + /* Clear incoming buffer */ + buf.Length = 0; if (sslCtx == IntPtr.Zero) { @@ -954,7 +1231,11 @@ namespace wolfSSL.CSharp { try { IntPtr sslCtx; +#if WindowsCE + GCHandle gch = (GCHandle)ssl; +#else GCHandle gch = GCHandle.FromIntPtr(ssl); +#endif ssl_handle handles = (ssl_handle)gch.Target; sslCtx = handles.get_ssl(); @@ -1006,7 +1287,11 @@ namespace wolfSSL.CSharp { { try { +#if WindowsCE + GCHandle gch = (GCHandle)ctx; +#else GCHandle gch = GCHandle.FromIntPtr(ctx); +#endif ctx_handle handles = (ctx_handle)gch.Target; /* check if already stored handle needs freed */ @@ -1037,7 +1322,11 @@ namespace wolfSSL.CSharp { { try { +#if WindowsCE + GCHandle gch = (GCHandle)ctx; +#else GCHandle gch = GCHandle.FromIntPtr(ctx); +#endif ctx_handle handles = (ctx_handle)gch.Target; /* check if already stored handle needs freed */ @@ -1084,7 +1373,11 @@ namespace wolfSSL.CSharp { wolfSSL_CTX_SetIOSend(ctx, send); /* keep memory pinned */ +#if WindowsCE + return (IntPtr)GCHandle.Alloc(io, GCHandleType.Pinned); +#else return GCHandle.ToIntPtr(GCHandle.Alloc(io, GCHandleType.Pinned)); +#endif } catch (Exception e) { @@ -1119,7 +1412,11 @@ namespace wolfSSL.CSharp { wolfSSL_CTX_SetIOSend(ctx, send); /* keep memory pinned */ +#if WindowsCE + return (IntPtr)GCHandle.Alloc(io, GCHandleType.Pinned); +#else return GCHandle.ToIntPtr(GCHandle.Alloc(io, GCHandleType.Pinned)); +#endif } catch (Exception e) { @@ -1132,12 +1429,17 @@ namespace wolfSSL.CSharp { /// /// Free information used in CTX structure /// - /// structure to free + /* structure to free + */ public static void CTX_free(IntPtr ctx) { try { +#if WindowsCE + GCHandle gch = (GCHandle)ctx; +#else GCHandle gch = GCHandle.FromIntPtr(ctx); +#endif ctx_handle handles = (ctx_handle)gch.Target; wolfSSL_CTX_free(handles.get_ctx()); handles.free(); @@ -1149,10 +1451,14 @@ namespace wolfSSL.CSharp { } } - public static void CTX_set_servername_callback(IntPtr ctx, sni_delegate sni_cb) + public static void CTX_set_servername_callback(IntPtr ctx, sni_delegate sni_cb) { try { +#if WindowsCE + GCHandle gch = (GCHandle)ctx; +#else GCHandle gch = GCHandle.FromIntPtr(ctx); +#endif ctx_handle handles = (ctx_handle)gch.Target; handles.set_sni(GCHandle.Alloc(sni_cb)); @@ -1163,10 +1469,14 @@ namespace wolfSSL.CSharp { } } - public static int CTX_set_servername_arg(IntPtr ctx, IntPtr arg) + public static int CTX_set_servername_arg(IntPtr ctx, IntPtr arg) { try { +#if WindowsCE + GCHandle gch = (GCHandle)ctx; +#else GCHandle gch = GCHandle.FromIntPtr(ctx); +#endif ctx_handle handles = (ctx_handle)gch.Target; handles.set_arg(GCHandle.Alloc(arg)); @@ -1178,10 +1488,14 @@ namespace wolfSSL.CSharp { } } - public static int CTX_UseSNI(IntPtr ctx, byte type, IntPtr data, ushort size) + public static int CTX_UseSNI(IntPtr ctx, byte type, IntPtr data, ushort size) { try { +#if WindowsCE + GCHandle gch = (GCHandle)ctx; +#else GCHandle gch = GCHandle.FromIntPtr(ctx); +#endif ctx_handle handles = (ctx_handle)gch.Target; return wolfSSL_CTX_UseSNI(handles.get_ctx(), type, data, size); @@ -1191,10 +1505,14 @@ namespace wolfSSL.CSharp { } } - public static int UseSNI(IntPtr ssl, byte type, IntPtr data, ushort size) + public static int UseSNI(IntPtr ssl, byte type, IntPtr data, ushort size) { try { +#if WindowsCE + GCHandle gch = (GCHandle)ssl; +#else GCHandle gch = GCHandle.FromIntPtr(ssl); +#endif ssl_handle handles = (ssl_handle)gch.Target; return wolfSSL_UseSNI(handles.get_ssl(), type, data, size); @@ -1204,10 +1522,14 @@ namespace wolfSSL.CSharp { } } - public static ushort SNI_GetRequest(IntPtr ssl, byte type, ref IntPtr data) + public static ushort SNI_GetRequest(IntPtr ssl, byte type, ref IntPtr data) { try { +#if WindowsCE + GCHandle gch = (GCHandle)ssl; +#else GCHandle gch = GCHandle.FromIntPtr(ssl); +#endif ssl_handle handles = (ssl_handle)gch.Target; return wolfSSL_SNI_GetRequest(handles.get_ssl(), type, ref data); @@ -1217,7 +1539,7 @@ namespace wolfSSL.CSharp { } } - public static int SNI_GetFromBuffer(byte []clientHello, uint helloSz, byte type, IntPtr sni, IntPtr inOutSz) + public static int SNI_GetFromBuffer(byte []clientHello, uint helloSz, byte type, IntPtr sni, IntPtr inOutSz) { try { return wolfSSL_SNI_GetFromBuffer(clientHello, helloSz, type, sni, inOutSz); @@ -1233,7 +1555,11 @@ namespace wolfSSL.CSharp { /// pointer to structure of ctx to set hint in /// hint to use /// 1 on success +#if WindowsCE + public static int CTX_use_psk_identity_hint(IntPtr ctx, string hint) +#else public static int CTX_use_psk_identity_hint(IntPtr ctx, StringBuilder hint) +#endif { try { @@ -1244,7 +1570,11 @@ namespace wolfSSL.CSharp { return FAILURE; } + #if WindowsCE + return wolfSSL_CTX_use_psk_identity_hint(local_ctx, wolfssl.AsciiToUnicode(hint)); + #else return wolfSSL_CTX_use_psk_identity_hint(local_ctx, hint); + #endif } catch (Exception e) { @@ -1263,7 +1593,11 @@ namespace wolfSSL.CSharp { { try { +#if WindowsCE + GCHandle gch = (GCHandle)ctx; +#else GCHandle gch = GCHandle.FromIntPtr(ctx); +#endif ctx_handle handles = (ctx_handle)gch.Target; handles.set_psk(GCHandle.Alloc(psk_cb)); @@ -1285,7 +1619,11 @@ namespace wolfSSL.CSharp { { try { +#if WindowsCE + GCHandle gch = (GCHandle)ctx; +#else GCHandle gch = GCHandle.FromIntPtr(ctx); +#endif ctx_handle handles = (ctx_handle)gch.Target; handles.set_psk(GCHandle.Alloc(psk_cb)); @@ -1307,7 +1645,11 @@ namespace wolfSSL.CSharp { { try { +#if WindowsCE + GCHandle gch = (GCHandle)ssl; +#else GCHandle gch = GCHandle.FromIntPtr(ssl); +#endif ssl_handle handles = (ssl_handle)gch.Target; handles.set_psk(GCHandle.Alloc(psk_cb)); @@ -1338,11 +1680,16 @@ namespace wolfSSL.CSharp { { if (!fd.Equals(null)) { +#if WindowsCE + GCHandle gch = (GCHandle)ssl; +#else GCHandle gch = GCHandle.FromIntPtr(ssl); +#endif ssl_handle handles = (ssl_handle)gch.Target; + IntPtr sslCtx = handles.get_ssl(); IntPtr ptr; - GCHandle fd_pin = GCHandle.Alloc(fd); + GCHandle fd_pin = GCHandle.Alloc(fd); // NOTE: Pinned only needed for buffers, not simple refs if (sslCtx == IntPtr.Zero) { @@ -1351,7 +1698,12 @@ namespace wolfSSL.CSharp { } handles.set_fd(fd_pin); +#if WindowsCE + ptr = (IntPtr)fd_pin; +#else ptr = GCHandle.ToIntPtr(fd_pin); +#endif + wolfSSL_SetIOWriteCtx(sslCtx, ptr); //pass along the socket for writing to wolfSSL_SetIOReadCtx(sslCtx, ptr); //pass along the socket for reading from @@ -1388,7 +1740,11 @@ namespace wolfSSL.CSharp { ptr = wolfSSL_GetIOReadCtx(sslCtx); if (ptr != IntPtr.Zero) { +#if WindowsCE + GCHandle gch = (GCHandle)ptr; +#else GCHandle gch = GCHandle.FromIntPtr(ptr); +#endif return (System.Net.Sockets.Socket)gch.Target; } return null; @@ -1423,7 +1779,11 @@ namespace wolfSSL.CSharp { { IntPtr ptr; DTLS_con con; +#if WindowsCE + GCHandle gch = (GCHandle)ssl; +#else GCHandle gch = GCHandle.FromIntPtr(ssl); +#endif ssl_handle handles = (ssl_handle)gch.Target; GCHandle fd_pin; @@ -1432,7 +1792,11 @@ namespace wolfSSL.CSharp { con.ep = ep; fd_pin = GCHandle.Alloc(con); handles.set_fd(fd_pin); +#if WindowsCE + ptr = (IntPtr)fd_pin; +#else ptr = GCHandle.ToIntPtr(fd_pin); +#endif wolfSSL_SetIOWriteCtx(handles.get_ssl(), ptr); //pass along the socket for writing to wolfSSL_SetIOReadCtx(handles.get_ssl(), ptr); //pass along the socket for reading from @@ -1468,7 +1832,11 @@ namespace wolfSSL.CSharp { ptr = wolfSSL_GetIOReadCtx(sslCtx); if (ptr != IntPtr.Zero) { +#if WindowsCE + GCHandle gch = (GCHandle)ptr; +#else GCHandle gch = GCHandle.FromIntPtr(ptr); +#endif return (DTLS_con)gch.Target; } return null; @@ -1480,6 +1848,31 @@ namespace wolfSSL.CSharp { } } + public static string get_ciphers() + { + try + { + #if WindowsCE + string ciphers = new string(' ', 4096); + #else + StringBuilder ciphers = new StringBuilder(new String(' ', 4096)); + #endif + int ret = wolfSSL_get_ciphers(ciphers, ciphers.Length); + if (ret != SUCCESS) + return null; + + #if WindowsCE + return wolfssl.UnicodeToAscii(ciphers); + #else + return ciphers.ToString(); + #endif + } + catch (Exception e) + { + log(ERROR_LOG, "wolfssl get_ciphers error " + e.ToString()); + return null; + } + } /// /// Get available cipher suites @@ -1487,7 +1880,11 @@ namespace wolfSSL.CSharp { /// list to fill with cipher suite names /// size of list available to fill /// 1 on success +#if WindowsCE + public static int get_ciphers(string list, int sz) +#else public static int get_ciphers(StringBuilder list, int sz) +#endif { try { @@ -1702,7 +2099,7 @@ namespace wolfSSL.CSharp { ssl_cipher = wolfSSL_get_current_cipher(sslCtx); ssl_cipher_ptr = wolfSSL_CIPHER_get_name(ssl_cipher); - ssl_cipher_str = Marshal.PtrToStringAnsi(ssl_cipher_ptr); + ssl_cipher_str = wolfssl.PtrToStringAnsi(ssl_cipher_ptr); return ssl_cipher_str; } @@ -1720,7 +2117,11 @@ namespace wolfSSL.CSharp { /// CTX structure to set /// List full of ciphers suites /// 1 on success +#if WindowsCE + public static int CTX_set_cipher_list(IntPtr ctx, string list) +#else public static int CTX_set_cipher_list(IntPtr ctx, StringBuilder list) +#endif { try { @@ -1731,7 +2132,11 @@ namespace wolfSSL.CSharp { return FAILURE; } + #if WindowsCE + return wolfSSL_CTX_set_cipher_list(local_ctx, wolfssl.AsciiToUnicode(list)); + #else return wolfSSL_CTX_set_cipher_list(local_ctx, list); + #endif } catch (Exception e) { @@ -1747,7 +2152,11 @@ namespace wolfSSL.CSharp { /// Structure to set cipher suite in /// List of cipher suites /// 1 on success +#if WindowsCE + public static int set_cipher_list(IntPtr ssl, string list) +#else public static int set_cipher_list(IntPtr ssl, StringBuilder list) +#endif { try { @@ -1758,7 +2167,11 @@ namespace wolfSSL.CSharp { return FAILURE; } + #if WindowsCE + return wolfSSL_set_cipher_list(sslCtx, wolfssl.AsciiToUnicode(list)); + #else return wolfSSL_set_cipher_list(sslCtx, list); + #endif } catch (Exception e) { @@ -1791,8 +2204,7 @@ namespace wolfSSL.CSharp { } version_ptr = wolfSSL_get_version(sslCtx); - version = Marshal.PtrToStringAnsi(version_ptr); - + version = wolfssl.PtrToStringAnsi(version_ptr); return version; } catch (Exception e) @@ -1861,7 +2273,11 @@ namespace wolfSSL.CSharp { return FAILURE; } + #if WindowsCE + return wolfSSL_CTX_use_certificate_file(local_ctx, wolfssl.AsciiToUnicode(fileCert), type); + #else return wolfSSL_CTX_use_certificate_file(local_ctx, fileCert, type); + #endif } catch (Exception e) { @@ -1889,7 +2305,11 @@ namespace wolfSSL.CSharp { return FAILURE; } + #if WindowsCE + return wolfSSL_CTX_load_verify_locations(local_ctx, wolfssl.AsciiToUnicode(fileCert), wolfssl.AsciiToUnicode(path)); + #else return wolfSSL_CTX_load_verify_locations(local_ctx, fileCert, path); + #endif } catch (Exception e) { @@ -1916,7 +2336,11 @@ namespace wolfSSL.CSharp { return FAILURE; } + #if WindowsCE + return wolfSSL_CTX_use_PrivateKey_file(local_ctx, wolfssl.AsciiToUnicode(fileKey), type); + #else return wolfSSL_CTX_use_PrivateKey_file(local_ctx, fileKey, type); + #endif } catch (Exception e) { @@ -1933,7 +2357,11 @@ namespace wolfSSL.CSharp { /// file name /// type of file ie PEM /// 1 on success +#if WindowsCE + public static int SetTmpDH_file(IntPtr ssl, string dhparam, int file_type) +#else public static int SetTmpDH_file(IntPtr ssl, StringBuilder dhparam, int file_type) +#endif { try { @@ -1944,7 +2372,11 @@ namespace wolfSSL.CSharp { return FAILURE; } + #if WindowsCE + return wolfSSL_SetTmpDH_file(sslCtx, wolfssl.AsciiToUnicode(dhparam), file_type); + #else return wolfSSL_SetTmpDH_file(sslCtx, dhparam, file_type); + #endif } catch (Exception e) { @@ -1960,7 +2392,11 @@ namespace wolfSSL.CSharp { /// file name /// type of file ie PEM /// 1 on success +#if WindowsCE + public static int CTX_SetTmpDH_file(IntPtr ctx, string dhparam, int file_type) +#else public static int CTX_SetTmpDH_file(IntPtr ctx, StringBuilder dhparam, int file_type) +#endif { try { @@ -1971,7 +2407,11 @@ namespace wolfSSL.CSharp { return FAILURE; } + #if WindowsCE + return wolfSSL_CTX_SetTmpDH_file(local_ctx, wolfssl.AsciiToUnicode(dhparam), file_type); + #else return wolfSSL_CTX_SetTmpDH_file(local_ctx, dhparam, file_type); + #endif } catch (Exception e) { @@ -2028,7 +2468,11 @@ namespace wolfSSL.CSharp { /* pin the verify callback to protect from garbage collection */ if (!vc.Equals(null)) { +#if WindowsCE + gch = (GCHandle)ctx; +#else gch = GCHandle.FromIntPtr(ctx); +#endif handles = (ctx_handle)gch.Target; handles.set_vrf(GCHandle.Alloc(vc)); } @@ -2064,7 +2508,11 @@ namespace wolfSSL.CSharp { /* pin the verify callback to protect from garbage collection */ if (!vc.Equals(null)) { +#if WindowsCE + gch = (GCHandle)ssl; +#else gch = GCHandle.FromIntPtr(ssl); +#endif handles = (ssl_handle)gch.Target; handles.set_vrf(GCHandle.Alloc(vc)); } @@ -2196,6 +2644,9 @@ namespace wolfSSL.CSharp { public static int SetLogging(loggingCb input) { internal_log = input; + + wolfSSL_SetLoggingCb(input); + return SUCCESS; } @@ -2210,8 +2661,12 @@ namespace wolfSSL.CSharp { /* if log is not set then print nothing */ if (internal_log == null) return; - StringBuilder ptr = new StringBuilder(msg); - internal_log(lvl, ptr); + #if WindowsCE + internal_log(lvl, msg); + #else + StringBuilder msg_sb = new StringBuilder(msg); + internal_log(lvl, msg_sb); + #endif } } }