HttpServerBoxed 0.0.11 alpha
A simple http server for C# and .NET
Loading...
Searching...
No Matches
HSB.SslConfiguration Class Reference

Public Member Functions

 SslConfiguration (string certificatePath, string certificatePassword)
 
 SslConfiguration (string certificatePath, string certificatePassword, List< TLSVersion > tlsVersions, bool checkCertificateRevocation=true, bool validateClientCertificate=false, bool clientCertificateRequired=false)
 
bool IsEnabled ()
 
bool IsDebugModeEnabled ()
 
X509Certificate2 GetCertificate ()
 Returns a X509Certificate2 object from the certificate path and password. If a certificate in form of bytes is provided, it will be used instead of the path.
 
void SetCertificate (string path)
 Sets the certificate path.
 
void SetCertificate (byte[] bytes)
 Sets the certificate data.
 
void SetCertificatePassword (string password)
 Sets the certificate password.
 

Static Public Member Functions

static SslConfiguration FromJSON (JsonElement json)
 
static bool CreateDebugCertificate ()
 Creates a developer certificate valid only for 1 month and for localhost. OpenSSL is required to use this feature.
 
static ? X509Certificate2 TryLoadDebugCertificate (bool create=true, Configuration? c=null)
 

Public Attributes

ushort SslPort = 8443
 
SSL_PORT_MODE PortMode = SSL_PORT_MODE.DUAL_PORT
 
bool UpgradeUnsecureRequests = true
 
string? CertificatePath
 
bool UseDebugCertificate = false
 When set, a developer certificate valid only for 1 month and for localhost will be created (if doesn't exist or is expired) and used. OpenSSL is required to use this feature.
 
byte?[] CertificateBytes
 
string? CertificatePassword
 
List< TLSVersion > TLSVersions
 
bool CheckCertificateRevocation = true
 
bool ValidateClientCertificate = true
 
bool ClientCertificateRequired = false
 

Detailed Description

Definition at line 15 of file SslConfiguration.cs.

Constructor & Destructor Documentation

◆ SslConfiguration() [1/3]

HSB.SslConfiguration.SslConfiguration ( )

Definition at line 48 of file SslConfiguration.cs.

49 {
50
51 TLSVersions = [];
52
53 }

◆ SslConfiguration() [2/3]

HSB.SslConfiguration.SslConfiguration ( string  certificatePath,
string  certificatePassword 
)

Definition at line 55 of file SslConfiguration.cs.

56 {
57
58 TLSVersions = [];
59 CertificatePassword = certificatePassword;
60 CertificatePath = certificatePath;
61 }

◆ SslConfiguration() [3/3]

HSB.SslConfiguration.SslConfiguration ( string  certificatePath,
string  certificatePassword,
List< TLSVersion >  tlsVersions,
bool  checkCertificateRevocation = true,
bool  validateClientCertificate = false,
bool  clientCertificateRequired = false 
)

Definition at line 63 of file SslConfiguration.cs.

70 {
71
72 TLSVersions = tlsVersions;
73 CertificatePassword = certificatePassword;
74 CertificatePath = certificatePath;
75 CheckCertificateRevocation = checkCertificateRevocation;
76 ValidateClientCertificate = validateClientCertificate;
77 ClientCertificateRequired = clientCertificateRequired;
78 }

Member Function Documentation

◆ CreateDebugCertificate()

static bool HSB.SslConfiguration.CreateDebugCertificate ( )
static

Creates a developer certificate valid only for 1 month and for localhost. OpenSSL is required to use this feature.

Definition at line 193 of file SslConfiguration.cs.

194 {
195 //check if openssl is installed
196 var startInfo = new ProcessStartInfo
197 {
198 RedirectStandardOutput = true,
199 RedirectStandardError = true,
200 UseShellExecute = false,
201 CreateNoWindow = true
202 };
203
204 if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
205 {
206 startInfo.FileName = "cmd.exe";
207 startInfo.Arguments = $"/C openssl version";
208
209 }
210 else
211 {
212 startInfo.FileName = "/bin/bash";
213 startInfo.Arguments = $"-c openssl version";
214 }
215 var process = new Process
216 {
217 StartInfo = startInfo
218 };
219 process.Start();
220 process.WaitForExit();
221 if (process.ExitCode != 0)
222 {
223 Terminal.ERROR($"Openssl is not installed, cannot continue ({process.ExitCode})", true);
224 return false;
225 }
226
227 //if old certificate exists, delete it
228 if (Directory.Exists(DEBUG_CERT_FOLDER_PATH))
229 {
230 if (File.Exists(DEBUG_CERT_P12_PATH))
231 {
232 File.Delete(DEBUG_CERT_P12_PATH);
233 }
234 if (File.Exists(DEBUG_CERT_CRT_PATH))
235 {
236 File.Delete(DEBUG_CERT_CRT_PATH);
237 }
238 if (File.Exists(DEBUG_CERT_KEY_PATH))
239 {
240 File.Delete(DEBUG_CERT_KEY_PATH);
241 }
242 }
243 else //create folder if not exists
244 {
245 Directory.CreateDirectory(DEBUG_CERT_FOLDER_PATH);
246 }
247
248
249 var command =
250 $"openssl version && openssl " +
251 $"req -x509 -newkey rsa:4096 -sha256 -days 30 -nodes -subj \"/CN=localhost/C=US\" " +
252 $"-keyout \"{DEBUG_CERT_KEY_PATH}\" " +
253 $"-out \"{DEBUG_CERT_CRT_PATH}\" && " +
254 $"openssl pkcs12 -export " +
255 $"-out \"{DEBUG_CERT_P12_PATH}\" " +
256 $"-inkey \"{DEBUG_CERT_KEY_PATH}\" " +
257 $"-in \"{DEBUG_CERT_CRT_PATH}\" " +
258 $"-passout pass:\"{DEBUG_CERT_PASSWORD}\"";
259
260
261 Terminal.DEBUG($"Creating debug certificate with command: {command}");
262
263
264
265 //if windows
266 if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
267 {
268 startInfo.FileName = "cmd.exe";
269 startInfo.Arguments = $"/C \"{command}\"";
270
271 }
272 else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
273 {
274 startInfo.FileName = "/bin/zsh";
275 startInfo.Arguments = $"-c \"{command}\"";
276 }
277 else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
278 {
279 startInfo.FileName = "/bin/bash";
280 startInfo.Arguments = $"-c \"{command}\"";
281 }
282 startInfo.RedirectStandardOutput = false;
283 startInfo.RedirectStandardError = true;
284 process = new()
285 {
286 StartInfo = startInfo
287 };
288
289 process.Start();
290 process.WaitForExit();
291
292 if (process.ExitCode != 0)
293 {
294 Terminal.WARNING($"Openssl error, certificate has not been created\nCommand used is : {command}", true);
295 return false;
296 }
297 else
298 {
299 Terminal.DEBUG($"Debug certificate (valid only for localhost) created successfully");
300 }
301 return true;
302 }

◆ FromJSON()

static SslConfiguration HSB.SslConfiguration.FromJSON ( JsonElement  json)
static

Definition at line 147 of file SslConfiguration.cs.

148 {
149
150 //IsEnabled
151
152 var lastProp = "SslPort";
153 try
154 {
155
156 var SslPort = Utils.Safe(json.GetProperty("SslPort").GetUInt16(), (ushort)8443);
157 lastProp = "PortMode"; SSL_PORT_MODE PortMode = (SSL_PORT_MODE)Utils.Safe(json.GetProperty("PortMode").GetInt16(), (int)SSL_PORT_MODE.DUAL_PORT);
158 lastProp = "upgradeUnsecureRequests"; bool upgradeUnsecureRequests = Utils.Safe(json.GetProperty("UpgradeUnsecureRequests").GetBoolean(), true);
159 lastProp = "CertificatePath"; var CertificatePath = json.GetProperty("CertificatePath").GetString();
160 lastProp = "CertificatePassword"; var CertificatePassword = json.GetProperty("CertificatePassword").GetString();
161 lastProp = "CheckCertificateRevocation"; var CheckCertificateRevocation = Utils.Safe(json.GetProperty("CheckCertificateRevocation").GetBoolean(), true);
162 lastProp = "ValidateClientCertificate"; var ValidateClientCertificate = Utils.Safe(json.GetProperty("ValidateClientCertificate").GetBoolean(), false);
163 lastProp = "ClientCertificateRequired"; var ClientCertificateRequired = Utils.Safe(json.GetProperty("ClientCertificateRequired").GetBoolean(), false);
164 lastProp = "tlsVersions"; var tlsVersions = json.GetProperty("TLSVersions").EnumerateArray().Select(x => (TLSVersion)x.GetInt16()).ToList();
165
166 return new()
167 {
168
169 PortMode = PortMode,
170 SslPort = SslPort,
171 UpgradeUnsecureRequests = upgradeUnsecureRequests,
172 CertificatePath = CertificatePath,
173 CertificatePassword = CertificatePassword,
174 TLSVersions = tlsVersions,
175 CheckCertificateRevocation = CheckCertificateRevocation,
176 ValidateClientCertificate = ValidateClientCertificate,
177 ClientCertificateRequired = ClientCertificateRequired,
178 };
179 }
180 catch (Exception e)
181 {
182 Terminal.ERROR("Error while parsing SslSettings property: " + lastProp + " " + e.Message);
183 return new();
184 }
185
186
187 }
SSL_PORT_MODE
This enum is used to determine if the server should listen on a single port for both HTTP and HTTPS o...
Definition SSLPortMode.cs:7

◆ GetCertificate()

X509Certificate2 HSB.SslConfiguration.GetCertificate ( )

Returns a X509Certificate2 object from the certificate path and password. If a certificate in form of bytes is provided, it will be used instead of the path.

Returns

Definition at line 89 of file SslConfiguration.cs.

90 {
91 if (CertificateBytes != null)
92 {
93 return new(CertificateBytes, CertificatePassword!);
94 }
95 return new(CertificatePath!, CertificatePassword!);
96 }

◆ SetCertificate() [1/2]

void HSB.SslConfiguration.SetCertificate ( byte[]  bytes)

Sets the certificate data.

Parameters
bytes

Definition at line 109 of file SslConfiguration.cs.

110 {
111 CertificateBytes = bytes;
112 }

◆ SetCertificate() [2/2]

void HSB.SslConfiguration.SetCertificate ( string  path)

Sets the certificate path.

Parameters
path

Definition at line 101 of file SslConfiguration.cs.

102 {
103 CertificatePath = path;
104 }

◆ SetCertificatePassword()

void HSB.SslConfiguration.SetCertificatePassword ( string  password)

Sets the certificate password.

Parameters
password

Definition at line 117 of file SslConfiguration.cs.

118 {
119 CertificatePassword = password;
120 }

◆ TryLoadDebugCertificate()

static ? X509Certificate2 HSB.SslConfiguration.TryLoadDebugCertificate ( bool  create = true,
Configuration c = null 
)
static

Definition at line 304 of file SslConfiguration.cs.

305 {
306
307 X509Certificate2 cert;
308
309 if (!File.Exists(DEBUG_CERT_P12_PATH))
310 {
311 if (create)
312 {
314 {
315 c?.Debug.WARNING("Cannot load debug certificate, file not found");
316 return null;
317 }
318
319 }
320 else
321 {
322 c?.Debug.WARNING("Cannot load debug certificate, file not found");
323 //Terminal.DEBUG("Cannot load debug certificate, file not found");
324 return null;
325 }
326 }
327 //workaround to avoid error if path contains spaces
328 var bytes = File.ReadAllBytes(DEBUG_CERT_P12_PATH);
329 cert = new X509Certificate2(bytes, DEBUG_CERT_PASSWORD);
330
331 //if expired, delete and create again
332 if (cert.NotAfter < DateTime.Now)
333 {
334 File.Delete(DEBUG_CERT_P12_PATH);
336 {
337 c?.Debug.DEBUG("Cannot load debug certificate, file not found");
338 return null;
339 }
340
341 cert = new X509Certificate2(DEBUG_CERT_P12_PATH, DEBUG_CERT_PASSWORD);
342 }
343
344 c?.Debug.DEBUG("Debug certificate loaded. Remember to trust it in your system!");
345 return cert;
346 }
static bool CreateDebugCertificate()
Creates a developer certificate valid only for 1 month and for localhost. OpenSSL is required to use ...

Member Data Documentation

◆ CertificateBytes

byte? [] HSB.SslConfiguration.CertificateBytes

Definition at line 40 of file SslConfiguration.cs.

◆ CertificatePassword

string? HSB.SslConfiguration.CertificatePassword

Definition at line 41 of file SslConfiguration.cs.

◆ CertificatePath

string? HSB.SslConfiguration.CertificatePath

Definition at line 32 of file SslConfiguration.cs.

◆ CheckCertificateRevocation

bool HSB.SslConfiguration.CheckCertificateRevocation = true

Definition at line 43 of file SslConfiguration.cs.

◆ ClientCertificateRequired

bool HSB.SslConfiguration.ClientCertificateRequired = false

Definition at line 45 of file SslConfiguration.cs.

◆ PortMode

SSL_PORT_MODE HSB.SslConfiguration.PortMode = SSL_PORT_MODE.DUAL_PORT

Definition at line 30 of file SslConfiguration.cs.

◆ SslPort

ushort HSB.SslConfiguration.SslPort = 8443

Definition at line 29 of file SslConfiguration.cs.

◆ TLSVersions

List<TLSVersion> HSB.SslConfiguration.TLSVersions

Definition at line 42 of file SslConfiguration.cs.

◆ UpgradeUnsecureRequests

bool HSB.SslConfiguration.UpgradeUnsecureRequests = true

Definition at line 31 of file SslConfiguration.cs.

◆ UseDebugCertificate

bool HSB.SslConfiguration.UseDebugCertificate = false

When set, a developer certificate valid only for 1 month and for localhost will be created (if doesn't exist or is expired) and used. OpenSSL is required to use this feature.

Definition at line 38 of file SslConfiguration.cs.

◆ ValidateClientCertificate

bool HSB.SslConfiguration.ValidateClientCertificate = true

Definition at line 44 of file SslConfiguration.cs.


The documentation for this class was generated from the following file: