Unverified Commit cc18c33e authored by mohammadlachgar's avatar mohammadlachgar Committed by GitHub

Changed the naming conventions ...

Changed the naming conventions
Changed some properties to "Auto-Implemented Properties"
Add some properties
parent 391acf96
...@@ -28,9 +28,6 @@ namespace Titanium.Web.Proxy.Network ...@@ -28,9 +28,6 @@ namespace Titanium.Web.Proxy.Network
BouncyCastle = 1 BouncyCastle = 1
} }
/// <summary> /// <summary>
/// A class to manage SSL certificates used by this proxy server /// A class to manage SSL certificates used by this proxy server
/// </summary> /// </summary>
...@@ -60,30 +57,6 @@ namespace Titanium.Web.Proxy.Network ...@@ -60,30 +57,6 @@ namespace Titanium.Web.Proxy.Network
} }
} }
private bool trustRootCertificate;
public bool TrustrootCertificate
{
get => trustRootCertificate;
set
{
trustRootCertificate = value;
}
}
private bool saveCertificate;
public bool SaveCertificate
{
get => saveCertificate;
set
{
saveCertificate = value;
}
}
private const string defaultRootCertificateIssuer = "Titanium"; private const string defaultRootCertificateIssuer = "Titanium";
private const string defaultRootRootCertificateName = "Titanium Root Certificate Authority"; private const string defaultRootRootCertificateName = "Titanium Root Certificate Authority";
...@@ -96,11 +69,23 @@ namespace Titanium.Web.Proxy.Network ...@@ -96,11 +69,23 @@ namespace Titanium.Web.Proxy.Network
private string rootCertificateName; private string rootCertificateName;
private bool PFXfileExists = false;
private bool clearCertificates { get; set; } private bool clearCertificates { get; set; }
private X509Certificate2 rootCertificate; private X509Certificate2 rootCertificate;
private bool overwriteRootCert; internal bool trustRootCertificate { get; set; } = false;
internal bool overwritePFXfile { get; set; } = true;
internal string passwordpfx { get; set; } = string.Empty;
internal string namePFXfile { get; set; } = string.Empty;
internal X509KeyStorageFlags storageFlag { get; set; } = X509KeyStorageFlags.Exportable;
internal bool saveFakeCertificates { get; set; }=false;
/// <summary> /// <summary>
/// Cache dictionary /// Cache dictionary
...@@ -108,7 +93,6 @@ namespace Titanium.Web.Proxy.Network ...@@ -108,7 +93,6 @@ namespace Titanium.Web.Proxy.Network
private readonly IDictionary<string, CachedCertificate> certificateCache; private readonly IDictionary<string, CachedCertificate> certificateCache;
private readonly Action<Exception> exceptionFunc; private readonly Action<Exception> exceptionFunc;
internal string Issuer internal string Issuer
{ {
...@@ -140,24 +124,11 @@ namespace Titanium.Web.Proxy.Network ...@@ -140,24 +124,11 @@ namespace Titanium.Web.Proxy.Network
} }
} }
private string password_rootCert = string.Empty;
public string Password_rootCert
{
get => password_rootCert;
set
{
password_rootCert = value;
}
}
/// <summary> /// <summary>
/// Is the root certificate used by this proxy is valid? /// Is the root certificate used by this proxy is valid?
/// </summary> /// </summary>
internal bool CertValidated => RootCertificate != null; internal bool CertValidated => RootCertificate != null;
internal CertificateManager(Action<Exception> exceptionFunc) internal CertificateManager(Action<Exception> exceptionFunc)
{ {
this.exceptionFunc = exceptionFunc; this.exceptionFunc = exceptionFunc;
...@@ -172,7 +143,6 @@ namespace Titanium.Web.Proxy.Network ...@@ -172,7 +143,6 @@ namespace Titanium.Web.Proxy.Network
rootCertificate = null; rootCertificate = null;
} }
private string GetRootCertificateDirectory() private string GetRootCertificateDirectory()
{ {
string assemblyLocation = Assembly.GetExecutingAssembly().Location; string assemblyLocation = Assembly.GetExecutingAssembly().Location;
...@@ -189,7 +159,6 @@ namespace Titanium.Web.Proxy.Network ...@@ -189,7 +159,6 @@ namespace Titanium.Web.Proxy.Network
return path; return path;
} }
private string GetCertPath() private string GetCertPath()
{ {
string path = GetRootCertificateDirectory(); string path = GetRootCertificateDirectory();
...@@ -203,14 +172,11 @@ namespace Titanium.Web.Proxy.Network ...@@ -203,14 +172,11 @@ namespace Titanium.Web.Proxy.Network
{ {
string path = GetRootCertificateDirectory(); string path = GetRootCertificateDirectory();
//string fileName = Path.Combine(path, "rootCert.pfx"); string fileName = namePFXfile;
string fileName = filename_loadx;
if ((fileName == string.Empty)) if ((fileName == string.Empty))
{ {
fileName = Path.Combine(path, "rootCert.pfx"); fileName = Path.Combine(path, "rootCert.pfx");
passwordx = password_rootCert; /*string.Empty;*/ storageFlag = X509KeyStorageFlags.Exportable;
StorageFlag = X509KeyStorageFlags.Exportable;
this.overwriteRootCert = true;
} }
return fileName; return fileName;
} }
...@@ -218,29 +184,23 @@ namespace Titanium.Web.Proxy.Network ...@@ -218,29 +184,23 @@ namespace Titanium.Web.Proxy.Network
public X509Certificate2 LoadRootCertificate() public X509Certificate2 LoadRootCertificate()
{ {
string fileName = GetRootCertificatePath(); string fileName = GetRootCertificatePath();
this.PFXfileExists = File.Exists(fileName);
if (!File.Exists(fileName)) if (!this.PFXfileExists)
{ {
return null; return null;
} }
try try
{ {
//return new X509Certificate2(fileName, string.Empty, X509KeyStorageFlags.Exportable); //return new X509Certificate2(fileName, string.Empty, X509KeyStorageFlags.Exportable);
return new X509Certificate2(fileName, passwordx, StorageFlag); return new X509Certificate2(fileName, passwordpfx, storageFlag);
} }
catch (Exception e) catch (Exception e)
{ {
exceptionFunc(e); exceptionFunc(e);
return null; return null;
} }
} }
/// <summary> /// <summary>
/// Attempts to create a RootCertificate /// Attempts to create a RootCertificate
...@@ -250,31 +210,29 @@ namespace Titanium.Web.Proxy.Network ...@@ -250,31 +210,29 @@ namespace Titanium.Web.Proxy.Network
/// true if succeeded, else false /// true if succeeded, else false
/// </returns> /// </returns>
public bool CreateTrustedRootCertificate(bool persistToFile = true) public bool CreateTrustedRootCertificate(bool persistToFile = true)
{ {
if (persistToFile && RootCertificate == null) if (persistToFile && RootCertificate == null)
{ {
RootCertificate = LoadRootCertificate(); RootCertificate = LoadRootCertificate();
} }
if (RootCertificate != null) if (RootCertificate != null)
{ {
return true; return true;
} }
else else
{ {
if (overwriteRootCert == false) if ((overwritePFXfile == false) && (PFXfileExists == true))
{ {
return false; return false;
} }
} }
try try
{ {
RootCertificate = CreateCertificate(RootCertificateName, true); RootCertificate = CreateCertificate(RootCertificateName, true);
} }
catch (Exception e) catch (Exception e)
{ {
...@@ -289,66 +247,37 @@ namespace Titanium.Web.Proxy.Network ...@@ -289,66 +247,37 @@ namespace Titanium.Web.Proxy.Network
Directory.Delete(GetCertPath(), true); Directory.Delete(GetCertPath(), true);
} catch { } } catch { }
string fileName = GetRootCertificatePath(); string fileName = GetRootCertificatePath();
File.WriteAllBytes(fileName, RootCertificate.Export(X509ContentType.Pkcs12, password_rootCert)); File.WriteAllBytes(fileName, RootCertificate.Export(X509ContentType.Pkcs12, passwordpfx));
} }
catch (Exception e) catch (Exception e)
{ {
exceptionFunc(e); exceptionFunc(e);
} }
} }
return RootCertificate != null; return RootCertificate != null;
} }
/// <summary> /// <summary>
/// Manually load a RootCertificate /// Manually load a Root certificate file(.pfx file)
/// set password and path_fileRootCert
/// </summary> /// </summary>
/// <param name="fileName"></param> /// <param name="namePFXfile">Set the name(path) of the .pfx file. If it is string.Empty Root certificate file will be named as "rootCert.pfx" (and will be saved in proxy dll directory)</param>
/// <param name="password"></param> /// <param name="password">Set a password for the .pfx file</param>
/// <param name="overwriteRootCert"></param> /// <param name="overwritePFXfile">true : replace an existing .pfx file if password is incorect or if RootCertificate==null</param>
/// <param name="StorageFlag"></param> /// <param name="StorageFlag"></param>
/// <returns> /// <returns>
/// true if succeeded, else false /// true if succeeded, else false
/// </returns> /// </returns>
public void SetInfo_LoadRootCertificate(string fileName, string password, bool overwriteRootCert = true, X509KeyStorageFlags StorageFlag = X509KeyStorageFlags.Exportable) public bool LoadRootCertificate(string namePFXfile, string password, bool overwritePFXfile = true, X509KeyStorageFlags StorageFlag = X509KeyStorageFlags.Exportable)
{ {
filename_loadx = fileName; this.namePFXfile = namePFXfile;
this.passwordx = password; this.passwordpfx = password;
this.password_rootCert = passwordx; this.overwritePFXfile = overwritePFXfile;
this.StorageFlag = StorageFlag; this.storageFlag = StorageFlag;
this.overwriteRootCert = overwriteRootCert;
}
/// <summary> RootCertificate = LoadRootCertificate();
/// Manually load a RootCertificate
/// </summary>
/// <param name="fileName"></param>
/// <param name="password"></param>
/// <param name="overwriteRootCert"></param>
/// <param name="StorageFlag"></param>
/// <returns>
/// true if succeeded, else false
/// </returns>
public bool LoadRootCertificate(string fileName, string password, bool overwriteRootCert = true, X509KeyStorageFlags StorageFlag = X509KeyStorageFlags.Exportable)
{
SetInfo_LoadRootCertificate(fileName, password, overwriteRootCert, StorageFlag);
RootCertificate = LoadRootCertificate();
return (RootCertificate != null); return (RootCertificate != null);
} }
private string passwordx = string.Empty;
private string filename_loadx = "";
X509KeyStorageFlags StorageFlag = X509KeyStorageFlags.Exportable;
/// <summary> /// <summary>
/// Trusts the root certificate. /// Trusts the root certificate.
...@@ -362,11 +291,8 @@ namespace Titanium.Web.Proxy.Network ...@@ -362,11 +291,8 @@ namespace Titanium.Web.Proxy.Network
TrustRootCertificate(StoreLocation.LocalMachine); TrustRootCertificate(StoreLocation.LocalMachine);
} }
/// <summary> /// <summary>
/// Puts the certificate to the local machine's certificate store. /// Puts the certificate to the local machine's certificate store.
/// Needs elevated permission. Works only on Windows. /// Needs elevated permission. Works only on Windows.
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
...@@ -378,12 +304,12 @@ namespace Titanium.Web.Proxy.Network ...@@ -378,12 +304,12 @@ namespace Titanium.Web.Proxy.Network
} }
string fileName = Path.GetTempFileName(); string fileName = Path.GetTempFileName();
File.WriteAllBytes(fileName, RootCertificate.Export(X509ContentType.Pkcs12, password_rootCert)); File.WriteAllBytes(fileName, RootCertificate.Export(X509ContentType.Pkcs12, passwordpfx));
var info = new ProcessStartInfo var info = new ProcessStartInfo
{ {
FileName = "certutil.exe", FileName = "certutil.exe",
Arguments = "-importPFX -p \""+ password_rootCert + "\" -f \"" + fileName + "\"", Arguments = "-importPFX -p \""+ passwordpfx + "\" -f \"" + fileName + "\"",
CreateNoWindow = true, CreateNoWindow = true,
UseShellExecute = true, UseShellExecute = true,
Verb = "runas", Verb = "runas",
...@@ -423,7 +349,7 @@ namespace Titanium.Web.Proxy.Network ...@@ -423,7 +349,7 @@ namespace Titanium.Web.Proxy.Network
} }
/// <summary> /// <summary>
/// Removes the trusted certificates from the local machine's certificate store. /// Removes the trusted certificates from the local machine's certificate store.
/// Needs elevated permission. Works only on Windows. /// Needs elevated permission. Works only on Windows.
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
...@@ -498,7 +424,6 @@ namespace Titanium.Web.Proxy.Network ...@@ -498,7 +424,6 @@ namespace Titanium.Web.Proxy.Network
} }
} }
private X509Certificate2 makeCertificate(string certificateName, bool isRootCertificate) private X509Certificate2 makeCertificate(string certificateName, bool isRootCertificate)
{ {
if (!isRootCertificate && RootCertificate == null) if (!isRootCertificate && RootCertificate == null)
...@@ -508,7 +433,6 @@ namespace Titanium.Web.Proxy.Network ...@@ -508,7 +433,6 @@ namespace Titanium.Web.Proxy.Network
return certEngine.MakeCertificate(certificateName, isRootCertificate, RootCertificate); return certEngine.MakeCertificate(certificateName, isRootCertificate, RootCertificate);
} }
/// <summary> /// <summary>
/// Create an SSL certificate /// Create an SSL certificate
/// </summary> /// </summary>
...@@ -531,45 +455,40 @@ namespace Titanium.Web.Proxy.Network ...@@ -531,45 +455,40 @@ namespace Titanium.Web.Proxy.Network
{ {
try try
{ {
//if (!isRootCertificate && RootCertificate == null)
// {
// CreateTrustedRootCertificate();
// }
if ((isRootCertificate == false)&& (saveCertificate == true)) { if ((isRootCertificate == false) && (saveFakeCertificates == true)) {
string path = GetCertPath(); string path = GetCertPath();
string subjectName = System.Text.RegularExpressions.Regex.Replace(certificateName.ToLower(), @"^" + "CN".ToLower() + @"\s*=\s*", ""); string subjectName = System.Text.RegularExpressions.Regex.Replace(certificateName, @"^CN\s*=\s*", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
subjectName = subjectName.Replace("*", "$x$"); subjectName = subjectName.Replace("*", "$x$");
subjectName= Path.Combine(path, subjectName+ ".pfx"); subjectName= Path.Combine(path, subjectName+ ".pfx");
if (!File.Exists(subjectName)) if (!File.Exists(subjectName))
{ {
certificate = this.makeCertificate(certificateName, isRootCertificate); certificate = this.makeCertificate(certificateName, isRootCertificate);
File.WriteAllBytes(subjectName, certificate.Export(X509ContentType.Pkcs12)); File.WriteAllBytes(subjectName, certificate.Export(X509ContentType.Pkcs12));
} }
else else
{ {
try try
{ {
certificate = new X509Certificate2(subjectName, string.Empty, StorageFlag); certificate = new X509Certificate2(subjectName, string.Empty, storageFlag);
} }
catch/* (Exception e)*/ catch/* (Exception e)*/
{ {
certificate = this.makeCertificate(certificateName, isRootCertificate); certificate = this.makeCertificate(certificateName, isRootCertificate);
} }
} }
} }
else { else {
certificate = this.makeCertificate(certificateName, isRootCertificate); certificate = this.makeCertificate(certificateName, isRootCertificate);
} }
} }
catch (Exception e) catch (Exception e)
{ {
exceptionFunc(e); exceptionFunc(e);
} }
if (certificate != null && !certificateCache.ContainsKey(certificateName)) if (certificate != null && !certificateCache.ContainsKey(certificateName))
...@@ -650,7 +569,7 @@ namespace Titanium.Web.Proxy.Network ...@@ -650,7 +569,7 @@ namespace Titanium.Web.Proxy.Network
x509PersonalStore.Add(RootCertificate); x509PersonalStore.Add(RootCertificate);
} }
catch (Exception e) catch (Exception e)
{ {
exceptionFunc( exceptionFunc(
new Exception("Failed to make system trust root certificate " new Exception("Failed to make system trust root certificate "
+ $" for {storeLocation} store location. You may need admin rights.", e)); + $" for {storeLocation} store location. You may need admin rights.", e));
...@@ -661,7 +580,6 @@ namespace Titanium.Web.Proxy.Network ...@@ -661,7 +580,6 @@ namespace Titanium.Web.Proxy.Network
x509PersonalStore.Close(); x509PersonalStore.Close();
} }
} }
/// <summary> /// <summary>
/// Remove the Root Certificate trust /// Remove the Root Certificate trust
...@@ -676,7 +594,6 @@ namespace Titanium.Web.Proxy.Network ...@@ -676,7 +594,6 @@ namespace Titanium.Web.Proxy.Network
new Exception("Could not set root certificate" new Exception("Could not set root certificate"
+ " as system proxy since it is null or empty.")); + " as system proxy since it is null or empty."));
return; return;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment