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;
...@@ -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
...@@ -203,22 +203,27 @@ namespace Titanium.Web.Proxy.Network ...@@ -203,22 +203,27 @@ namespace Titanium.Web.Proxy.Network
{ {
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);
...@@ -259,7 +264,7 @@ namespace Titanium.Web.Proxy.Network ...@@ -259,7 +264,7 @@ namespace Titanium.Web.Proxy.Network
} }
else else
{ {
if (only_load_rootCert == true) if (overwriteRootCert == false)
{ {
return false; return false;
} }
...@@ -270,8 +275,6 @@ namespace Titanium.Web.Proxy.Network ...@@ -270,8 +275,6 @@ namespace Titanium.Web.Proxy.Network
try try
{ {
RootCertificate = CreateCertificate(RootCertificateName, true); RootCertificate = CreateCertificate(RootCertificateName, true);
} }
catch (Exception e) catch (Exception e)
{ {
...@@ -282,16 +285,11 @@ namespace Titanium.Web.Proxy.Network ...@@ -282,16 +285,11 @@ namespace Titanium.Web.Proxy.Network
{ {
try try
{ {
try try {
{
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, password_rootCert));
} }
catch (Exception e) catch (Exception e)
{ {
...@@ -308,33 +306,38 @@ namespace Titanium.Web.Proxy.Network ...@@ -308,33 +306,38 @@ 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;
this.StorageFlag = StorageFlag;
only_load_rootCert = true;
RootCertificate = LoadRootCertificate(); RootCertificate = LoadRootCertificate();
return (RootCertificate != null); return (RootCertificate != null);
...@@ -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",
...@@ -534,13 +537,12 @@ namespace Titanium.Web.Proxy.Network ...@@ -534,13 +537,12 @@ 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);
...@@ -558,8 +560,7 @@ namespace Titanium.Web.Proxy.Network ...@@ -558,8 +560,7 @@ namespace Titanium.Web.Proxy.Network
} }
} }
} }
else else {
{
certificate = this.makeCertificate(certificateName, isRootCertificate); certificate = this.makeCertificate(certificateName, isRootCertificate);
......
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