// // OpenVPN Administrator // // Author(s): Everaldo Canuto // // (C) 2006 Everaldo Canuto // (C) 2006 The Gang // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // using System; using System.IO; using Microsoft.Win32; using System.Diagnostics; using System.ComponentModel; using Mono.Unix; namespace OpenVPN.Admin { /// /// Certificate class for OpenVPN Certificate Wizard. /// public class Certificate { #region Fields private bool win32; private string openssl = "openssl"; private string config = ""; private string keysize = "1024"; private string name; private string email; private string country; private string state; private string city; private string organization; private string unit; private string passphrase; private string path; private string fileprefix; private string keyfile; private string requestfile; #endregion #region Constructors and destructors public Certificate() { // Verify if is win32 enviroment. this.win32 = ((System.Environment.OSVersion.Platform == PlatformID.Win32NT) || (System.Environment.OSVersion.Platform == PlatformID.Win32Windows) || (System.Environment.OSVersion.Platform == PlatformID.Win32S)); // Set openssl path for win32 enviroments. if (this.win32) { RegistryKey registry = Registry.LocalMachine.OpenSubKey("Software\\OpenVPN"); if (registry != null) { string path = System.IO.Path.GetDirectoryName(registry.GetValue("exe_path").ToString()) +System.IO.Path.DirectorySeparatorChar.ToString() +"openssl."; if (File.Exists(path + "exe")) this.openssl = path + "exe"; if (File.Exists(path + "conf")) this.config = path + "conf"; } } } #endregion #region Public methods public void Make() { if (!System.IO.Directory.Exists(this.path)) { System.IO.Directory.CreateDirectory(this.path); if (!System.IO.Directory.Exists(this.path)) throw new Exception(Catalog.GetString("Invalid folder name!")); } this.keyfile = this.fileprefix + ".key"; this.requestfile = this.fileprefix + ".csr"; string subj = "\"/CN="+this.name; subj = subj + ((this.email == "") ? "" : "/emailAddress="+this.email); subj = subj + ((this.country == "") ? "" : "/C="+this.country ); subj = subj + ((this.state == "") ? "" : "/ST="+this.state); subj = subj + ((this.city == "") ? "" : "/L="+this.city); subj = subj + ((this.organization == "") ? "" : "/O="+this.organization); subj = subj + ((this.unit == "") ? "" : "/OU="+this.unit); subj = subj + "\""; string args = String.Format("req -days 3650 -newkey rsa:{1} -keyout {2} -out {3} -subj {4}", this.passphrase, this.keysize, this.keyfile, this.requestfile, subj); args = args + String.Format(" -passout pass:{0}", this.passphrase); if (this.config != "") { args = args + String.Format(" -config \"{0}\"", this.config); } #if DEBUG StreamWriter SW; SW = File.CreateText("c:\\Temp\\openssl.log"); SW.WriteLine(this.path + " " + this.openssl + " " + args); SW.Close(); #endif Console.WriteLine(this.openssl + " " + args); Process process = new Process(); process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardOutput = false; process.StartInfo.RedirectStandardError = true; process.StartInfo.RedirectStandardInput = true; process.StartInfo.CreateNoWindow = true; process.StartInfo.WorkingDirectory = this.path; process.StartInfo.FileName = this.openssl; process.StartInfo.Arguments = args; process.EnableRaisingEvents = true; process.Start(); process.WaitForExit(); } #endregion #region Public properties public string KeySize { get { return this.keysize; } set { this.keysize = value; } } public string Name { get { return this.name; } set { this.name = value; } } public string EMail { get { return this.email; } set { this.email = value; } } public string Country { get { return this.country; } set { this.country = value; } } public string State { get { return this.state; } set { this.state = value; } } public string City { get { return this.city; } set { this.city = value; } } public string Organization { get { return this.organization; } set { this.organization = value; } } public string Unit { get { return this.unit; } set { this.unit = value; } } public string Passphrase { get { return this.passphrase; } set { this.passphrase = value; } } public string Path { get { return this.path; } set { this.path = value; } } public string FilePrefix { get { return this.fileprefix; } set { this.fileprefix = value; } } public string KeyFile { get { return this.path + System.IO.Path.DirectorySeparatorChar.ToString() + this.keyfile; } } public string RequestFile { get { return this.path + System.IO.Path.DirectorySeparatorChar.ToString() + this.requestfile; } } #endregion } }