Commit 3d73a6bd authored by jmh76's avatar jmh76

#550 sslStream not disposed; improve ProxyConnectException

parent 9570acbb
...@@ -13,11 +13,11 @@ namespace Titanium.Web.Proxy.Exceptions ...@@ -13,11 +13,11 @@ namespace Titanium.Web.Proxy.Exceptions
/// </summary> /// </summary>
/// <param name="message">Message for this exception</param> /// <param name="message">Message for this exception</param>
/// <param name="innerException">Associated inner exception</param> /// <param name="innerException">Associated inner exception</param>
/// <param name="connectEventArgs">Instance of <see cref="EventArguments.TunnelConnectSessionEventArgs" /> associated to the exception</param> /// <param name="session">Instance of <see cref="EventArguments.TunnelConnectSessionEventArgs" /> associated to the exception</param>
internal ProxyConnectException(string message, Exception innerException, TunnelConnectSessionEventArgs connectEventArgs) : base( internal ProxyConnectException(string message, Exception innerException, SessionEventArgsBase session) : base(
message, innerException) message, innerException)
{ {
ConnectEventArgs = connectEventArgs; Session = session;
} }
/// <summary> /// <summary>
...@@ -26,6 +26,6 @@ namespace Titanium.Web.Proxy.Exceptions ...@@ -26,6 +26,6 @@ namespace Titanium.Web.Proxy.Exceptions
/// <remarks> /// <remarks>
/// This object properties should not be edited. /// This object properties should not be edited.
/// </remarks> /// </remarks>
public TunnelConnectSessionEventArgs ConnectEventArgs { get; } public SessionEventArgsBase Session { get; }
} }
} }
...@@ -13,11 +13,11 @@ namespace Titanium.Web.Proxy.Exceptions ...@@ -13,11 +13,11 @@ namespace Titanium.Web.Proxy.Exceptions
/// </summary> /// </summary>
/// <param name="message">Message for this exception</param> /// <param name="message">Message for this exception</param>
/// <param name="innerException">Associated inner exception</param> /// <param name="innerException">Associated inner exception</param>
/// <param name="sessionEventArgs">Instance of <see cref="EventArguments.SessionEventArgs" /> associated to the exception</param> /// <param name="session">Instance of <see cref="EventArguments.SessionEventArgs" /> associated to the exception</param>
internal ProxyHttpException(string message, Exception innerException, SessionEventArgs sessionEventArgs) : base( internal ProxyHttpException(string message, Exception innerException, SessionEventArgs session) : base(
message, innerException) message, innerException)
{ {
SessionEventArgs = sessionEventArgs; Session = session;
} }
/// <summary> /// <summary>
...@@ -26,6 +26,6 @@ namespace Titanium.Web.Proxy.Exceptions ...@@ -26,6 +26,6 @@ namespace Titanium.Web.Proxy.Exceptions
/// <remarks> /// <remarks>
/// This object properties should not be edited. /// This object properties should not be edited.
/// </remarks> /// </remarks>
public SessionEventArgs SessionEventArgs { get; } public SessionEventArgs Session { get; }
} }
} }
...@@ -161,13 +161,13 @@ namespace Titanium.Web.Proxy ...@@ -161,13 +161,13 @@ namespace Titanium.Web.Proxy
cancellationToken: CancellationToken.None); cancellationToken: CancellationToken.None);
} }
X509Certificate2 certificate = null;
try try
{ {
sslStream = new SslStream(clientStream); sslStream = new SslStream(clientStream, true);
string certName = HttpHelper.GetWildCardDomainName(connectHostname); string certName = HttpHelper.GetWildCardDomainName(connectHostname);
certificate = endPoint.GenericCertificate ??
var certificate = endPoint.GenericCertificate ??
await CertificateManager.CreateServerCertificate(certName); await CertificateManager.CreateServerCertificate(certName);
// Successfully managed to authenticate the client using the fake certificate // Successfully managed to authenticate the client using the fake certificate
...@@ -197,9 +197,13 @@ namespace Titanium.Web.Proxy ...@@ -197,9 +197,13 @@ namespace Titanium.Web.Proxy
} }
catch (Exception e) catch (Exception e)
{ {
sslStream?.Dispose(); var certname = certificate?.GetNameInfo(X509NameType.SimpleName, false);
throw new ProxyConnectException( throw new ProxyConnectException(
$"Could'nt authenticate client '{connectHostname}' with fake certificate.", e, connectArgs); $"Couldn't authenticate host '{connectHostname}' with certificate '{certname}'.", e, connectArgs);
}
finally
{
sslStream?.Dispose();
} }
if (await HttpHelper.IsConnectMethod(clientStream) == -1) if (await HttpHelper.IsConnectMethod(clientStream) == -1)
......
...@@ -4,6 +4,7 @@ using System.IO; ...@@ -4,6 +4,7 @@ using System.IO;
using System.Net.Security; using System.Net.Security;
using System.Net.Sockets; using System.Net.Sockets;
using System.Security.Authentication; using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using StreamExtended; using StreamExtended;
...@@ -62,16 +63,17 @@ namespace Titanium.Web.Proxy ...@@ -62,16 +63,17 @@ namespace Titanium.Web.Proxy
SslStream sslStream = null; SslStream sslStream = null;
//do client authentication using fake certificate //do client authentication using certificate
X509Certificate2 certificate = null;
try try
{ {
sslStream = new SslStream(clientStream); sslStream = new SslStream(clientStream, true);
string certName = HttpHelper.GetWildCardDomainName(httpsHostName); string certName = HttpHelper.GetWildCardDomainName(httpsHostName);
var certificate = endPoint.GenericCertificate ?? certificate = endPoint.GenericCertificate ??
await CertificateManager.CreateServerCertificate(certName); await CertificateManager.CreateServerCertificate(certName);
// Successfully managed to authenticate the client using the fake certificate // Successfully managed to authenticate the client using the certificate
await sslStream.AuthenticateAsServerAsync(certificate, false, SslProtocols.Tls, false); await sslStream.AuthenticateAsServerAsync(certificate, false, SslProtocols.Tls, false);
// HTTPS server created - we can now decrypt the client's traffic // HTTPS server created - we can now decrypt the client's traffic
...@@ -81,9 +83,18 @@ namespace Titanium.Web.Proxy ...@@ -81,9 +83,18 @@ namespace Titanium.Web.Proxy
} }
catch (Exception e) catch (Exception e)
{ {
sslStream?.Dispose(); var certname = certificate?.GetNameInfo(X509NameType.SimpleName, false);
var session = new SessionEventArgs(this, endPoint, cancellationTokenSource)
{
ProxyClient = { Connection = clientConnection },
HttpClient = { ConnectRequest = null }
};
throw new ProxyConnectException( throw new ProxyConnectException(
$"Could'nt authenticate client '{httpsHostName}' with fake certificate.", e, null); $"Couldn't authenticate host '{httpsHostName}' with certificate '{certname}'.", e, session);
}
finally
{
sslStream?.Dispose();
} }
} }
else else
......
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