Creates a developer certificate valid only for 1 month and for localhost. OpenSSL is required to use this feature.
194 {
195
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
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
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
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 }