wolfssl icon indicating copy to clipboard operation
wolfssl copied to clipboard

CSharp Wrapper Improvements

Open gojimmypi opened this issue 6 months ago • 10 comments

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_ALERT and WOLFSSL_ALERT_HISTORY structs 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 native wolfSSL context.
  • Improved Path Detection

    • Dynamically searches for the certs folder in parent directories to better support different working directories across Windows and Linux.
  • Sample Application Improvements

    • Enhanced logging and diagnostics in:
      • wolfSSL-TLS-Client
      • wolfSSL-TLS-PSK-Client
      • wolfSSL-TLS-Server
    • Added constant definitions for SERVER_NAME, SERVER_PORT, and cipher suite selection.
    • Introduced a new Visual Studio solution: wolfSSL_CSharp-Clients.sln to support concurrent client/server projects.
  • Documentation Updates

    • Updated README.md with:
      • Troubleshooting for pwsh.exe not recognized
      • Architecture mismatch errors (AnyCPU vs. x64)
      • Best practices for setting startup projects and debugging

Compatibility Improvements

  • Replaced #warning with #pragma message when compiling with MSVC (_MSC_VER) to prevent build interruptions.
  • Added Visual Studio 2015+ specific suppression for enum-type mismatch warnings (C5287) around wc_static_assert.
  • Updated the XINET_PTON macro to cast to PCWSTR or PCSTR based on Visual Studio version for better Windows API compliance.

Dynamic DLL Loading

  • Added wolfssl.LoadDLL() to locate and load wolfssl.dll at 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 a certs folder.
  • 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 #warning with #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

gojimmypi avatar Jul 01 '25 04:07 gojimmypi

Jenkins retest this please.

For missing results, SSL_read input error -308, error state on socket

gojimmypi avatar Jul 01 '25 13:07 gojimmypi

🛟 Devin Lifeguard found 1 likely issues in this PR

  • check-all-return-codes snippet 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) {

gojimmypi avatar Jul 02 '25 20:07 gojimmypi

@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.

gojimmypi avatar Jul 02 '25 23:07 gojimmypi

🛟 Devin Lifeguard found 1 likely issues in this PR

  • do-not-change-external-apis snippet: Re-introduce the original delegate (loggingCb with StringBuilder msg) and add a new one (e.g., loggingCbEx) that uses IntPtr; 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.

gojimmypi avatar Jul 03 '25 04:07 gojimmypi

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

gojimmypi avatar Jul 03 '25 12:07 gojimmypi

🛟 Devin Lifeguard found 2 likely issues in this PR

  • do-not-change-external-apis snippet: Restore the original get_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-leaks snippet: After the call sequence that uses sniHostName, add Marshal.FreeHGlobal(sniHostName); (inside a finally block) to release the unmanaged memory allocated by wolfssl.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

gojimmypi avatar Jul 03 '25 21:07 gojimmypi

@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

gojimmypi avatar Jul 03 '25 23:07 gojimmypi