Skip to content

Commit eb6bcd3

Browse files
committed
Improve CSharp Examples
1 parent 2125cbd commit eb6bcd3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1857
-775
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
name: Build for .NET 3.5
2+
3+
on:
4+
push:
5+
branches:
6+
- master # Trigger only on commits to master
7+
pull_request:
8+
branches:
9+
- master
10+
workflow_dispatch:
11+
12+
jobs:
13+
build:
14+
if: github.repository_owner == 'wolfssl'
15+
runs-on: windows-2019 # .NET 3.5 is available here
16+
17+
steps:
18+
- name: Checkout Repository (master branch)
19+
uses: actions/checkout@v3
20+
with:
21+
ref: master # Ensures correct branch is checked out
22+
23+
- name: Checkout wolfSSL (master branch)
24+
uses: actions/checkout@v3
25+
with:
26+
repository: wolfssl/wolfssl
27+
ref: master
28+
path: wolfssl # Clone wolfSSL into the "wolfssl" subdirectory
29+
30+
- name: Setup MSBuild
31+
uses: microsoft/setup-msbuild@v1
32+
33+
# wolfssl core
34+
- name: Build wolfSSL (DLL Debug, x64)
35+
working-directory: wolfssl
36+
run: |
37+
msbuild wolfssl.vcxproj /p:Configuration="DLL Debug" /p:Platform=x64 /p:PlatformToolset=v142 /m /verbosity:normal
38+
shell: cmd
39+
40+
# C# Wrapper
41+
- name: Build wolfSSL_CSharp
42+
working-directory: wrapper/CSharp/wolfSSL_CSharp
43+
run: msbuild wolfSSL_CSharp.csproj /p:TargetFrameworkVersion=v3.5
44+
45+
# wolfCrypt-Test
46+
- name: Build wolfCrypt-Test
47+
working-directory: wrapper/CSharp/wolfCrypt-Test
48+
run: msbuild wolfCrypt-Test.csproj /p:TargetFrameworkVersion=v3.5
49+
50+
# DTLS
51+
# TODO wolfSSL-DTLS-Client
52+
- name: Build wolfSSL-DTLS-Server
53+
working-directory: wrapper/CSharp/wolfSSL-DTLS-Server
54+
run: msbuild wolfSSL-DTLS-Server.csproj /p:TargetFrameworkVersion=v3.5
55+
56+
# DTLS-PSK
57+
# TODO wolfSSL-DTLS-PSK-Client
58+
- name: Build wolfSSL-DTLS-PSK-Server
59+
working-directory: wrapper/CSharp/wolfSSL-DTLS-PSK-Server
60+
run: msbuild wolfSSL-DTLS-PSK-Server.csproj /p:TargetFrameworkVersion=v3.5
61+
62+
# TLS
63+
- name: Build wolfSSL-TLS-Client
64+
working-directory: wrapper/CSharp/wolfSSL-TLS-Client
65+
run: msbuild wolfSSL-TLS-Client.csproj /p:TargetFrameworkVersion=v3.5
66+
67+
- name: Build wolfSSL-TLS-Server
68+
working-directory: wrapper/CSharp/wolfSSL-TLS-Server
69+
run: msbuild wolfSSL-TLS-Server.csproj /p:TargetFrameworkVersion=v3.5
70+
71+
- name: Build wolfSSL-TLS-ServerThreaded
72+
working-directory: wrapper/CSharp/wolfSSL-TLS-ServerThreaded
73+
run: msbuild wolfSSL-TLS-ServerThreaded.csproj /p:TargetFrameworkVersion=v3.5
74+
75+
# TLS PSK
76+
- name: Build wolfSSL-TLS-PSK-Client
77+
working-directory: wrapper/CSharp/wolfSSL-TLS-PSK-Client
78+
run: msbuild wolfSSL-TLS-PSK-Client.csproj /p:TargetFrameworkVersion=v3.5
79+
80+
- name: Build wolfSSL-TLS-PSK-Server
81+
working-directory: wrapper/CSharp/wolfSSL-TLS-PSK-Server
82+
run: msbuild wolfSSL-TLS-PSK-Server.csproj /p:TargetFrameworkVersion=v3.5
83+
84+
# Example-IOCallbacks
85+
- name: Build wolfSSL-Example-IOCallbacks
86+
working-directory: wrapper/CSharp/wolfSSL-Example-IOCallbacks
87+
run: msbuild wolfSSL-Example-IOCallbacks.csproj /p:TargetFrameworkVersion=v3.5

wolfssl/sniffer.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,11 @@
3131
#include <wolfssl/wolfcrypt/wolfevent.h>
3232
#endif
3333

34-
3534
#ifdef _WIN32
3635
#ifdef SSL_SNIFFER_EXPORTS
3736
#define SSL_SNIFFER_API __declspec(dllexport)
3837
#else
39-
#define SSL_SNIFFER_API __declspec(dllimport)
38+
#define SSL_SNIFFER_API
4039
#endif
4140
#else
4241
#define SSL_SNIFFER_API

wolfssl/wolfcrypt/curve25519.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,6 @@ curve25519_key* wc_curve25519_new(void* heap, int devId, int *result_code);
165165
WOLFSSL_API
166166
int wc_curve25519_delete(curve25519_key* key, curve25519_key** key_p);
167167
#endif
168-
WOLFSSL_API
169168

170169
/* raw key helpers */
171170
WOLFSSL_API

wolfssl/wolfcrypt/ed25519.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,6 @@ ed25519_key* wc_ed25519_new(void* heap, int devId, int *result_code);
187187
WOLFSSL_API
188188
int wc_ed25519_delete(ed25519_key* key, ed25519_key** key_p);
189189
#endif
190-
WOLFSSL_API
191190

192191
#ifdef HAVE_ED25519_KEY_IMPORT
193192
WOLFSSL_API

wrapper/CSharp/README.md

Lines changed: 181 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,191 @@ need to open a new solution `wolfSSL_CSharp.sln` located in `wrapper\CSharp\wolf
2828
Select the CPU type, configuration, and target file.
2929
select `Build` and either `Rebuild Solution` or `Build Solution`.
3030

31+
### Debugging Native Code
32+
33+
Right-click on the `wolfSSL_CSharp` project, select `Properties` and
34+
navigate to the `Debug` panel.
35+
36+
Be sure to check the box under `Debugger engines`: [x] `Enable native code debugging`.
37+
38+
This will allow single-step debugging into the native wolfSSL C library.
39+
40+
Do this also for the startup project being debugged.
41+
42+
If the error `Interop debugging is not supported` is encountered,
43+
check which version of the .NET framework is being used.
44+
Only 4.x or later supports native code debugging from a C# app.
45+
46+
See also:
47+
48+
https://learn.microsoft.com/en-us/visualstudio/debugger/debugging-native-code
49+
50+
### Calling Convention
51+
52+
The wolfSSL functions are wrapped like this:
53+
54+
```
55+
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
56+
private extern static int wolfSSL_Init();
57+
```
58+
59+
Where `wolfssl_dll` is a constant compile-time string that points to the `wolfssl.dll` file.
60+
61+
### Troubleshooting Windows DLL
62+
63+
The `wolfssl.dll` file is typically compiled to:
64+
65+
```
66+
C:\workspace\wolfssl-%USERNAME$\wrapper\CSharp\Debug\x64\wolfssl.dll
67+
```
68+
69+
From a developer command prompt:
70+
71+
```
72+
dumpbin /EXPORTS C:\workspace\wolfssl-$USER\wrapper\CSharp\Debug\x64\wolfssl.dll
73+
```
74+
75+
There should be a long list of functions. If not, be sure to build with `WOLFSSL_DLL`.
76+
77+
See the project file `PreprocessorDefinitions` section:
78+
79+
```
80+
<PreprocessorDefinitions>BUILDING_WOLFSSL;WOLFSSL_DLL;WOLFSSL_USER_SETTINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
81+
```
82+
83+
If wolfSSL was _not_ compiled with `WOLFSSL_DLL` the `Unable to find an entry point` will be encountered:
84+
85+
```
86+
wolfssl init error System.EntryPointNotFoundException: Unable to find an entry point named 'wolfSSL_Init' in DLL 'wolfssl.dll'.
87+
at wolfSSL.CSharp.wolfssl.wolfSSL_Init()
88+
at wolfSSL.CSharp.wolfssl.Init() in C:\workspace\wolfssl-%USERNAME%\wrapper\CSharp\wolfSSL_CSharp\wolfSSL.cs:line nnn
89+
Calling ctx Init from wolfSSL
90+
```
91+
92+
Note the `WOLFSSL_DLL` is used in the wolfssl `visibiility.h` and defines the `WOLFSSL_API` like this:
93+
94+
```
95+
#define WOLFSSL_API __declspec(dllexport)
96+
```
97+
98+
Only the wolfSSL function declarations decorated with this tag will be visible ion the DLL.
99+
100+
#### Finding the wolfssl.dll
101+
102+
The most common problem encountered on Windows is the DLL location.
103+
104+
If not developing wolfSSL for Windows, one option is to copy the `wolfssl.dll` to `C:\Windows\System32\`.
105+
106+
Another option is to add to the system environment path or your user environment path:
107+
108+
```
109+
set PATH=%PATH%;C:\path\to\your\wolfssl.dll
110+
```
111+
112+
#### Check Architecture (x86 vs x64)
113+
114+
Your C# application must match the architecture of wolfssl.dll:
115+
116+
- If your C# app is x64, wolfssl.dll must be 64-bit.
117+
- If your C# app is x86, wolfssl.dll must be 32-bit.
118+
119+
#### Ensure wolfssl.dll is Unblocked
120+
121+
If you downloaded wolfssl.dll from the Internet, Windows may block it.
122+
123+
Right-click wolfssl.dll, go to Properties, and check Unblock under Security.
124+
125+
126+
## Linux (Ubuntu) using WSL
127+
128+
The Microsoft Windows Subsystem for Linux cam be used for wolfSSL.
129+
130+
```bash
131+
sudo
132+
sudo apt-get update
133+
sudo apt-get upgrade
134+
sudo apt install -y build-essential autoconf automake libtool pkg-config
135+
136+
export WORKSPACE="/mnt/c/workspace"
137+
export WOLFSSL_ROOT="$WORKSPACE/wolfssl-$USER"
138+
139+
cd "WOLFSSL_ROOT"
140+
```
141+
142+
When using a git repository, run `autogen.sh`:
143+
144+
```
145+
cd "$WOLFSSL_ROOT"
146+
./autogen.sh
147+
```
148+
149+
### Build wolfSSL and install it system-wide
150+
151+
To have a single instance of wolfSSL:
152+
153+
```
154+
./configure
155+
make
156+
make check # (optional, but highly recommended)
157+
sudo make install
158+
```
159+
160+
### Build wolfSSL and install to arbitrary directory
161+
162+
To have a n isolated instance of wolfSSL, in this case `$HOME/wolfssl-install-psk`:
163+
164+
```bash
165+
make clean
166+
make distclean
167+
168+
rm -rf "$HOME/wolfssl-install-psk"
169+
170+
./configure --enable-all --disable-crypttests \
171+
--disable-examples \
172+
--enable-opensslall --enable-opensslextra \
173+
--enable-tls13 --enable-dtls13 --enable-dtls --enable-psk \
174+
CFLAGS="-DWOLFSSL_STATIC_PSK" --enable-shared \
175+
--prefix="$HOME/wolfssl-install-psk"
176+
177+
make -j$(nproc)
178+
make install
179+
```
180+
181+
182+
183+
### Compile specific example in WSL
184+
185+
```bash
186+
# Optionally fetch additional examples
187+
git clone https://github.com/wolfSSL/wolfssl-examples.git "$WORKSPACE/wolfssl-examples-$USER"
188+
cd "$WORKSPACE/wolfssl-examples-$USER"
189+
190+
THIS_EXAMPLE="client-dtls-psk"
191+
192+
export WOLFSSL_DIR="$HOME/wolfssl-install-psk"
193+
export CFLAGS="-I$WOLFSSL_DIR/include"
194+
export LDFLAGS="-L$WOLFSSL_DIR/lib"
195+
196+
export LD_LIBRARY_PATH="$HOME/wolfssl-install-psk/lib:$LD_LIBRARY_PATH"
197+
198+
gcc -o "$THIS_EXAMPLE" "$THIS_EXAMPLE".c \
199+
-I$HOME/wolfssl-install-psk/include \
200+
-L$HOME/wolfssl-install-psk/lib -Wl,-rpath=$HOME/wolfssl-install-psk/lib -lwolfssl -lm
201+
```
202+
31203
## Linux (Ubuntu) using mono
32204

33205
Prerequisites for linux:
34206

35-
```
207+
```bash
36208
apt-get update
37209
apt-get upgrade
38210
apt-get install mono-complete
39211
```
40212

41-
### Build wolfSSL and install
213+
### Build wolfSSL and install system-wide
42214

43-
```
215+
```bash
44216
./autogen.sh
45217
./configure --enable-keygen --enable-eccencrypt --enable-ed25519 --enable-curve25519 --enable-aesgcm
46218
make
@@ -52,9 +224,9 @@ sudo make install
52224

53225
From the `wrapper/CSharp` directory (`cd wrapper/CSharp`):
54226

55-
Compile wolfCrypt test:
227+
Compile wolfCrypt test with mono:
56228

57-
```
229+
```bash
58230
mcs wolfCrypt-Test/wolfCrypt-Test.cs wolfSSL_CSharp/wolfCrypt.cs -OUT:wolfcrypttest.exe
59231
mono wolfcrypttest.exe
60232
```
@@ -63,15 +235,15 @@ mono wolfcrypttest.exe
63235

64236
From the `wrapper/CSharp` directory (`cd wrapper/CSharp`):
65237

66-
Compile server:
238+
Compile server with mono:
67239

68-
```
240+
```bash
69241
mcs wolfSSL_CSharp/wolfSSL.cs wolfSSL_CSharp/X509.cs wolfSSL-TLS-Server/wolfSSL-TLS-Server.cs -OUT:server.exe
70242
```
71243

72-
Compile client:
244+
Compile client with mono:
73245

74-
```
246+
```bash
75247
mcs wolfSSL_CSharp/wolfSSL.cs wolfSSL_CSharp/X509.cs wolfSSL-TLS-Client/wolfSSL-TLS-Client.cs -OUT:client.exe
76248
```
77249

wrapper/CSharp/cpp.hint

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Hint files help the Visual Studio IDE interpret Visual C++ identifiers
2+
// such as names of functions and macros.
3+
// For more information see https://go.microsoft.com/fwlink/?linkid=865984
4+
#define WOLFSSL_API
5+
#define WOLFSSL_API __declspec(dllexport)
6+
#define WOLFSSL_API __attribute__ ((visibility("default")))
7+

wrapper/CSharp/include.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# wolfSSL CSharp wrapper files
2+
EXTRA_DIST+= wrapper/CSharp/cpp.hint
23
EXTRA_DIST+= wrapper/CSharp/README.md
34
EXTRA_DIST+= wrapper/CSharp/wolfSSL-DTLS-PSK-Server/App.config
45
EXTRA_DIST+= wrapper/CSharp/wolfSSL-DTLS-PSK-Server/Properties/AssemblyInfo.cs
@@ -47,3 +48,4 @@ EXTRA_DIST+= wrapper/CSharp/wolfCrypt-Test/App.config
4748
EXTRA_DIST+= wrapper/CSharp/wolfCrypt-Test/Properties/AssemblyInfo.cs
4849
EXTRA_DIST+= wrapper/CSharp/wolfCrypt-Test/wolfCrypt-Test.cs
4950
EXTRA_DIST+= wrapper/CSharp/wolfCrypt-Test/wolfCrypt-Test.csproj
51+
EXTRA_DIST+= wrapper/CSharp/wolfssl_clients/wolfSSL_CSharp-Clients.sln

wrapper/CSharp/user_settings.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@
126126
#endif
127127
#endif
128128

129-
/* Debug logging */
130-
#if 1
129+
/* Debug logging; DLL_DEBUG is a preprocessor for the "DLL Debug" build config */
130+
#if defined(DLL_DEBUG) || defined(Debug) || defined(DEBUG)
131131
#define DEBUG_WOLFSSL
132132
#else
133133
/* #define NO_ERROR_STRINGS */
Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
<?xml version="1.0" encoding="utf-8"?>
2-
<configuration>
3-
<startup>
4-
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8"/>
5-
</startup>
6-
</configuration>
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v2.0.50727"/>
5+
</startup>
6+
<appSettings>
7+
<add key="WOLFSSL_DLL_PATH" value="C:/Windows/System32"/>
8+
<add key="WOLFSSL_CERTS_PATH" value="C:/workspace/wolfssl/certs"/>
9+
</appSettings>
10+
</configuration>

0 commit comments

Comments
 (0)