Commit cfdcaee9 authored by Honfika's avatar Honfika

Allow to generate root certificate with Titanium without storing it in file system

parent b6d18a61
...@@ -17,6 +17,12 @@ namespace Titanium.Web.Proxy.Examples.Basic ...@@ -17,6 +17,12 @@ namespace Titanium.Web.Proxy.Examples.Basic
public ProxyTestController() public ProxyTestController()
{ {
proxyServer = new ProxyServer(); proxyServer = new ProxyServer();
//generate root certificate without storing it in file system
//proxyServer.CertificateEngine = Network.CertificateEngine.BouncyCastle;
//proxyServer.CertificateManager.CreateTrustedRootCertificate(false);
//proxyServer.CertificateManager.TrustRootCertificate();
proxyServer.ExceptionFunc = exception => Console.WriteLine(exception.Message); proxyServer.ExceptionFunc = exception => Console.WriteLine(exception.Message);
proxyServer.TrustRootCertificate = true; proxyServer.TrustRootCertificate = true;
......
...@@ -29,9 +29,9 @@ namespace Titanium.Web.Proxy.Network ...@@ -29,9 +29,9 @@ namespace Titanium.Web.Proxy.Network
/// <summary> /// <summary>
/// A class to manage SSL certificates used by this proxy server /// A class to manage SSL certificates used by this proxy server
/// </summary> /// </summary>
internal class CertificateManager : IDisposable public class CertificateManager : IDisposable
{ {
public CertificateEngine Engine internal CertificateEngine Engine
{ {
get { return engine; } get { return engine; }
set set
...@@ -164,10 +164,13 @@ namespace Titanium.Web.Proxy.Network ...@@ -164,10 +164,13 @@ namespace Titanium.Web.Proxy.Network
/// <summary> /// <summary>
/// Attempts to create a RootCertificate /// Attempts to create a RootCertificate
/// </summary> /// </summary>
/// <returns>true if succeeded, else false</returns> /// <param name="persistToFile">if set to <c>true</c> try to load/save the certificate from rootCert.pfx.</param>
internal bool CreateTrustedRootCertificate() /// <returns>
/// true if succeeded, else false
/// </returns>
public bool CreateTrustedRootCertificate(bool persistToFile = true)
{ {
if (RootCertificate == null) if (persistToFile && RootCertificate == null)
{ {
RootCertificate = LoadRootCertificate(); RootCertificate = LoadRootCertificate();
} }
...@@ -186,7 +189,7 @@ namespace Titanium.Web.Proxy.Network ...@@ -186,7 +189,7 @@ namespace Titanium.Web.Proxy.Network
exceptionFunc(e); exceptionFunc(e);
} }
if (RootCertificate != null) if (persistToFile && RootCertificate != null)
{ {
try try
{ {
...@@ -205,7 +208,7 @@ namespace Titanium.Web.Proxy.Network ...@@ -205,7 +208,7 @@ namespace Titanium.Web.Proxy.Network
/// <summary> /// <summary>
/// Trusts the root certificate. /// Trusts the root certificate.
/// </summary> /// </summary>
internal void TrustRootCertificate() public void TrustRootCertificate()
{ {
//current user //current user
TrustRootCertificate(StoreLocation.CurrentUser); TrustRootCertificate(StoreLocation.CurrentUser);
......
...@@ -24,11 +24,6 @@ namespace Titanium.Web.Proxy ...@@ -24,11 +24,6 @@ namespace Titanium.Web.Proxy
/// </summary> /// </summary>
private bool proxyRunning { get; set; } private bool proxyRunning { get; set; }
/// <summary>
/// Manages certificates used by this proxy
/// </summary>
private CertificateManager certificateManager { get; set; }
/// <summary> /// <summary>
/// An default exception log func /// An default exception log func
/// </summary> /// </summary>
...@@ -64,13 +59,18 @@ namespace Titanium.Web.Proxy ...@@ -64,13 +59,18 @@ namespace Titanium.Web.Proxy
/// </summary> /// </summary>
public int BufferSize { get; set; } = 8192; public int BufferSize { get; set; } = 8192;
/// <summary>
/// Manages certificates used by this proxy
/// </summary>
public CertificateManager CertificateManager { get; }
/// <summary> /// <summary>
/// The root certificate /// The root certificate
/// </summary> /// </summary>
public X509Certificate2 RootCertificate public X509Certificate2 RootCertificate
{ {
get { return certificateManager.RootCertificate; } get { return CertificateManager.RootCertificate; }
set { certificateManager.RootCertificate = value; } set { CertificateManager.RootCertificate = value; }
} }
/// <summary> /// <summary>
...@@ -79,8 +79,8 @@ namespace Titanium.Web.Proxy ...@@ -79,8 +79,8 @@ namespace Titanium.Web.Proxy
/// </summary> /// </summary>
public string RootCertificateIssuerName public string RootCertificateIssuerName
{ {
get { return certificateManager.Issuer; } get { return CertificateManager.Issuer; }
set { certificateManager.RootCertificateName = value; } set { CertificateManager.RootCertificateName = value; }
} }
/// <summary> /// <summary>
...@@ -92,8 +92,8 @@ namespace Titanium.Web.Proxy ...@@ -92,8 +92,8 @@ namespace Titanium.Web.Proxy
/// </summary> /// </summary>
public string RootCertificateName public string RootCertificateName
{ {
get { return certificateManager.RootCertificateName; } get { return CertificateManager.RootCertificateName; }
set { certificateManager.Issuer = value; } set { CertificateManager.Issuer = value; }
} }
/// <summary> /// <summary>
...@@ -121,8 +121,8 @@ namespace Titanium.Web.Proxy ...@@ -121,8 +121,8 @@ namespace Titanium.Web.Proxy
/// </summary> /// </summary>
public CertificateEngine CertificateEngine public CertificateEngine CertificateEngine
{ {
get { return certificateManager.Engine; } get { return CertificateManager.Engine; }
set { certificateManager.Engine = value; } set { CertificateManager.Engine = value; }
} }
/// <summary> /// <summary>
...@@ -251,7 +251,7 @@ namespace Titanium.Web.Proxy ...@@ -251,7 +251,7 @@ namespace Titanium.Web.Proxy
new FireFoxProxySettingsManager(); new FireFoxProxySettingsManager();
#endif #endif
certificateManager = new CertificateManager(ExceptionFunc); CertificateManager = new CertificateManager(ExceptionFunc);
if (rootCertificateName != null) if (rootCertificateName != null)
{ {
RootCertificateName = rootCertificateName; RootCertificateName = rootCertificateName;
...@@ -356,7 +356,7 @@ namespace Titanium.Web.Proxy ...@@ -356,7 +356,7 @@ namespace Titanium.Web.Proxy
EnsureRootCertificate(); EnsureRootCertificate();
//If certificate was trusted by the machine //If certificate was trusted by the machine
if (certificateManager.CertValidated) if (CertificateManager.CertValidated)
{ {
systemProxySettingsManager.SetHttpsProxy( systemProxySettingsManager.SetHttpsProxy(
Equals(endPoint.IpAddress, IPAddress.Any) | Equals(endPoint.IpAddress, IPAddress.Any) |
...@@ -435,7 +435,7 @@ namespace Titanium.Web.Proxy ...@@ -435,7 +435,7 @@ namespace Titanium.Web.Proxy
Listen(endPoint); Listen(endPoint);
} }
certificateManager.ClearIdleCertificates(CertificateCacheTimeOutMinutes); CertificateManager.ClearIdleCertificates(CertificateCacheTimeOutMinutes);
proxyRunning = true; proxyRunning = true;
} }
...@@ -468,7 +468,7 @@ namespace Titanium.Web.Proxy ...@@ -468,7 +468,7 @@ namespace Titanium.Web.Proxy
ProxyEndPoints.Clear(); ProxyEndPoints.Clear();
certificateManager?.StopClearIdleCertificates(); CertificateManager?.StopClearIdleCertificates();
proxyRunning = false; proxyRunning = false;
} }
...@@ -483,7 +483,7 @@ namespace Titanium.Web.Proxy ...@@ -483,7 +483,7 @@ namespace Titanium.Web.Proxy
Stop(); Stop();
} }
certificateManager?.Dispose(); CertificateManager?.Dispose();
} }
/// <summary> /// <summary>
...@@ -543,13 +543,13 @@ namespace Titanium.Web.Proxy ...@@ -543,13 +543,13 @@ namespace Titanium.Web.Proxy
private void EnsureRootCertificate() private void EnsureRootCertificate()
{ {
if (!certificateManager.CertValidated) if (!CertificateManager.CertValidated)
{ {
certificateManager.CreateTrustedRootCertificate(); CertificateManager.CreateTrustedRootCertificate();
if (TrustRootCertificate) if (TrustRootCertificate)
{ {
certificateManager.TrustRootCertificate(); CertificateManager.TrustRootCertificate();
} }
} }
} }
......
...@@ -110,7 +110,7 @@ namespace Titanium.Web.Proxy ...@@ -110,7 +110,7 @@ namespace Titanium.Web.Proxy
{ {
sslStream = new SslStream(clientStream, true); sslStream = new SslStream(clientStream, true);
var certificate = endPoint.GenericCertificate ?? certificateManager.CreateCertificate(httpRemoteUri.Host, false); var certificate = endPoint.GenericCertificate ?? CertificateManager.CreateCertificate(httpRemoteUri.Host, false);
//Successfully managed to authenticate the client using the fake certificate //Successfully managed to authenticate the client using the fake certificate
await sslStream.AuthenticateAsServerAsync(certificate, false, await sslStream.AuthenticateAsServerAsync(certificate, false,
...@@ -177,7 +177,7 @@ namespace Titanium.Web.Proxy ...@@ -177,7 +177,7 @@ namespace Titanium.Web.Proxy
var sslStream = new SslStream(clientStream, true); var sslStream = new SslStream(clientStream, true);
//implement in future once SNI supported by SSL stream, for now use the same certificate //implement in future once SNI supported by SSL stream, for now use the same certificate
var certificate = certificateManager.CreateCertificate(endPoint.GenericCertificateName, false); var certificate = CertificateManager.CreateCertificate(endPoint.GenericCertificateName, false);
try try
{ {
......
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