Skip to content

Commit 2ab709c

Browse files
committed
- Platform specific function to correctly set the path for the certificates;
- Updated all the examples with it;
1 parent 6cb97a7 commit 2ab709c

9 files changed

Lines changed: 130 additions & 52 deletions

File tree

wrapper/CSharp/wolfSSL-DTLS-PSK-Server/wolfSSL-DTLS-PSK-Server.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,14 @@ public static void Main(string[] args)
7878
IntPtr ssl;
7979

8080
/* These paths should be changed according to use */
81-
string fileCert = @"server-cert.pem";
82-
string fileKey = @"server-key.pem";
83-
StringBuilder dhparam = new StringBuilder("dh2048.pem");
81+
string fileCert = wolfssl.setPath("server-cert.pem");
82+
string fileKey = wolfssl.setPath("server-key.pem");
83+
StringBuilder dhparam = new StringBuilder(wolfssl.setPath("dh2048.pem"));
84+
85+
if (fileCert == "" || fileKey == "" || dhparam.Length == 0) {
86+
Console.WriteLine("Platform not supported");
87+
return;
88+
}
8489

8590
wolfssl.psk_delegate psk_cb = new wolfssl.psk_delegate(my_psk_server_cb);
8691

@@ -106,6 +111,12 @@ public static void Main(string[] args)
106111
return;
107112
}
108113

114+
if (!File.Exists(dhparam.ToString())) {
115+
Console.WriteLine("Could not find dh file");
116+
wolfssl.CTX_free(ctx);
117+
return;
118+
}
119+
109120

110121
if (wolfssl.CTX_use_certificate_file(ctx, fileCert, wolfssl.SSL_FILETYPE_PEM) != wolfssl.SUCCESS)
111122
{

wrapper/CSharp/wolfSSL-DTLS-Server/wolfSSL-DTLS-Server.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,14 @@ public static void Main(string[] args)
5858
IntPtr ssl;
5959

6060
/* These paths should be changed for use */
61-
string fileCert = @"server-cert.pem";
62-
string fileKey = @"server-key.pem";
63-
StringBuilder dhparam = new StringBuilder("dh2048.pem");
61+
string fileCert = wolfssl.setPath("server-cert.pem");
62+
string fileKey = wolfssl.setPath(@"server-key.pem");
63+
StringBuilder dhparam = new StringBuilder(wolfssl.setPath("dh2048.pem"));
64+
65+
if (fileCert == "" || fileKey == "" || dhparam.Length == 0) {
66+
Console.WriteLine("Platform not supported");
67+
return;
68+
}
6469

6570
StringBuilder buff = new StringBuilder(1024);
6671
StringBuilder reply = new StringBuilder("Hello, this is the wolfSSL C# wrapper");
@@ -87,6 +92,12 @@ public static void Main(string[] args)
8792
return;
8893
}
8994

95+
if (!File.Exists(dhparam.ToString())) {
96+
Console.WriteLine("Could not find dh file");
97+
wolfssl.CTX_free(ctx);
98+
return;
99+
}
100+
90101

91102
if (wolfssl.CTX_use_certificate_file(ctx, fileCert, wolfssl.SSL_FILETYPE_PEM) != wolfssl.SUCCESS)
92103
{

wrapper/CSharp/wolfSSL-Example-IOCallbacks/wolfSSL-Example-IOCallbacks.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,12 +214,17 @@ static void Main(string[] args)
214214
IntPtr ssl;
215215
Socket fd;
216216

217-
wolfssl.psk_delegate psk_cb = new wolfssl.psk_delegate(my_psk_server_cb);
218217
wolfssl.CallbackVerify_delegate verify_cb = new wolfssl.CallbackVerify_delegate(my_verify_cb);
219218

220219
/* These paths should be changed according to use */
221-
string fileCert = @"server-cert.pem";
222-
string fileKey = @"server-key.pem";
220+
string fileCert = wolfssl.setPath("server-cert.pem");
221+
string fileKey = wolfssl.setPath("server-key.pem");
222+
StringBuilder dhparam = new StringBuilder(wolfssl.setPath("dh2048.pem"));
223+
224+
if (fileCert == "" || fileKey == "" || dhparam.Length == 0) {
225+
Console.WriteLine("Platform not supported");
226+
return;
227+
}
223228

224229
StringBuilder buff = new StringBuilder(1024);
225230
StringBuilder reply = new StringBuilder("Hello, this is the wolfSSL C# wrapper");
@@ -242,6 +247,12 @@ static void Main(string[] args)
242247
return;
243248
}
244249

250+
if (!File.Exists(dhparam.ToString())) {
251+
Console.WriteLine("Could not find dh file");
252+
wolfssl.CTX_free(ctx);
253+
return;
254+
}
255+
245256
if (wolfssl.CTX_use_certificate_file(ctx, fileCert, wolfssl.SSL_FILETYPE_PEM) != wolfssl.SUCCESS)
246257
{
247258
Console.WriteLine("Error in setting cert file");

wrapper/CSharp/wolfSSL-TLS-Client/wolfSSL-TLS-Client.cs

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,6 @@ private static int haveSNI(string[] args)
7777
return -1;
7878
}
7979

80-
public static string setPath() {
81-
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
82-
{
83-
return @"../../certs/ca-cert.pem";
84-
} else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
85-
{
86-
return @"../../../../certs/ca-cert.pem";
87-
} else
88-
{
89-
return "";
90-
}
91-
}
92-
9380
public static void Main(string[] args)
9481
{
9582
IntPtr ctx;
@@ -98,14 +85,14 @@ public static void Main(string[] args)
9885
IntPtr sniHostName;
9986

10087
/* These paths should be changed for use */
101-
string caCert = setPath();
102-
if (caCert == "") {
88+
string caCert = wolfssl.setPath("ca-cert.pem");
89+
StringBuilder dhparam = new StringBuilder(wolfssl.setPath("dh2048.pem"));
90+
91+
if (caCert == "" || dhparam.Length == 0) {
10392
Console.WriteLine("Platform not supported.");
10493
return;
10594
}
10695

107-
StringBuilder dhparam = new StringBuilder("dh2048.pem");
108-
10996
StringBuilder buff = new StringBuilder(1024);
11097
StringBuilder reply = new StringBuilder("Hello, this is the wolfSSL C# wrapper");
11198

@@ -131,6 +118,12 @@ public static void Main(string[] args)
131118
return;
132119
}
133120

121+
if (!File.Exists(dhparam.ToString())) {
122+
Console.WriteLine("Could not find dh file");
123+
wolfssl.CTX_free(ctx);
124+
return;
125+
}
126+
134127
if (wolfssl.CTX_load_verify_locations(ctx, caCert, null)
135128
!= wolfssl.SUCCESS)
136129
{

wrapper/CSharp/wolfSSL-TLS-PSK-Client/wolfSSL-TLS-PSK-Client.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,11 @@ public static void Main(string[] args)
8282

8383
wolfssl.psk_client_delegate psk_cb = new wolfssl.psk_client_delegate(my_psk_client_cb);
8484

85-
StringBuilder dhparam = new StringBuilder("dh2048.pem");
85+
StringBuilder dhparam = new StringBuilder(wolfssl.setPath("dh2048.pem"));
86+
if (dhparam.Length == 0) {
87+
Console.WriteLine("Platform not supported");
88+
return;
89+
}
8690

8791
StringBuilder buff = new StringBuilder(1024);
8892
StringBuilder reply = new StringBuilder("Hello, this is the wolfSSL C# client psk wrapper");
@@ -157,6 +161,12 @@ public static void Main(string[] args)
157161
return;
158162
}
159163

164+
if (!File.Exists(dhparam.ToString())) {
165+
Console.WriteLine("Could not find dh file");
166+
wolfssl.CTX_free(ctx);
167+
return;
168+
}
169+
160170
wolfssl.SetTmpDH_file(ssl, dhparam, wolfssl.SSL_FILETYPE_PEM);
161171

162172
if (wolfssl.connect(ssl) != wolfssl.SUCCESS)

wrapper/CSharp/wolfSSL-TLS-PSK-Server/wolfSSL-TLS-PSK-Server.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,14 @@ public static void Main(string[] args)
8080
wolfssl.psk_delegate psk_cb = new wolfssl.psk_delegate(my_psk_server_cb);
8181

8282
/* These paths should be changed according to use */
83-
string fileCert = @"server-cert.pem";
84-
string fileKey = @"server-key.pem";
85-
StringBuilder dhparam = new StringBuilder("dh2048.pem");
83+
string fileCert = wolfssl.setPath("server-cert.pem");
84+
string fileKey = wolfssl.setPath("server-key.pem");
85+
StringBuilder dhparam = new StringBuilder(wolfssl.setPath("dh2048.pem"));
86+
87+
if (fileCert == "" || fileKey == "" || dhparam.Length == 0) {
88+
Console.WriteLine("Platform not supported");
89+
return;
90+
}
8691

8792
StringBuilder buff = new StringBuilder(1024);
8893
StringBuilder reply = new StringBuilder("Hello, this is the wolfSSL C# wrapper");
@@ -105,6 +110,12 @@ public static void Main(string[] args)
105110
return;
106111
}
107112

113+
if (!File.Exists(dhparam.ToString())) {
114+
Console.WriteLine("Could not find dh file");
115+
wolfssl.CTX_free(ctx);
116+
return;
117+
}
118+
108119
if (wolfssl.CTX_use_certificate_file(ctx, fileCert, wolfssl.SSL_FILETYPE_PEM) != wolfssl.SUCCESS)
109120
{
110121
Console.WriteLine("Error in setting cert file");

wrapper/CSharp/wolfSSL-TLS-Server/wolfSSL-TLS-Server.cs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,6 @@ public static int my_sni_server_cb(IntPtr ssl, IntPtr ret, IntPtr exArg) {
8080
return 0;
8181
}
8282

83-
public static string setPath(string file) {
84-
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
85-
{
86-
return @"../../certs/" + file;
87-
} else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
88-
{
89-
return @"../../../../certs/" + file;
90-
} else
91-
{
92-
return "";
93-
}
94-
}
95-
9683
public static void Main(string[] args)
9784
{
9885
IntPtr ctx;
@@ -101,15 +88,15 @@ public static void Main(string[] args)
10188
IntPtr arg_sni;
10289

10390
/* These paths should be changed for use */
104-
string fileCert = setPath("server-cert.pem");
105-
string fileKey = setPath("server-key.pem");
106-
if (fileCert == "" || fileKey == "") {
91+
string fileCert = wolfssl.setPath("server-cert.pem");
92+
string fileKey = wolfssl.setPath("server-key.pem");
93+
StringBuilder dh2048Pem = new StringBuilder(wolfssl.setPath("dh2048.pem"));
94+
95+
if (fileCert == "" || fileKey == "" || dh2048Pem.Length == 0) {
10796
Console.WriteLine("Platform not supported.");
10897
return;
10998
}
11099

111-
StringBuilder dhparam = new StringBuilder("dh2048.pem");
112-
113100
StringBuilder buff = new StringBuilder(1024);
114101
StringBuilder reply = new StringBuilder("Hello, this is the wolfSSL C# wrapper");
115102

@@ -134,6 +121,12 @@ public static void Main(string[] args)
134121
return;
135122
}
136123

124+
if (!File.Exists(dhparam.ToString())) {
125+
Console.WriteLine("Could not find dh file");
126+
wolfssl.CTX_free(ctx);
127+
return;
128+
}
129+
137130
if (wolfssl.CTX_use_certificate_file(ctx, fileCert, wolfssl.SSL_FILETYPE_PEM) != wolfssl.SUCCESS)
138131
{
139132
Console.WriteLine("Error in setting cert file");
@@ -197,7 +190,14 @@ public static void Main(string[] args)
197190
return;
198191
}
199192

200-
wolfssl.SetTmpDH_file(ssl, dhparam, wolfssl.SSL_FILETYPE_PEM);
193+
if (wolfssl.SetTmpDH_file(ssl, dh2048Pem, wolfssl.SSL_FILETYPE_PEM) != wolfssl.SUCCESS)
194+
{
195+
Console.WriteLine("Error in setting dh2048Pem");
196+
Console.WriteLine(wolfssl.get_error(ssl));
197+
tcp.Stop();
198+
clean(ssl, ctx);
199+
return;
200+
}
201201

202202
if (wolfssl.accept(ssl) != wolfssl.SUCCESS)
203203
{

wrapper/CSharp/wolfSSL-TLS-ServerThreaded/wolfSSL-TLS-ServerThreaded.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,14 @@ public static void Main(string[] args)
116116
IntPtr ctx;
117117

118118
/* These paths should be changed for use */
119-
string fileCert = @"server-cert.pem";
120-
string fileKey = @"server-key.pem";
121-
StringBuilder dhparam = new StringBuilder("dh2048.pem");
119+
string fileCert = wolfssl.setPath("server-cert.pem");
120+
string fileKey = wolfssl.setPath("server-key.pem");
121+
StringBuilder dhparam = new StringBuilder(wolfssl.setPath("dh2048.pem"));
122+
123+
if (fileCert == "" || fileKey == "" || dhparam.Length == 0) {
124+
Console.WriteLine("Platform not supported");
125+
return;
126+
}
122127

123128
/* example of function used for setting logging */
124129
wolfssl.SetLogging(standard_log);
@@ -140,6 +145,12 @@ public static void Main(string[] args)
140145
return;
141146
}
142147

148+
if (!File.Exists(dhparam.ToString())) {
149+
Console.WriteLine("Could not find dh file");
150+
wolfssl.CTX_free(ctx);
151+
return;
152+
}
153+
143154
if (wolfssl.CTX_use_certificate_file(ctx, fileCert, wolfssl.SSL_FILETYPE_PEM) != wolfssl.SUCCESS)
144155
{
145156
Console.WriteLine("Error in setting cert file");

wrapper/CSharp/wolfSSL_CSharp/wolfSSL.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,26 @@ private static IntPtr unwrap_ssl(IntPtr ssl)
485485
}
486486
}
487487

488+
/// <summary>
489+
/// Utility function used to access the certificates
490+
/// based on the platform.
491+
/// <returns>return the platform specific path to the certificate</returns>
492+
/// </summary>
493+
public static string setPath(string file) {
494+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
495+
{
496+
Console.WriteLine("Linux - " + file);
497+
return @"../../certs/" + file;
498+
} else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
499+
{
500+
Console.WriteLine("Windows - " + file);
501+
return @"../../../../certs/" + file;
502+
} else
503+
{
504+
return "";
505+
}
506+
}
507+
488508

489509
/// <summary>
490510
/// Call back to allow receiving TLS information

0 commit comments

Comments
 (0)