Commit 5d5b32e9 authored by justcoding121's avatar justcoding121

experiment test on build server; update certificate trust logic

parent fabfe979
...@@ -44,7 +44,7 @@ namespace Titanium.Web.Proxy.UnitTests ...@@ -44,7 +44,7 @@ namespace Titanium.Web.Proxy.UnitTests
//uncomment this to compare WinCert maker performance with BC (BC takes more time for same test above) //uncomment this to compare WinCert maker performance with BC (BC takes more time for same test above)
//cannot run this test in build server since trusting the certificate won't happen successfully //cannot run this test in build server since trusting the certificate won't happen successfully
//[TestMethod] [TestMethod]
public async Task Simple_Create_Win_Certificate_Test() public async Task Simple_Create_Win_Certificate_Test()
{ {
var tasks = new List<Task>(); var tasks = new List<Task>();
...@@ -57,7 +57,6 @@ namespace Titanium.Web.Proxy.UnitTests ...@@ -57,7 +57,6 @@ namespace Titanium.Web.Proxy.UnitTests
mgr.CertificateEngine = CertificateEngine.DefaultWindows; mgr.CertificateEngine = CertificateEngine.DefaultWindows;
mgr.CreateRootCertificate(true); mgr.CreateRootCertificate(true);
mgr.TrustRootCertificate(); mgr.TrustRootCertificate();
//mgr.TrustRootCertificateAsAdmin();
mgr.ClearIdleCertificates(); mgr.ClearIdleCertificates();
for (int i = 0; i < 5; i++) for (int i = 0; i < 5; i++)
...@@ -72,7 +71,7 @@ namespace Titanium.Web.Proxy.UnitTests ...@@ -72,7 +71,7 @@ namespace Titanium.Web.Proxy.UnitTests
} }
await Task.WhenAll(tasks.ToArray()); await Task.WhenAll(tasks.ToArray());
mgr.RemoveTrustedRootCertificate();
mgr.StopClearIdleCertificates(); mgr.StopClearIdleCertificates();
} }
} }
......
...@@ -188,7 +188,7 @@ namespace Titanium.Web.Proxy.Network ...@@ -188,7 +188,7 @@ namespace Titanium.Web.Proxy.Network
internal CertificateManager(Action<Exception> exceptionFunc) internal CertificateManager(Action<Exception> exceptionFunc)
{ {
this.exceptionFunc = exceptionFunc; this.exceptionFunc = exceptionFunc;
if(RunTime.IsWindows) if (RunTime.IsWindows)
{ {
//this is faster in Windows based on tests (see unit test project CertificateManagerTests.cs) //this is faster in Windows based on tests (see unit test project CertificateManagerTests.cs)
CertificateEngine = CertificateEngine.DefaultWindows; CertificateEngine = CertificateEngine.DefaultWindows;
...@@ -282,7 +282,7 @@ namespace Titanium.Web.Proxy.Network ...@@ -282,7 +282,7 @@ namespace Titanium.Web.Proxy.Network
/// </summary> /// </summary>
/// <param name="storeLocation"></param> /// <param name="storeLocation"></param>
/// <returns></returns> /// <returns></returns>
private void TrustRootCertificate(StoreLocation storeLocation) private void TrustRootCertificate(StoreName storeName, StoreLocation storeLocation)
{ {
if (RootCertificate == null) if (RootCertificate == null)
{ {
...@@ -293,29 +293,25 @@ namespace Titanium.Web.Proxy.Network ...@@ -293,29 +293,25 @@ namespace Titanium.Web.Proxy.Network
return; return;
} }
var x509RootStore = new X509Store(StoreName.Root, storeLocation); var x509store = new X509Store(storeName, storeLocation);
var x509PersonalStore = new X509Store(StoreName.My, storeLocation);
//TODO //TODO
//also it should do not duplicate if certificate already exists //also it should do not duplicate if certificate already exists
try try
{ {
x509RootStore.Open(OpenFlags.ReadWrite); x509store.Open(OpenFlags.ReadWrite);
x509PersonalStore.Open(OpenFlags.ReadWrite); x509store.Add(RootCertificate);
x509RootStore.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 {storeName}\\{storeLocation} store location. You may need admin rights.", e));
} }
finally finally
{ {
x509RootStore.Close(); x509store.Close();
x509PersonalStore.Close();
} }
} }
...@@ -324,7 +320,7 @@ namespace Titanium.Web.Proxy.Network ...@@ -324,7 +320,7 @@ namespace Titanium.Web.Proxy.Network
/// </summary> /// </summary>
/// <param name="storeLocation"></param> /// <param name="storeLocation"></param>
/// <returns></returns> /// <returns></returns>
private void RemoveTrustedRootCertificate(StoreLocation storeLocation) private void RemoveTrustedRootCertificate(StoreName storeName, StoreLocation storeLocation)
{ {
if (RootCertificate == null) if (RootCertificate == null)
{ {
...@@ -335,16 +331,13 @@ namespace Titanium.Web.Proxy.Network ...@@ -335,16 +331,13 @@ namespace Titanium.Web.Proxy.Network
return; return;
} }
var x509RootStore = new X509Store(StoreName.Root, storeLocation); var x509Store = new X509Store(storeName, storeLocation);
var x509PersonalStore = new X509Store(StoreName.My, storeLocation);
try try
{ {
x509RootStore.Open(OpenFlags.ReadWrite); x509Store.Open(OpenFlags.ReadWrite);
x509PersonalStore.Open(OpenFlags.ReadWrite);
x509RootStore.Remove(RootCertificate); x509Store.Remove(RootCertificate);
x509PersonalStore.Remove(RootCertificate);
} }
catch (Exception e) catch (Exception e)
{ {
...@@ -354,8 +347,7 @@ namespace Titanium.Web.Proxy.Network ...@@ -354,8 +347,7 @@ namespace Titanium.Web.Proxy.Network
} }
finally finally
{ {
x509RootStore.Close(); x509Store.Close();
x509PersonalStore.Close();
} }
} }
...@@ -632,10 +624,13 @@ namespace Titanium.Web.Proxy.Network ...@@ -632,10 +624,13 @@ namespace Titanium.Web.Proxy.Network
public void TrustRootCertificate() public void TrustRootCertificate()
{ {
//current user //current user
TrustRootCertificate(StoreLocation.CurrentUser); TrustRootCertificate(StoreName.My, StoreLocation.CurrentUser);
//current system //current system
TrustRootCertificate(StoreLocation.LocalMachine); TrustRootCertificate(StoreName.My, StoreLocation.LocalMachine);
//this adds to both currentUser\Root & currentMachine\Root
TrustRootCertificate(StoreName.Root, StoreLocation.LocalMachine);
} }
/// <summary> /// <summary>
...@@ -650,20 +645,22 @@ namespace Titanium.Web.Proxy.Network ...@@ -650,20 +645,22 @@ namespace Titanium.Web.Proxy.Network
return false; return false;
} }
string fileName = Path.GetTempFileName(); string pfxFileName = Path.GetTempFileName();
File.WriteAllBytes(fileName, RootCertificate.Export(X509ContentType.Pkcs12, PfxPassword)); File.WriteAllBytes(pfxFileName, RootCertificate.Export(X509ContentType.Pkcs12, PfxPassword));
//TODO //currentUser\Personal
//need to figure out why this command do not add TrustRootCertificate(StoreName.My, StoreLocation.CurrentUser);
//certificate to currentuser\personal store
var info = new ProcessStartInfo //currentUser\Root, currentMachine\Personal & currentMachine\Root
var info = new ProcessStartInfo()
{ {
FileName = "certutil.exe", FileName = "certutil.exe",
Arguments = "-importPFX -p \"" + PfxPassword + "\" -f \"" + fileName + "\"", Arguments = "-importPFX -p \"" + PfxPassword + "\" -f \"" + pfxFileName + "\"",
CreateNoWindow = true, CreateNoWindow = true,
UseShellExecute = true, UseShellExecute = true,
Verb = "runas", Verb = "runas",
ErrorDialog = false, ErrorDialog = false,
WindowStyle = ProcessWindowStyle.Hidden
}; };
try try
...@@ -675,8 +672,8 @@ namespace Titanium.Web.Proxy.Network ...@@ -675,8 +672,8 @@ namespace Titanium.Web.Proxy.Network
} }
process.WaitForExit(); process.WaitForExit();
File.Delete(pfxFileName);
File.Delete(fileName);
} }
catch catch
{ {
...@@ -692,10 +689,13 @@ namespace Titanium.Web.Proxy.Network ...@@ -692,10 +689,13 @@ namespace Titanium.Web.Proxy.Network
public void RemoveTrustedRootCertificate() public void RemoveTrustedRootCertificate()
{ {
//current user //current user
RemoveTrustedRootCertificate(StoreLocation.CurrentUser); RemoveTrustedRootCertificate(StoreName.My, StoreLocation.CurrentUser);
//current system //current system
RemoveTrustedRootCertificate(StoreLocation.LocalMachine); RemoveTrustedRootCertificate(StoreName.My, StoreLocation.LocalMachine);
//this removes from both currentUser\Root & currentMachine\Root
RemoveTrustedRootCertificate(StoreName.Root, StoreLocation.LocalMachine);
} }
/// <summary> /// <summary>
...@@ -710,32 +710,54 @@ namespace Titanium.Web.Proxy.Network ...@@ -710,32 +710,54 @@ namespace Titanium.Web.Proxy.Network
return false; return false;
} }
var info = new ProcessStartInfo //currentUser\Personal
RemoveTrustedRootCertificate(StoreName.My, StoreLocation.CurrentUser);
var infos = new List<ProcessStartInfo>()
{ {
//currentMachine\Personal
new ProcessStartInfo(){
FileName = "certutil.exe",
Arguments = "-delstore My \"" + RootCertificateName + "\"",
CreateNoWindow = true,
UseShellExecute = true,
Verb = "runas",
ErrorDialog = false,
WindowStyle = ProcessWindowStyle.Hidden
},
//currentUser\Personal & currentMachine\Personal
new ProcessStartInfo(){
FileName = "certutil.exe", FileName = "certutil.exe",
Arguments = "-delstore Root \"" + RootCertificateName + "\"", Arguments = "-delstore Root \"" + RootCertificateName + "\"",
CreateNoWindow = true, CreateNoWindow = true,
UseShellExecute = true, UseShellExecute = true,
Verb = "runas", Verb = "runas",
ErrorDialog = false, ErrorDialog = false,
WindowStyle = ProcessWindowStyle.Hidden
}
}; };
var success = true;
try try
{ {
var process = Process.Start(info); foreach (var info in infos)
if (process == null)
{ {
return false; var process = Process.Start(info);
}
process.WaitForExit(); if (process == null)
{
success = false;
}
process.WaitForExit();
}
} }
catch catch
{ {
return false; success = false;
} }
return true; return success;
} }
/// <summary> /// <summary>
...@@ -754,7 +776,7 @@ namespace Titanium.Web.Proxy.Network ...@@ -754,7 +776,7 @@ namespace Titanium.Web.Proxy.Network
return FindRootCertificate(StoreLocation.LocalMachine); return FindRootCertificate(StoreLocation.LocalMachine);
} }
/// <summary> /// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
......
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