Skip to content

Commit 61bce5e

Browse files
committed
Improve CSharp Examples
1 parent 6d299ea commit 61bce5e

Some content is hidden

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

42 files changed

+1899
-788
lines changed

.github/workflows/visual-studio.yml

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
name: Build for .NET 3.5
2+
3+
# START OF COMMON SECTION
4+
on:
5+
push:
6+
branches: [ 'master', 'main', 'release/**' ]
7+
pull_request:
8+
branches: [ '*' ]
9+
workflow_dispatch:
10+
11+
concurrency:
12+
group: ${{ github.workflow }}-${{ github.ref }}
13+
cancel-in-progress: true
14+
# END OF COMMON SECTION
15+
16+
jobs:
17+
build:
18+
if: github.repository_owner == 'wolfssl'
19+
runs-on: windows-2019 # .NET 3.5 is available here
20+
21+
steps:
22+
# Checkout PR branch (supports both forks and local PRs)
23+
- name: Checkout Repository (PR branch or push branch)
24+
uses: actions/checkout@v3
25+
with:
26+
repository: ${{ github.event.pull_request.head.repo.full_name || github.repository }}
27+
ref: ${{ github.event.pull_request.head.ref || github.ref_name }}
28+
29+
# Checkout wolfSSL (PR branch if from fork, master if push)
30+
- name: Checkout wolfSSL (PR branch or master)
31+
uses: actions/checkout@v3
32+
with:
33+
repository: ${{ github.event.pull_request.head.repo.full_name || 'wolfssl/wolfssl' }} # Pull from fork if PR exists
34+
ref: ${{ github.event.pull_request.head.ref || 'master' }} # Use PR branch if available, otherwise master
35+
path: wolfssl # Clone wolfSSL into "wolfssl" subdirectory
36+
37+
- name: Setup MSBuild
38+
uses: microsoft/setup-msbuild@v1
39+
40+
# wolfSSL core
41+
- name: Build wolfSSL (DLL Debug, x64)
42+
working-directory: wolfssl
43+
run: |
44+
msbuild wolfssl.vcxproj /p:Configuration="DLL Debug" /p:Platform=x64 /p:PlatformToolset=v142 /m /verbosity:normal
45+
shell: cmd
46+
47+
# C# Wrapper
48+
- name: Build wolfSSL_CSharp
49+
working-directory: wrapper/CSharp/wolfSSL_CSharp
50+
run: msbuild wolfSSL_CSharp.csproj /p:TargetFrameworkVersion=v3.5
51+
52+
# wolfCrypt-Test
53+
- name: Build wolfCrypt-Test
54+
working-directory: wrapper/CSharp/wolfCrypt-Test
55+
run: msbuild wolfCrypt-Test.csproj /p:TargetFrameworkVersion=v3.5
56+
57+
# DTLS
58+
- name: Build wolfSSL-DTLS-Server
59+
working-directory: wrapper/CSharp/wolfSSL-DTLS-Server
60+
run: msbuild wolfSSL-DTLS-Server.csproj /p:TargetFrameworkVersion=v3.5
61+
62+
# DTLS-PSK
63+
- name: Build wolfSSL-DTLS-PSK-Server
64+
working-directory: wrapper/CSharp/wolfSSL-DTLS-PSK-Server
65+
run: msbuild wolfSSL-DTLS-PSK-Server.csproj /p:TargetFrameworkVersion=v3.5
66+
67+
# TLS
68+
- name: Build wolfSSL-TLS-Client
69+
working-directory: wrapper/CSharp/wolfSSL-TLS-Client
70+
run: msbuild wolfSSL-TLS-Client.csproj /p:TargetFrameworkVersion=v3.5
71+
72+
- name: Build wolfSSL-TLS-Server
73+
working-directory: wrapper/CSharp/wolfSSL-TLS-Server
74+
run: msbuild wolfSSL-TLS-Server.csproj /p:TargetFrameworkVersion=v3.5
75+
76+
- name: Build wolfSSL-TLS-ServerThreaded
77+
working-directory: wrapper/CSharp/wolfSSL-TLS-ServerThreaded
78+
run: msbuild wolfSSL-TLS-ServerThreaded.csproj /p:TargetFrameworkVersion=v3.5
79+
80+
# TLS PSK
81+
- name: Build wolfSSL-TLS-PSK-Client
82+
working-directory: wrapper/CSharp/wolfSSL-TLS-PSK-Client
83+
run: msbuild wolfSSL-TLS-PSK-Client.csproj /p:TargetFrameworkVersion=v3.5
84+
85+
- name: Build wolfSSL-TLS-PSK-Server
86+
working-directory: wrapper/CSharp/wolfSSL-TLS-PSK-Server
87+
run: msbuild wolfSSL-TLS-PSK-Server.csproj /p:TargetFrameworkVersion=v3.5
88+
89+
# Example-IOCallbacks
90+
- name: Build wolfSSL-Example-IOCallbacks
91+
working-directory: wrapper/CSharp/wolfSSL-Example-IOCallbacks
92+
run: msbuild wolfSSL-Example-IOCallbacks.csproj /p:TargetFrameworkVersion=v3.5

.wolfssl_known_macro_extras

+2
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ DILITHIUM_MUL_Q_SLOW
164164
DILITHIUM_MUL_SLOW
165165
DILITHIUM_USE_HINT_CT
166166
DTLS_RECEIVEFROM_NO_TIMEOUT_ON_INVALID_PEER
167+
Debug
167168
ECCSI_ORDER_MORE_BITS_THAN_PRIME
168169
ECC_DUMP_OID
169170
ECDHE_SIZE
@@ -418,6 +419,7 @@ RTC_ALARMSUBSECONDMASK_ALL
418419
RTE_CMSIS_RTOS_RTX
419420
RTOS_MODULE_NET_AVAIL
420421
RTPLATFORM
422+
Release
421423
SA_INTERRUPT
422424
SCEKEY_INSTALLED
423425
SHA256_MANY_REGISTERS

wolfssl/sniffer.h

+1-2
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

-1
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

-1
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

+209-9
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,219 @@ 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+
### Visual Studio Build Configurations
32+
33+
In addition to the Configuration and Platform build options, Visual Studio has a `Platform Toolset` option in the (C not C#) `wolfssl` project file.
34+
This can be found in the (Right-click on wolfssl project) `Property pages - General`.
35+
36+
A missing Platform Toolset is assumed to be Visual Studio 2010. Click the drop-down to see options available.
37+
38+
```text
39+
<PropertyGroup Condition ...
40+
<PlatformToolset>v143</PlatformToolset>
41+
```
42+
43+
| Visual Studio Version | Internal Version | Platform Toolset |
44+
|-----------------------|------------------|------------------|
45+
| Visual Studio 2010 | 10.0 | v100 |
46+
| Visual Studio 2012 | 11.0 | v110 |
47+
| Visual Studio 2013 | 12.0 | v120 |
48+
| Visual Studio 2015 | 14.0 | v140 |
49+
| Visual Studio 2017 | 15.0 | v141 |
50+
| Visual Studio 2019 | 16.0 | v142 |
51+
| Visual Studio 2022 | 17.0 | v143 |
52+
53+
The `wolfssl` C project can also have the Toolset modified by right-clicking on the project and selecting "Retarget Projects".
54+
55+
Retargeting typically only offers to upgrade to the latest Platform Toolset, so the `wolfssl.vcxproj` file
56+
will need to be manually edited if older versions are required.
57+
58+
### Debugging Native Code
59+
60+
Right-click on the `wolfSSL_CSharp` project, select `Properties` and
61+
navigate to the `Debug` panel.
62+
63+
Be sure to check the box under `Debugger engines`: [x] `Enable native code debugging`.
64+
65+
This will allow single-step debugging into the native wolfSSL C library.
66+
67+
Do this also for the startup project being debugged.
68+
69+
If the error `Interop debugging is not supported` is encountered,
70+
check which version of the .NET framework is being used.
71+
Only 4.x or later supports native code debugging from a C# app.
72+
73+
See also:
74+
75+
https://learn.microsoft.com/en-us/visualstudio/debugger/debugging-native-code
76+
77+
### Calling Convention
78+
79+
The wolfSSL functions are wrapped like this:
80+
81+
```
82+
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
83+
private extern static int wolfSSL_Init();
84+
```
85+
86+
Where `wolfssl_dll` is a constant compile-time string that points to the `wolfssl.dll` file.
87+
88+
### Troubleshooting Windows DLL
89+
90+
The `wolfssl.dll` file is created with the `DLL Debug` and `DLL Release` configurations
91+
and is typically compiled to:
92+
93+
```
94+
C:\workspace\wolfssl-%USERNAME$\wrapper\CSharp\Debug\x64\wolfssl.dll
95+
```
96+
97+
From a developer command prompt:
98+
99+
```
100+
dumpbin /EXPORTS C:\workspace\wolfssl-$USER\wrapper\CSharp\Debug\x64\wolfssl.dll
101+
```
102+
103+
There should be a long list of functions. If not, be sure to build with `WOLFSSL_DLL` (should be automatically included with DLL Debug/Release configurations).
104+
105+
See the project file `PreprocessorDefinitions` section:
106+
107+
```
108+
<PreprocessorDefinitions>BUILDING_WOLFSSL;WOLFSSL_DLL;WOLFSSL_USER_SETTINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
109+
```
110+
111+
If wolfSSL was _not_ compiled with `WOLFSSL_DLL` the `Unable to find an entry point` will be encountered:
112+
113+
```
114+
wolfssl init error System.EntryPointNotFoundException: Unable to find an entry point named 'wolfSSL_Init' in DLL 'wolfssl.dll'.
115+
at wolfSSL.CSharp.wolfssl.wolfSSL_Init()
116+
at wolfSSL.CSharp.wolfssl.Init() in C:\workspace\wolfssl-%USERNAME%\wrapper\CSharp\wolfSSL_CSharp\wolfSSL.cs:line nnn
117+
Calling ctx Init from wolfSSL
118+
```
119+
120+
Note the `WOLFSSL_DLL` is used in the wolfssl `wolfssl/wolfssl/wolfcrypt/visibility.h` and defines the `WOLFSSL_API` like this:
121+
122+
```
123+
#define WOLFSSL_API __declspec(dllexport)
124+
```
125+
126+
Only the wolfSSL function declarations decorated with this tag will be visible ion the DLL.
127+
128+
#### Finding the wolfssl.dll
129+
130+
The most common problem encountered on Windows is the DLL location.
131+
132+
If not developing wolfSSL for Windows, one option is to copy the `wolfssl.dll` to `C:\Windows\System32\`.
133+
134+
Another option is to add to the system environment path or your user environment path:
135+
136+
```
137+
set PATH=%PATH%;C:\path\to\your\wolfssl.dll
138+
```
139+
140+
#### Check Architecture (x86 vs x64)
141+
142+
Your C# application must match the architecture of wolfssl.dll:
143+
144+
- If your C# app is x64, wolfssl.dll must be 64-bit.
145+
- If your C# app is x86, wolfssl.dll must be 32-bit.
146+
147+
#### Ensure wolfssl.dll is Unblocked
148+
149+
If you downloaded wolfssl.dll from the Internet, Windows may block it.
150+
151+
Right-click wolfssl.dll, go to Properties, and check Unblock under Security.
152+
153+
154+
## Linux (Ubuntu) using WSL
155+
156+
The Microsoft Windows Subsystem for Linux cam be used for wolfSSL.
157+
158+
```bash
159+
sudo
160+
sudo apt-get update
161+
sudo apt-get upgrade
162+
sudo apt install -y build-essential autoconf automake libtool pkg-config
163+
164+
export WORKSPACE="/mnt/c/workspace"
165+
export WOLFSSL_ROOT="$WORKSPACE/wolfssl-$USER"
166+
167+
cd "WOLFSSL_ROOT"
168+
```
169+
170+
When using a git repository, run `autogen.sh`:
171+
172+
```
173+
cd "$WOLFSSL_ROOT"
174+
./autogen.sh
175+
```
176+
177+
### Build wolfSSL and install it system-wide
178+
179+
To have a single instance of wolfSSL:
180+
181+
```
182+
./configure
183+
make
184+
make check # (optional, but highly recommended)
185+
sudo make install
186+
```
187+
188+
### Build wolfSSL and install to arbitrary directory
189+
190+
To have an isolated instance of wolfSSL, in this case `$HOME/wolfssl-install-psk`:
191+
192+
```bash
193+
make clean
194+
make distclean
195+
196+
rm -rf "$HOME/wolfssl-install-psk"
197+
198+
./configure --enable-all --disable-crypttests \
199+
--disable-examples \
200+
--enable-opensslall --enable-opensslextra \
201+
--enable-tls13 --enable-dtls13 --enable-dtls --enable-psk \
202+
CFLAGS="-DWOLFSSL_STATIC_PSK" --enable-shared \
203+
--prefix="$HOME/wolfssl-install-psk"
204+
205+
make -j$(nproc)
206+
make install
207+
```
208+
209+
210+
211+
### Compile specific example in WSL
212+
213+
```bash
214+
# Optionally fetch additional examples
215+
git clone https://github.com/wolfSSL/wolfssl-examples.git "$WORKSPACE/wolfssl-examples-$USER"
216+
cd "$WORKSPACE/wolfssl-examples-$USER"
217+
218+
THIS_EXAMPLE="client-dtls-psk"
219+
220+
export WOLFSSL_DIR="$HOME/wolfssl-install-psk"
221+
export CFLAGS="-I$WOLFSSL_DIR/include"
222+
export LDFLAGS="-L$WOLFSSL_DIR/lib"
223+
224+
export LD_LIBRARY_PATH="$HOME/wolfssl-install-psk/lib:$LD_LIBRARY_PATH"
225+
226+
gcc -o "$THIS_EXAMPLE" "$THIS_EXAMPLE".c \
227+
-I$HOME/wolfssl-install-psk/include \
228+
-L$HOME/wolfssl-install-psk/lib -Wl,-rpath=$HOME/wolfssl-install-psk/lib -lwolfssl -lm
229+
```
230+
31231
## Linux (Ubuntu) using mono
32232

33233
Prerequisites for linux:
34234

35-
```
235+
```bash
36236
apt-get update
37237
apt-get upgrade
38238
apt-get install mono-complete
39239
```
40240

41-
### Build wolfSSL and install
241+
### Build wolfSSL and install system-wide
42242

43-
```
243+
```bash
44244
./autogen.sh
45245
./configure --enable-keygen --enable-eccencrypt --enable-ed25519 --enable-curve25519 --enable-aesgcm
46246
make
@@ -52,9 +252,9 @@ sudo make install
52252

53253
From the `wrapper/CSharp` directory (`cd wrapper/CSharp`):
54254

55-
Compile wolfCrypt test:
255+
Compile wolfCrypt test with mono:
56256

57-
```
257+
```bash
58258
mcs wolfCrypt-Test/wolfCrypt-Test.cs wolfSSL_CSharp/wolfCrypt.cs -OUT:wolfcrypttest.exe
59259
mono wolfcrypttest.exe
60260
```
@@ -63,15 +263,15 @@ mono wolfcrypttest.exe
63263

64264
From the `wrapper/CSharp` directory (`cd wrapper/CSharp`):
65265

66-
Compile server:
266+
Compile server with mono:
67267

68-
```
268+
```bash
69269
mcs wolfSSL_CSharp/wolfSSL.cs wolfSSL_CSharp/X509.cs wolfSSL-TLS-Server/wolfSSL-TLS-Server.cs -OUT:server.exe
70270
```
71271

72-
Compile client:
272+
Compile client with mono:
73273

74-
```
274+
```bash
75275
mcs wolfSSL_CSharp/wolfSSL.cs wolfSSL_CSharp/X509.cs wolfSSL-TLS-Client/wolfSSL-TLS-Client.cs -OUT:client.exe
76276
```
77277

0 commit comments

Comments
 (0)