CSharp Wrapper Improvements
Description
Summary: CSharp Wrapper Improvements
This PR enhances the C# wrapper and improves cross-platform compatibility, developer experience, and debugging support when using wolfSSL with .NET applications.
CSharp Wrapper Enhancements
-
New Structs and Functions
- Added
WOLFSSL_ALERTandWOLFSSL_ALERT_HISTORYstructs to expose alert-level history to .NET apps. - Added new public method
get_alert_history()in C# to retrieve and display alert codes/levels from the nativewolfSSLcontext.
- Added
-
Improved Path Detection
- Dynamically searches for the
certsfolder in parent directories to better support different working directories across Windows and Linux.
- Dynamically searches for the
-
Sample Application Improvements
- Enhanced logging and diagnostics in:
wolfSSL-TLS-ClientwolfSSL-TLS-PSK-ClientwolfSSL-TLS-Server
- Added constant definitions for
SERVER_NAME,SERVER_PORT, and cipher suite selection. - Introduced a new Visual Studio solution:
wolfSSL_CSharp-Clients.slnto support concurrent client/server projects.
- Enhanced logging and diagnostics in:
-
Documentation Updates
- Updated
README.mdwith:- Troubleshooting for
pwsh.exenot recognized - Architecture mismatch errors (
AnyCPUvs.x64) - Best practices for setting startup projects and debugging
- Troubleshooting for
- Updated
Compatibility Improvements
- Replaced
#warningwith#pragma messagewhen compiling with MSVC (_MSC_VER) to prevent build interruptions. - Added Visual Studio 2015+ specific suppression for enum-type mismatch warnings (
C5287) aroundwc_static_assert. - Updated the
XINET_PTONmacro to cast toPCWSTRorPCSTRbased on Visual Studio version for better Windows API compliance.
Dynamic DLL Loading
- Added
wolfssl.LoadDLL()to locate and loadwolfssl.dllat runtime. - Searches common subdirectories (e.g.
Debug,Debug\\x64) when not found in the working directory. - Added
SetVerbosity(true)for diagnostic output about the load process, DLL size, and timestamp.
Improved Path Resolution
setPath()now recursively searches parent directories for acertsfolder.- Added platform-specific logging to help identify missing files or incorrect paths.
Verbose Debug Mode
- New global flag in the C# wrapper enables detailed output for:
- DLL discovery
- Certificate path detection
- Wrapper initialization behavior
Alert History & Diagnostics
- Sample clients now retrieve and print TLS alert history after handshake failures using
get_alert_history(). - Improves post-failure analysis for SSL/TLS issues.
Other Fixes
- Initialized unassigned local variables (
keyTypeTemp,keySizeTemp) to eliminate compiler warnings. - Minor cleanup of whitespace, formatting, and redundant logic across several files.
MSVC Compatibility Enhancements
- Replaced
#warningwith#pragma message(...)for MSVC (_MSC_VER) to suppress noisy or incompatible warnings during build:#ifndef WOLFSSL_IGNORE_FILE_WARN #if defined(_MSC_VER) #pragma message("file.c does not need to be compiled separately from ssl.c") #else #warning file.c does not need to be compiled separately from ssl.c #endif #endif
Fixes zd# n/a
Testing
How did you test?
Manually tested in VS2022, VS2008
Checklist
- [ ] added tests
- [ ] updated/added doxygen
- [ ] updated appropriate READMEs
- [ ] Updated manual and documentation
Jenkins retest this please.
For missing results, SSL_read input error -308, error state on socket
🛟 Devin Lifeguard found 1 likely issues in this PR
check-all-return-codessnippet snippet: Capture the bool returned by wolfssl.LoadDLL("") in the client and server files and abort or report an error when it is false, e.g.if (!wolfssl.LoadDLL("")) { Console.WriteLine("DLL load failed"); return; }.
@gojimmypi please take a look at the above issues which Devin flagged. Devin will not fix these issues automatically.
@dgarske @JacobBarthelmeh sorry for the delay, I ended up down a rabbit hole & should be done by the end of the day.
I've implemented a new StringToAnsiPtr and I'm converting callbacks from
public static void standard_log(int lvl, StringBuilder msg)
to
public static void standard_log(int lvl, IntPtr msg) {
@dgarske @JacobBarthelmeh This PR has changed substantially since submitted. I've left the new changes in a separate commit for now.
I've addressed the compile issues for CE, but I have been still unable to figure out how to run native C wolfSSL in any of the CE emulators with the wolfSSL_CSharp C# wrapper class.
@dgarske please test on customer hardware. Thank you.
🛟 Devin Lifeguard found 1 likely issues in this PR
do-not-change-external-apissnippet: Re-introduce the original delegate (loggingCbwithStringBuilder msg) and add a new one (e.g.,loggingCbEx) that usesIntPtr; keep the old overloads that accept the original delegate and internally marshal to the new implementation.
@gojimmypi please take a look at the above issues which Devin flagged. Devin will not fix these issues automatically.
I've addressed the do-not-change-external-apis comment in https://github.com/wolfSSL/wolfssl/pull/8946/commits/b987303631ccd6e985688ac407a2e6aadbd3d6e5:
Introduced loggingCbEx (taking parameters int level, IntPtr msg instead of int lvl, StringBuilder msg).
The IntPtr msg vs StringBuilder msg logging parameter changes should also be more robust and intuitive with regards to memory use and prevention of leaks.
Included is a consolidation of logging to use only the wolfssl class library. Duplicate code removed from wolfCrypt.cs.
Jenkins retest this please
For Unable to create live FilePath for wolf-linux-cloud-node-[n]; wolf-linux-cloud-node[n] was marked offline: Connection was broken
🛟 Devin Lifeguard found 2 likely issues in this PR
do-not-change-external-apissnippet: Restore the originalget_ciphers(StringBuilder …)overload and introduce a new overload (e.g.get_ciphers_ex(string …)), having the old method call the new one internally so existing user code keeps compiling.no-memory-leakssnippet: After the call sequence that usessniHostName, addMarshal.FreeHGlobal(sniHostName);(inside a finally block) to release the unmanaged memory allocated bywolfssl.StringToAnsiPtr.
@gojimmypi please take a look at the above issues which Devin flagged. Devin will not fix these issues automatically.
Jenkins retest this please
For ERROR: Unable to tear down: java.io.IOException: Unexpected EOF; Python Remote call to wolf-linux-cloud-node no workspace for PRB-python-port #8458
@dgarske you removed the For This Release tag today; If I have more time on this PR, would you like the other examples cleaned up and expanded (e.g. add new alert history)? I also have some new examples that are missing, such as wolfSSL-DTLS-PSK-Client.
We may also consider moving some of the C# examples to https://github.com/wolfSSL/wolfssl-examples