Skip to content

Commit

Permalink
refactor!: utilize the new certificate types
Browse files Browse the repository at this point in the history
  • Loading branch information
ArachisH committed Apr 7, 2024
1 parent dbb484b commit 30289ec
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 86 deletions.
2 changes: 1 addition & 1 deletion Eavesdrop.CLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static void Main()
//Eavesdropper.IsActingAsForwardingServer = true;

/* Otherwise, to be able to decrypt HTTPS traffic, we need to install a self-signed certificate to the root store. */
Eavesdropper.Certifier?.CreateTrustedRootCertificate();
Eavesdropper.CertProvider?.TryCreateTrustedRootCA("Eavesdrop");

Eavesdropper.Initiate(12030);
Console.WriteLine("Press any key to terminate the application at any time...");
Expand Down
73 changes: 0 additions & 73 deletions Eavesdrop.Tests/Certificates/SelfSignedCertificateHandler.cs

This file was deleted.

6 changes: 3 additions & 3 deletions Eavesdrop.Tests/EavesNodeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using System.Runtime.InteropServices;

using Eavesdrop.Network;
using Eavesdrop.Tests.Certificates;
using Eavesdrop.Certificates;

namespace Eavesdrop.Tests;

Expand Down Expand Up @@ -53,10 +53,10 @@ public async Task InterceptGetRequest_Https_RequestLineWithRelativePath()
*/
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) return;

var emptyCertifier = new SelfSignedCertificateHandler();
var certProvider = new CertificateProvider();
var (client, server) = await CreateConnectedPairAsync();

using var node = new EavesNode(server, emptyCertifier, false);
using var node = new EavesNode(server, certProvider, false);
using var clientStream = new NetworkStream(client, true);

Task<HttpRequestMessage> serverReceiveTask = node.ReceiveHttpRequestAsync();
Expand Down
8 changes: 4 additions & 4 deletions Eavesdrop/Eavesdropper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

using Eavesdrop.Network;
using Eavesdrop.Network.Http;
using Eavesdrop.Certificates;

namespace Eavesdrop;

Expand Down Expand Up @@ -78,8 +79,7 @@ public static IWebProxy? Proxy
}
}

public static Certifier? Certifier { get; set; }
public static Certifier DefaultCertifier { get; }
public static CertificateProvider CertProvider { get; }

public static List<string> Targets { get; }
public static List<string> IntranetHosts { get; }
Expand Down Expand Up @@ -123,7 +123,7 @@ static Eavesdropper()

Targets = new List<string>();
IntranetHosts = new List<string>();
Certifier = DefaultCertifier = new Certifier("Eavesdrop", "Eavesdrop Root Certificate Authority");
CertProvider = new CertificateProvider();
}

public static void Terminate()
Expand Down Expand Up @@ -249,7 +249,7 @@ private static async Task InterceptRequestAsync()
}
private static async Task HandleSocketAsync(Socket client, CancellationToken cancellationToken = default)
{
using var local = new EavesNode(client, Certifier, IsActingAsForwardingServer);
using var local = new EavesNode(client, CertProvider, IsActingAsForwardingServer);

RequestInterceptedEventArgs? requestArgs = null;
ResponseInterceptedEventArgs? responseArgs = null;
Expand Down
11 changes: 6 additions & 5 deletions Eavesdrop/Network/EavesNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Security.Cryptography.X509Certificates;

using Eavesdrop.Network.Http;
using Eavesdrop.Certificates;

namespace Eavesdrop.Network;

Expand All @@ -19,7 +20,7 @@ public sealed class EavesNode : IDisposable
private static ReadOnlySpan<byte> _eolBytes => "\r\n"u8;
private static ReadOnlySpan<byte> _eofBytes => "\r\n\r\n"u8;

private readonly ICertifier? _certifier;
private readonly CertificateProvider? _certProvider;

private bool _disposed;
private Stream _stream;
Expand All @@ -45,13 +46,13 @@ static EavesNode()
["CONNECT"] = AdditionalHttpMethods.Connect
};
}
public EavesNode(Socket socket, ICertifier? certifier, bool isHandlingConnectRequests = true)
public EavesNode(Socket socket, CertificateProvider? certProvider, bool isHandlingConnectRequests = true)
{
IsHandlingConnectRequests = isHandlingConnectRequests;

socket.NoDelay = true;

_certifier = certifier;
_certProvider = certProvider;
_stream = new NetworkStream(socket, ownsSocket: true);
}

Expand All @@ -77,12 +78,12 @@ public async Task<HttpRequestMessage> ReceiveHttpRequestAsync(CancellationToken
if (IsHandlingConnectRequests) return request;

await SendHttpResponseAsync(_okResponse, cancellationToken).ConfigureAwait(false);
if (_certifier == null)
if (_certProvider == null)
{
throw new NotSupportedException("Cannot process HTTPS upgrade without a certifier.");
}

X509Certificate2? certificate = _certifier?.GenerateCertificate(request.RequestUri.DnsSafeHost);
X509Certificate2? certificate = _certProvider?.IssueCertificate(request.RequestUri.DnsSafeHost);
if (certificate == null)
{
throw new NullReferenceException($"Failed to generate a self-signed certificate for '{request.RequestUri.DnsSafeHost}'.");
Expand Down

0 comments on commit 30289ec

Please sign in to comment.