Skip to content

Commit 6da607b

Browse files
committed
Add protocol version param to HelloVerifyRequest and update DTLS handling
1 parent 547e84c commit 6da607b

5 files changed

Lines changed: 22 additions & 11 deletions

File tree

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project>
22
<PropertyGroup>
3-
<TargetFrameworks>net9.0;net6.0</TargetFrameworks>
3+
<TargetFrameworks>net9.0;net8.0</TargetFrameworks>
44
<LangVersion>latest</LangVersion>
55
<Authors>Impostor,Next-Fast</Authors>
66
<PackageLicenseExpression>MIT</PackageLicenseExpression>

Next.Hazel/Dtls/DtlsConnectionListener.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,7 @@ private async ValueTask<bool> HandleClientHello(PeerData peer, IPEndPoint peerAd
764764
}
765765

766766
// Find an acceptable cipher suite we can use
767-
var selectedCipherSuite = CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256;
767+
const CipherSuite selectedCipherSuite = CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256;
768768
if (!clientHello.ContainsCipherSuite(CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) ||
769769
!clientHello.ContainsCurve(NamedCurve.x25519))
770770
{
@@ -787,7 +787,7 @@ private async ValueTask<bool> HandleClientHello(PeerData peer, IPEndPoint peerAd
787787
recordProtection = peer.CurrentEpoch.RecordProtection;
788788
}
789789

790-
await SendHelloVerifyRequest(peerAddress, outgoingSequence, record.Epoch, recordProtection);
790+
await SendHelloVerifyRequest(peerAddress, outgoingSequence, record.Epoch, recordProtection, peer.ProtocolVersion);
791791
return true;
792792
}
793793

@@ -1132,7 +1132,7 @@ private async ValueTask HandleNonPeerRecord(ByteSpan message, IPEndPoint peerAdd
11321132
if (!HelloVerifyRequest.VerifyCookie(clientHello.Cookie, peerAddress, currentCookieHmac))
11331133
if (!HelloVerifyRequest.VerifyCookie(clientHello.Cookie, peerAddress, previousCookieHmac))
11341134
{
1135-
await SendHelloVerifyRequest(peerAddress, 1, 0, NullRecordProtection.Instance);
1135+
await SendHelloVerifyRequest(peerAddress, 1, 0, NullRecordProtection.Instance, clientHello.ClientProtocolVersion);
11361136
return;
11371137
}
11381138

@@ -1151,7 +1151,7 @@ private async ValueTask HandleNonPeerRecord(ByteSpan message, IPEndPoint peerAdd
11511151

11521152
//Send a HelloVerifyRequest handshake message to a peer
11531153
private ValueTask SendHelloVerifyRequest(IPEndPoint peerAddress, ulong recordSequence, ushort epoch,
1154-
IRecordProtection recordProtection)
1154+
IRecordProtection recordProtection, ProtocolVersion protocolVersion)
11551155
{
11561156
// Do we need to rotate the HMAC key?
11571157
var now = DateTime.UtcNow;
@@ -1177,6 +1177,7 @@ private ValueTask SendHelloVerifyRequest(IPEndPoint peerAddress, ulong recordSeq
11771177
var record = new Record
11781178
{
11791179
ContentType = ContentType.Handshake,
1180+
ProtocolVersion = protocolVersion,
11801181
Epoch = epoch,
11811182
SequenceNumber = recordSequence,
11821183
Length = (ushort)recordProtection.GetEncryptedSize(plaintextPayloadSize)
@@ -1189,7 +1190,7 @@ private ValueTask SendHelloVerifyRequest(IPEndPoint peerAddress, ulong recordSeq
11891190
writer = writer[Record.Size..];
11901191
handshake.Encode(writer);
11911192
writer = writer[Handshake.Handshake.Size..];
1192-
HelloVerifyRequest.Encode(writer, peerAddress, currentCookieHmac);
1193+
HelloVerifyRequest.Encode(writer, peerAddress, currentCookieHmac, protocolVersion);
11931194

11941195
// Protect record payload
11951196
recordProtection.EncryptServerPlaintext(

Next.Hazel/Dtls/Handshake/HelloVerifyRequest.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,12 @@ public static bool Parse(out HelloVerifyRequest result, ProtocolVersion? expecte
5353
/// <param name="span"></param>
5454
/// <param name="peerAddress">Address of the remote peer</param>
5555
/// <param name="hmac">Listener HMAC signature provider</param>
56-
public static void Encode(ByteSpan span, EndPoint peerAddress, HMAC hmac)
56+
/// <param name="protocolVersion"></param>
57+
public static void Encode(ByteSpan span, EndPoint peerAddress, HMAC hmac, ProtocolVersion protocolVersion)
5758
{
5859
var cookie = ComputeAddressMac(peerAddress, hmac);
5960

60-
span.WriteBigEndian16((ushort)ProtocolVersion.DTLS1_2);
61+
span.WriteBigEndian16((ushort)protocolVersion);
6162
span[2] = CookieSize;
6263
cookie.CopyTo(span[3..]);
6364
}

Next.Hazel/Dtls/Record.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,7 @@ public static bool Parse(ByteSpan span)
9393
if (span.Length != 1) return false;
9494

9595
var value = (Value)span[0];
96-
if (value != Value.ChangeCipherSpec) return false;
97-
98-
return true;
96+
return value == Value.ChangeCipherSpec;
9997
}
10098

10199
/// <summary>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using Next.Hazel.Abstractions;
2+
3+
namespace Next.Hazel.Extensions;
4+
5+
public class DefaultMessageWriterProvider : IMessageWriterProvider
6+
{
7+
public IMessageWriter Get(MessageType sendOption = MessageType.Unreliable)
8+
{
9+
return MessageWriter.Get(sendOption);
10+
}
11+
}

0 commit comments

Comments
 (0)