Skip to content
This repository has been archived by the owner on Jul 9, 2023. It is now read-only.

Commit

Permalink
tls improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
honfika committed Dec 25, 2022
1 parent cd2d97e commit 7ccbdd9
Show file tree
Hide file tree
Showing 11 changed files with 761 additions and 474 deletions.
4 changes: 2 additions & 2 deletions src/Titanium.Web.Proxy/ExplicitClientHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ private async Task HandleClient(ExplicitProxyEndPoint endPoint, TcpClientConnect
{
connectRequest.IsHttps = true; // todo: move this line to the previous "if"

var sslProtocol = clientHelloInfo.SslProtocol;
if ((sslProtocol & SupportedSslProtocols) == SslProtocols.None)
var sslProtocol = clientHelloInfo.SslProtocol & SupportedSslProtocols;
if (sslProtocol == SslProtocols.None)
{
throw new Exception("Unsupported client SSL version.");
}
Expand Down
86 changes: 66 additions & 20 deletions src/Titanium.Web.Proxy/Extensions/SslExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
using System.Net.Security;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Titanium.Web.Proxy.StreamExtended;
using Titanium.Web.Proxy.StreamExtended.Models;

namespace Titanium.Web.Proxy.Extensions
{
Expand All @@ -31,24 +33,24 @@ internal static class SslExtensions
{
if (clientHelloInfo.Extensions != null && clientHelloInfo.Extensions.TryGetValue("ALPN", out var alpnExtension))
{
var alpn = alpnExtension.Data.Split(',');
if (alpn.Length != 0)
var alpn = alpnExtension.Alpns;
if (alpn.Count != 0)
{
var result = new List<SslApplicationProtocol>(alpn.Length);
foreach (string p in alpn)
{
string protocol = p.Trim();
if (protocol.Equals("http/1.1"))
{
result.Add(SslApplicationProtocol.Http11);
}
else if (protocol.Equals("h2"))
{
result.Add(SslApplicationProtocol.Http2);
}
}

return result;
return alpn;
}
}

return null;
}

internal static List<string>? GetSslProtocols(this ClientHelloInfo clientHelloInfo)
{
if (clientHelloInfo.Extensions != null && clientHelloInfo.Extensions.TryGetValue("supported_versions", out var versions))
{
var protocols = versions.Protocols;
if (protocols.Count != 0)
{
return protocols;
}
}

Expand Down Expand Up @@ -80,10 +82,54 @@ internal static Task AuthenticateAsServerAsync(this SslStream sslStream, SslServ
#if !NET6_0_OR_GREATER
namespace System.Net.Security
{
internal enum SslApplicationProtocol
internal struct SslApplicationProtocol
{
Http11,
Http2
public static readonly SslApplicationProtocol Http11 = new SslApplicationProtocol(SslExtension.Http11Utf8);

public static readonly SslApplicationProtocol Http2 = new SslApplicationProtocol(SslExtension.Http2Utf8);

public static readonly SslApplicationProtocol Http3 = new SslApplicationProtocol(SslExtension.Http3Utf8);

private readonly byte[] readOnlyProtocol;

public ReadOnlyMemory<byte> Protocol => readOnlyProtocol;

public SslApplicationProtocol(byte[] protocol)
{
readOnlyProtocol = protocol;
}

public bool Equals(SslApplicationProtocol other) => Protocol.Span.SequenceEqual(other.Protocol.Span);

public override bool Equals(object? obj) => obj is SslApplicationProtocol protocol && Equals(protocol);

public override int GetHashCode()
{
var arr = Protocol;
if (arr.Length == 0)
{
return 0;
}

int hash = 0;
for (int i = 0; i < arr.Length; i++)
{
hash = ((hash << 5) + hash) ^ arr.Span[i];
}

return hash;
}

public override string ToString()
{
return Encoding.UTF8.GetString(readOnlyProtocol);
}

public static bool operator ==(SslApplicationProtocol left, SslApplicationProtocol right) =>
left.Equals(right);

public static bool operator !=(SslApplicationProtocol left, SslApplicationProtocol right) =>
!(left == right);
}

[SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1402:FileMayOnlyContainASingleType", Justification =
Expand Down
30 changes: 30 additions & 0 deletions src/Titanium.Web.Proxy/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System;
using System.Buffers.Text;
using System.Buffers;
using System.Globalization;
using System.Text;

namespace Titanium.Web.Proxy.Extensions;

Expand All @@ -24,4 +27,31 @@ internal static int IndexOfIgnoreCase(this string str, string? value)
{
return CultureInfo.CurrentCulture.CompareInfo.IndexOf(str, value, CompareOptions.IgnoreCase);
}

internal static unsafe string ByteArrayToHexString(this ReadOnlySpan<byte> data)
{
if (data.Length == 0)
{
return string.Empty;
}

int length = data.Length * 3;
Span<byte> buf = stackalloc byte[length];
var buf2 = buf;
foreach (var b in data)
{
Utf8Formatter.TryFormat(b, buf2, out _, new StandardFormat('X', 2));
buf2[2] = 32; // space
buf2 = buf2.Slice(3);
}

#if NET6_0_OR_GREATER
return Encoding.UTF8.GetString(buf.Slice(0, length - 1));
#else
fixed (byte* bp = buf)
{
return Encoding.UTF8.GetString(bp, length -1);
}
#endif
}
}
Loading

0 comments on commit 7ccbdd9

Please sign in to comment.