Пример #1
0
        private static SSL CreateSelfSignedSSLForWin(string domain)
        {
            var openSSLDir = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "openssl");

            var execPath = Path.Combine(openSSLDir, "openssl.exe");

            var baseFileName = Guid.NewGuid().ToString("N");

            //Load base config file, append needed data and save for later usage
            var configFilePath    = Path.Combine(openSSLDir, $"{baseFileName}.cnf");
            var configFileContent = File.ReadAllText(Path.Combine(openSSLDir, "openssl.cnf")).Replace("{dir_placeholder}", openSSLDir + "\\ssl") +
                                    $"[SAN]\nsubjectAltName=DNS:{domain}";

            File.WriteAllText(configFilePath, configFileContent);

            var args = string.Format(winCmd, domain, baseFileName, openSSLDir, configFilePath);

            using (var psi = new Process
            {
                StartInfo = new ProcessStartInfo(execPath, args)
                {
                    UseShellExecute = true,
                }
            })
            {
                psi.Start();

                psi.WaitForExit();
            }

            var certCrtPath = Path.Combine(openSSLDir, $"{baseFileName}.crt");
            var certKeyPath = Path.Combine(openSSLDir, $"{baseFileName}.key");

            var result = new SSL()
            {
                CertCrt = File.ReadAllText(certCrtPath),
                CertKey = File.ReadAllText(certKeyPath)
            };

            //InstallCertificate(certCrtPath);

            //Lets clear the path.
            File.Delete(certCrtPath);
            File.Delete(certKeyPath);
            File.Delete(configFilePath);

            return(result);
        }
Пример #2
0
        private static SSL CreateSelfSignedSSLForLinux(string domain)
        {
            var openSSLDir = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "openssl");

            var baseFileName = Guid.NewGuid().ToString("N");

            //Load base config file, append needed data and save for later usage
            var configFilePath    = Path.Combine(openSSLDir, $"{baseFileName}.cnf");
            var configFileContent = File.ReadAllText(Path.Combine(openSSLDir, "openssl_lx.cnf")).Replace("{dir_placeholder}", openSSLDir + "/ssl") +
                                    $"[SAN]\nsubjectAltName=DNS:{domain}";

            File.WriteAllText(configFilePath, configFileContent);

            var args = string.Format(linuxCmd, domain, baseFileName, openSSLDir, configFilePath).Replace("\"", "\\\"");

            var line = "";

            try
            {
                using (var psi = new Process
                {
                    StartInfo = new ProcessStartInfo($"/bin/bash", $"-c \"{args}\"")
                    {
                        UseShellExecute = false,
                        RedirectStandardOutput = true,
                        RedirectStandardError = true,
                        CreateNoWindow = true
                    }
                })
                {
                    psi.Start();

                    while (!psi.StandardOutput.EndOfStream)
                    {
                        line += psi.StandardOutput.ReadLine();
                    }

                    while (!psi.StandardError.EndOfStream)
                    {
                        line += psi.StandardError.ReadLine();
                    }

                    if (!string.IsNullOrEmpty(line))
                    {
                        line = $"/bin/bash -c \"{args}\"{Environment.NewLine}" + line;
                    }

                    psi.WaitForExit();
                }

                var certCrtPath = Path.Combine(openSSLDir, $"{baseFileName}.crt");
                var certKeyPath = Path.Combine(openSSLDir, $"{baseFileName}.key");

                var result = new SSL()
                {
                    CertCrt = File.ReadAllText(certCrtPath),
                    CertKey = File.ReadAllText(certKeyPath)
                };

                //Lets clear the path.
                File.Delete(certCrtPath);
                File.Delete(certKeyPath);
                File.Delete(configFilePath);

                return(result);
            }
            catch (Exception ex)
            {
                throw new Exception(line, ex);
            }
        }