Commit f5925c52 authored by Honfika's avatar Honfika

Allow to change the root certificate names (issuer, name) in...

Allow to change the root certificate names (issuer, name) in CertificateManager without  recreating the object.
parent 8e02c62c
......@@ -18,6 +18,7 @@ namespace Titanium.Web.Proxy.Examples.Basic
public ProxyTestController()
{
proxyServer = new ProxyServer();
proxyServer.ExceptionFunc = exception => Console.WriteLine(exception.Message);
proxyServer.TrustRootCertificate = true;
//optionally set the Certificate Engine
......@@ -99,7 +100,7 @@ namespace Titanium.Web.Proxy.Examples.Basic
{
Console.WriteLine(e.WebSession.Request.Url);
////read request headers
//read request headers
var requestHeaders = e.WebSession.Request.RequestHeaders;
var method = e.WebSession.Request.Method.ToUpper();
......
......@@ -20,8 +20,7 @@ namespace Titanium.Web.Proxy.UnitTests
{
var tasks = new List<Task>();
var mgr = new CertificateManager(CertificateEngine.DefaultWindows, "Titanium", "Titanium Root Certificate Authority",
new Lazy<Action<Exception>>(() => (e => { })).Value);
var mgr = new CertificateManager(new Lazy<Action<Exception>>(() => (e => { })).Value);
mgr.ClearIdleCertificates(1);
......
......@@ -31,14 +31,48 @@ namespace Titanium.Web.Proxy.Network
/// </summary>
internal class CertificateManager : IDisposable
{
public CertificateEngine Engine
{
get { return engine; }
set
{
//For Mono only Bouncy Castle is supported
if (RunTime.IsRunningOnMono())
{
value = CertificateEngine.BouncyCastle;
}
if (value != engine)
{
certEngine = null;
engine = value;
}
if (certEngine == null)
{
certEngine = engine == CertificateEngine.BouncyCastle ?
(ICertificateMaker) new BCCertificateMaker()
: new WinCertificateMaker();
}
}
}
private const string defaultRootCertificateIssuer = "Titanium";
private const string defaultRootRootCertificateName = "Titanium Root Certificate Authority";
private readonly ICertificateMaker certEngine;
private CertificateEngine engine;
private ICertificateMaker certEngine;
private string issuer;
private string rootCertificateName;
private bool clearCertificates { get; set; }
private X509Certificate2 rootCertificate;
/// <summary>
/// Cache dictionary
/// </summary>
......@@ -46,36 +80,56 @@ namespace Titanium.Web.Proxy.Network
private readonly Action<Exception> exceptionFunc;
internal string Issuer { get; }
internal string RootCertificateName { get; }
internal X509Certificate2 RootCertificate { get; set; }
internal CertificateManager(CertificateEngine engine,
string issuer,
string rootCertificateName,
Action<Exception> exceptionFunc)
internal string Issuer
{
this.exceptionFunc = exceptionFunc;
get { return issuer ?? defaultRootCertificateIssuer; }
set
{
issuer = value;
ClearRootCertificate();
}
}
//For Mono only Bouncy Castle is supported
if (RunTime.IsRunningOnMono()
|| engine == CertificateEngine.BouncyCastle)
internal string RootCertificateName
{
get { return rootCertificateName ?? defaultRootRootCertificateName; }
set
{
certEngine = new BCCertificateMaker();
rootCertificateName = value;
ClearRootCertificate();
}
else
}
internal X509Certificate2 RootCertificate
{
get { return rootCertificate; }
set
{
certEngine = new WinCertificateMaker();
ClearRootCertificate();
rootCertificate = value;
}
}
/// <summary>
/// Is the root certificate used by this proxy is valid?
/// </summary>
internal bool CertValidated => RootCertificate != null;
Issuer = issuer ?? defaultRootCertificateIssuer;
RootCertificateName = rootCertificateName ?? defaultRootRootCertificateName;
internal CertificateManager(Action<Exception> exceptionFunc)
{
this.exceptionFunc = exceptionFunc;
Engine = CertificateEngine.DefaultWindows;
certificateCache = new ConcurrentDictionary<string, CachedCertificate>();
}
private void ClearRootCertificate()
{
certificateCache.Clear();
rootCertificate = null;
}
private string GetRootCertificatePath()
{
var assemblyLocation = System.Reflection.Assembly.GetExecutingAssembly().Location;
......
......@@ -19,12 +19,7 @@ namespace Titanium.Web.Proxy
/// </summary>
public partial class ProxyServer : IDisposable
{
/// <summary>
/// Is the root certificate used by this proxy is valid?
/// </summary>
private bool? certValidated { get; set; }
/// <summary>
/// <summary>
/// Is the proxy currently running
/// </summary>
private bool proxyRunning { get; set; }
......@@ -85,11 +80,7 @@ namespace Titanium.Web.Proxy
public string RootCertificateIssuerName
{
get { return certificateManager.Issuer; }
set
{
CreateCertificateManager(certificateManager.RootCertificateName, value);
certValidated = null;
}
set { certificateManager.RootCertificateName = value; }
}
/// <summary>
......@@ -102,11 +93,7 @@ namespace Titanium.Web.Proxy
public string RootCertificateName
{
get { return certificateManager.RootCertificateName; }
set
{
CreateCertificateManager(value, certificateManager.Issuer);
certValidated = null;
}
set { certificateManager.Issuer = value; }
}
/// <summary>
......@@ -132,7 +119,11 @@ namespace Titanium.Web.Proxy
/// Optionally set to BouncyCastle
/// Mono only support BouncyCastle and it is the default
/// </summary>
public CertificateEngine CertificateEngine { get; set; }
public CertificateEngine CertificateEngine
{
get { return certificateManager.Engine; }
set { certificateManager.Engine = value; }
}
/// <summary>
/// Does this proxy uses the HTTP protocol 100 continue behaviour strictly?
......@@ -272,7 +263,7 @@ namespace Titanium.Web.Proxy
new FireFoxProxySettingsManager();
#endif
CreateCertificateManager(rootCertificateName, rootCertificateIssuerName);
certificateManager = new CertificateManager(ExceptionFunc);
}
/// <summary>
......@@ -369,7 +360,7 @@ namespace Titanium.Web.Proxy
EnsureRootCertificate();
//If certificate was trusted by the machine
if (certValidated == true)
if (certificateManager.CertValidated)
{
systemProxySettingsManager.SetHttpsProxy(
Equals(endPoint.IpAddress, IPAddress.Any) |
......@@ -557,17 +548,11 @@ namespace Titanium.Web.Proxy
}
private void CreateCertificateManager(string rootCertificateName, string rootCertificateIssuerName)
{
certificateManager = new CertificateManager(CertificateEngine,
rootCertificateIssuerName, rootCertificateName, ExceptionFunc);
}
private void EnsureRootCertificate()
{
if (!certValidated.HasValue)
if (!certificateManager.CertValidated)
{
certValidated = certificateManager.CreateTrustedRootCertificate();
certificateManager.CreateTrustedRootCertificate();
if (TrustRootCertificate)
{
......
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