Unverified Commit 150d7595 authored by mohammadlachgar's avatar mohammadlachgar Committed by GitHub

Change only_load_rootCert to overwriteRootCert

//Methode 1
//------------
 proxyServer = new ProxyServer();
//proxyServer.Password_rootCert = "PfxPassword";
 proxyServer.TrustRootCertificate = true;
.....
.....
 proxyServer.Start(); 
//The provided root certificate will be stored in proxy dll directory

//=====================
//Methode 2
//----------
 proxyServer = new ProxyServer();
 proxyServer.TrustRootCertificate = true;

//Will not (load or create) certificate Before call function CreateTrustedRootCertificate
proxyServer.CertificateManager.SetInfo_LoadRootCertificate(@"C:\NameFolder\rootCert.pfx", "PfxPassword");
.....
.....
 proxyServer.Start(); 
//The provided root certificate will be stored in "C:\NameFolder\rootCert.pfx"


//=====================
//Methode 3
//------------
 proxyServer = new ProxyServer();
 proxyServer.TrustRootCertificate = true;

//note : load now "C:\NameFolder\rootCert.pfx" (if existed)
proxyServer.CertificateManager.LoadRootCertificate(@"C:\NameFolder\rootCert.pfx", "PfxPassword");
.....
.....
 proxyServer.Start(); 
//if doesn't exist file, or password is incorect and (overwriteRootCert=true) ====> create new pfx file 
//The provided root certificate will be stored in "C:\NameFolder\rootCert.pfx"
parent 21c6309f
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
...@@ -28,8 +28,8 @@ namespace Titanium.Web.Proxy.Network ...@@ -28,8 +28,8 @@ 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
...@@ -68,7 +68,7 @@ namespace Titanium.Web.Proxy.Network ...@@ -68,7 +68,7 @@ namespace Titanium.Web.Proxy.Network
set set
{ {
trustRootCertificate = value; trustRootCertificate = value;
} }
} }
...@@ -79,7 +79,7 @@ namespace Titanium.Web.Proxy.Network ...@@ -79,7 +79,7 @@ namespace Titanium.Web.Proxy.Network
get => saveCertificate; get => saveCertificate;
set set
{ {
saveCertificate = value; saveCertificate = value;
} }
} }
...@@ -100,7 +100,7 @@ namespace Titanium.Web.Proxy.Network ...@@ -100,7 +100,7 @@ namespace Titanium.Web.Proxy.Network
private X509Certificate2 rootCertificate; private X509Certificate2 rootCertificate;
public bool only_load_rootCert { get; set; } private bool overwriteRootCert;
/// <summary> /// <summary>
/// Cache dictionary /// Cache dictionary
...@@ -109,7 +109,7 @@ namespace Titanium.Web.Proxy.Network ...@@ -109,7 +109,7 @@ namespace Titanium.Web.Proxy.Network
private readonly Action<Exception> exceptionFunc; private readonly Action<Exception> exceptionFunc;
internal string Issuer internal string Issuer
{ {
get => issuer ?? defaultRootCertificateIssuer; get => issuer ?? defaultRootCertificateIssuer;
...@@ -185,7 +185,7 @@ namespace Titanium.Web.Proxy.Network ...@@ -185,7 +185,7 @@ namespace Titanium.Web.Proxy.Network
string path = Path.GetDirectoryName(assemblyLocation); string path = Path.GetDirectoryName(assemblyLocation);
if (null == path) if (null == path)
throw new NullReferenceException(); throw new NullReferenceException();
return path; return path;
} }
...@@ -202,76 +202,79 @@ namespace Titanium.Web.Proxy.Network ...@@ -202,76 +202,79 @@ namespace Titanium.Web.Proxy.Network
private string GetRootCertificatePath() private string GetRootCertificatePath()
{ {
string path = GetRootCertificateDirectory(); string path = GetRootCertificateDirectory();
string fileName = Path.Combine(path, "rootCert.pfx"); //string fileName = Path.Combine(path, "rootCert.pfx");
return fileName;
}
public X509Certificate2 LoadRootCertificate()
{
string fileName = filename_loadx; string fileName = filename_loadx;
if (/*(fileName.Trim().Length<=0)||*/ (fileName == string.Empty)) if ((fileName == string.Empty))
{ {
fileName = GetRootCertificatePath(); fileName = Path.Combine(path, "rootCert.pfx");
passwordx = password_rootCert; /*string.Empty;*/ passwordx = password_rootCert; /*string.Empty;*/
StorageFlag = X509KeyStorageFlags.Exportable; StorageFlag = X509KeyStorageFlags.Exportable;
this.overwriteRootCert = true;
} }
return fileName;
}
public X509Certificate2 LoadRootCertificate()
{
string fileName = GetRootCertificatePath();
if (!File.Exists(fileName)) if (!File.Exists(fileName))
{
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, passwordx, 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 /// </summary>
/// </summary> /// <param name="persistToFile">if set to <c>true</c> try to load/save the certificate from rootCert.pfx.</param>
/// <param name="persistToFile">if set to <c>true</c> try to load/save the certificate from rootCert.pfx.</param> /// <returns>
/// <returns> /// 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 (only_load_rootCert == true) if (overwriteRootCert == false)
{ {
return false; return false;
} }
} }
try try
{ {
RootCertificate = CreateCertificate(RootCertificateName, true); RootCertificate = CreateCertificate(RootCertificateName, true);
} }
catch (Exception e) catch (Exception e)
{ {
...@@ -282,25 +285,20 @@ namespace Titanium.Web.Proxy.Network ...@@ -282,25 +285,20 @@ namespace Titanium.Web.Proxy.Network
{ {
try try
{ {
try try {
{ Directory.Delete(GetCertPath(), true);
Directory.Delete(GetCertPath(), true); } catch { }
} string fileName = GetRootCertificatePath();
catch { } File.WriteAllBytes(fileName, RootCertificate.Export(X509ContentType.Pkcs12, password_rootCert));
string fileName = GetRootCertificatePath();
File.WriteAllBytes(fileName, RootCertificate.Export(X509ContentType.Pkcs12, password_rootCert));
} }
catch (Exception e) catch (Exception e)
{ {
exceptionFunc(e); exceptionFunc(e);
} }
} }
return RootCertificate != null; return RootCertificate != null;
} }
...@@ -308,43 +306,48 @@ namespace Titanium.Web.Proxy.Network ...@@ -308,43 +306,48 @@ namespace Titanium.Web.Proxy.Network
/// <summary> /// <summary>
/// Attempts to Manually load a RootCertificate /// Manually load a RootCertificate
/// set password and path_fileCertificate /// set password and path_fileRootCert
/// </summary> /// </summary>
/// <param name="fileName"></param>
/// <param name="password"></param>
/// <param name="overwriteRootCert"></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 passwordx, X509KeyStorageFlags StorageFlag = X509KeyStorageFlags.Exportable) public void SetInfo_LoadRootCertificate(string fileName, string password, bool overwriteRootCert = true, X509KeyStorageFlags StorageFlag = X509KeyStorageFlags.Exportable)
{ {
filename_loadx = fileName; filename_loadx = fileName;
this.passwordx = passwordx; this.passwordx = password;
this.password_rootCert = passwordx;
this.StorageFlag = StorageFlag; this.StorageFlag = StorageFlag;
only_load_rootCert = true; this.overwriteRootCert = overwriteRootCert;
} }
/// <summary> /// <summary>
/// Attempts to Manually load a RootCertificate /// Manually load a RootCertificate
/// </summary> /// </summary>
/// <param name="fileName"></param>
/// <param name="password"></param>
/// <param name="overwriteRootCert"></param>
/// <param name="StorageFlag"></param>
/// <returns> /// <returns>
/// true if succeeded, else false /// true if succeeded, else false
/// </returns> /// </returns>
public bool LoadRootCertificate(string fileName, string passwordx, X509KeyStorageFlags StorageFlag = X509KeyStorageFlags.Exportable) public bool LoadRootCertificate(string fileName, string password, bool overwriteRootCert = true, X509KeyStorageFlags StorageFlag = X509KeyStorageFlags.Exportable)
{ {
filename_loadx = fileName; SetInfo_LoadRootCertificate(fileName, password, overwriteRootCert, StorageFlag);
this.passwordx = passwordx; RootCertificate = LoadRootCertificate();
this.StorageFlag = StorageFlag;
only_load_rootCert = true;
RootCertificate = LoadRootCertificate();
return (RootCertificate != null); return (RootCertificate != null);
} }
private string passwordx = string.Empty; private string passwordx = string.Empty;
private string filename_loadx = ""; private string filename_loadx = "";
X509KeyStorageFlags StorageFlag = X509KeyStorageFlags.Exportable; X509KeyStorageFlags StorageFlag = X509KeyStorageFlags.Exportable;
/// <summary> /// <summary>
...@@ -359,7 +362,7 @@ namespace Titanium.Web.Proxy.Network ...@@ -359,7 +362,7 @@ namespace Titanium.Web.Proxy.Network
TrustRootCertificate(StoreLocation.LocalMachine); TrustRootCertificate(StoreLocation.LocalMachine);
} }
/// <summary> /// <summary>
...@@ -380,7 +383,7 @@ namespace Titanium.Web.Proxy.Network ...@@ -380,7 +383,7 @@ namespace Titanium.Web.Proxy.Network
var info = new ProcessStartInfo var info = new ProcessStartInfo
{ {
FileName = "certutil.exe", FileName = "certutil.exe",
Arguments = "-importPFX -p \"" + password_rootCert + "\" -f \"" + fileName + "\"", Arguments = "-importPFX -p \""+ password_rootCert + "\" -f \"" + fileName + "\"",
CreateNoWindow = true, CreateNoWindow = true,
UseShellExecute = true, UseShellExecute = true,
Verb = "runas", Verb = "runas",
...@@ -533,42 +536,40 @@ namespace Titanium.Web.Proxy.Network ...@@ -533,42 +536,40 @@ namespace Titanium.Web.Proxy.Network
// { // {
// CreateTrustedRootCertificate(); // CreateTrustedRootCertificate();
// } // }
if ((isRootCertificate == false) && (saveCertificate == true)) if ((isRootCertificate == false)&& (saveCertificate == 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.ToLower(), @"^" + "CN".ToLower() + @"\s*=\s*", "");
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))
...@@ -588,7 +589,7 @@ namespace Titanium.Web.Proxy.Network ...@@ -588,7 +589,7 @@ namespace Titanium.Web.Proxy.Network
return cached.Certificate; return cached.Certificate;
} }
} }
} }
return certificate; return certificate;
} }
...@@ -649,7 +650,7 @@ namespace Titanium.Web.Proxy.Network ...@@ -649,7 +650,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 +662,7 @@ namespace Titanium.Web.Proxy.Network ...@@ -661,7 +662,7 @@ namespace Titanium.Web.Proxy.Network
} }
} }
/// <summary> /// <summary>
/// Remove the Root Certificate trust /// Remove the Root Certificate trust
/// </summary> /// </summary>
......
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