Commit 1ea7f3eb authored by justcoding121's avatar justcoding121 Committed by justcoding121

#180 Add cert to local machine store

parent 0779f209
...@@ -25,7 +25,7 @@ ...@@ -25,7 +25,7 @@
<Prefer32Bit>false</Prefer32Bit> <Prefer32Bit>false</Prefer32Bit>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType> <DebugType>none</DebugType>
<Optimize>true</Optimize> <Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath> <OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE;NET45</DefineConstants> <DefineConstants>TRACE;NET45</DefineConstants>
...@@ -33,6 +33,7 @@ ...@@ -33,6 +33,7 @@
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion> <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<Prefer32Bit>false</Prefer32Bit> <Prefer32Bit>false</Prefer32Bit>
<DebugSymbols>false</DebugSymbols>
</PropertyGroup> </PropertyGroup>
<PropertyGroup> <PropertyGroup>
<StartupObject /> <StartupObject />
......
...@@ -177,35 +177,45 @@ namespace Titanium.Web.Proxy.Network ...@@ -177,35 +177,45 @@ namespace Titanium.Web.Proxy.Network
} }
} }
internal bool TrustRootCertificate() /// <summary>
/// Make current machine trust the Root Certificate used by this proxy
/// </summary>
/// <param name="storeLocation"></param>
/// <param name="exceptionFunc"></param>
/// <returns></returns>
internal void TrustRootCertificate(StoreLocation storeLocation,
Action<Exception> exceptionFunc)
{ {
if (rootCertificate == null) if (rootCertificate == null)
{ {
return false; exceptionFunc(
new Exception("Could not set root certificate"
+ " as system proxy since it is null or empty."));
return;
} }
X509Store x509RootStore = new X509Store(StoreName.Root, storeLocation);
var x509PersonalStore = new X509Store(StoreName.My, storeLocation);
try try
{ {
X509Store x509RootStore = new X509Store(StoreName.Root, StoreLocation.CurrentUser);
var x509PersonalStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
x509RootStore.Open(OpenFlags.ReadWrite); x509RootStore.Open(OpenFlags.ReadWrite);
x509PersonalStore.Open(OpenFlags.ReadWrite); x509PersonalStore.Open(OpenFlags.ReadWrite);
try x509RootStore.Add(rootCertificate);
{ x509PersonalStore.Add(rootCertificate);
x509RootStore.Add(rootCertificate); }
x509PersonalStore.Add(rootCertificate); catch (Exception e)
} {
finally exceptionFunc(
{ new Exception("Failed to make system trust root certificate "
x509RootStore.Close(); + $" for {storeLocation} store location. You may need admin rights.", e));
x509PersonalStore.Close();
}
return true;
} }
catch finally
{ {
return false; x509RootStore.Close();
x509PersonalStore.Close();
} }
} }
......
...@@ -10,6 +10,7 @@ using Titanium.Web.Proxy.Network; ...@@ -10,6 +10,7 @@ using Titanium.Web.Proxy.Network;
using System.Linq; using System.Linq;
using System.Security.Authentication; using System.Security.Authentication;
using Titanium.Web.Proxy.Network.Tcp; using Titanium.Web.Proxy.Network.Tcp;
using System.Security.Cryptography.X509Certificates;
namespace Titanium.Web.Proxy namespace Titanium.Web.Proxy
{ {
...@@ -55,7 +56,8 @@ namespace Titanium.Web.Proxy ...@@ -55,7 +56,8 @@ namespace Titanium.Web.Proxy
private SystemProxyManager systemProxySettingsManager { get; } private SystemProxyManager systemProxySettingsManager { get; }
#if !DEBUG #if !DEBUG
private FireFoxProxySettingsManager firefoxProxySettingsManager { get; set; } private FireFoxProxySettingsManager firefoxProxySettingsManager
= new FireFoxProxySettingsManager();
#endif #endif
/// <summary> /// <summary>
...@@ -360,7 +362,13 @@ namespace Titanium.Web.Proxy ...@@ -360,7 +362,13 @@ namespace Titanium.Web.Proxy
if (TrustRootCertificate) if (TrustRootCertificate)
{ {
certificateCacheManager.TrustRootCertificate(); //current user
certificateCacheManager
.TrustRootCertificate(StoreLocation.CurrentUser, exceptionFunc);
//current system
certificateCacheManager
.TrustRootCertificate(StoreLocation.LocalMachine, exceptionFunc);
} }
if (ForwardToUpstreamGateway && GetCustomUpStreamHttpProxyFunc == null && GetCustomUpStreamHttpsProxyFunc == null) if (ForwardToUpstreamGateway && GetCustomUpStreamHttpProxyFunc == null && GetCustomUpStreamHttpsProxyFunc == null)
......
...@@ -34,6 +34,8 @@ ...@@ -34,6 +34,8 @@
<DefineConstants>NET45</DefineConstants> <DefineConstants>NET45</DefineConstants>
<Prefer32Bit>false</Prefer32Bit> <Prefer32Bit>false</Prefer32Bit>
<DocumentationFile>bin\Release\Titanium.Web.Proxy.XML</DocumentationFile> <DocumentationFile>bin\Release\Titanium.Web.Proxy.XML</DocumentationFile>
<DebugType>none</DebugType>
<DebugSymbols>false</DebugSymbols>
</PropertyGroup> </PropertyGroup>
<PropertyGroup> <PropertyGroup>
<SignAssembly>true</SignAssembly> <SignAssembly>true</SignAssembly>
......
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