Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/protocols/ioxide.ngtcp2/native/ioxide_ngtcp2_shim.c
Original file line number Diff line number Diff line change
Expand Up @@ -1405,6 +1405,25 @@ uint32_t iq_abi(void)

/* ---- connection ------------------------------------------------------------------------- */

/* The server's collected_extensions: ngtcp2's, plus the refusal it leaves out. RFC 9001 8.2 makes a
* ClientHello without quic_transport_parameters a missing_extension alert, but ngtcp2's picotls glue
* hands the extension on only when it is there and returns 0 when it is not, so picotls finished the
* handshake without the client's transport parameters. A server learns its negotiated version from
* them, and ngtcp2 checks they came only once the handshake completes - by then the first 1-RTT
* packet had already gone through assert(conn->negotiated_version) and aborted the process. picotls
* calls this with whatever it collected, an empty list included, so a ClientHello carrying only the
* draft codepoint (0xffa5), which collect_extension never asks for, is refused here too. */
static int iq_collected_extensions(ptls_t *ptls, ptls_handshake_properties_t *properties,
ptls_raw_extension_t *extensions)
{
for (ptls_raw_extension_t *ext = extensions; ext->type != UINT16_MAX; ++ext) {
if (ext->type == NGTCP2_TLSEXT_QUIC_TRANSPORT_PARAMETERS_V1) {
return ngtcp2_crypto_picotls_collected_extensions(ptls, properties, extensions);
}
}
return PTLS_ALERT_MISSING_EXTENSION;
}

/* Validate the first datagram of a new connection and build the server conn for it.
* scid_out receives the connection ID this server minted (engine->cidlen bytes) so the caller
* can register the route. shard / shard_count identify the reactor calling this, and are stamped
Expand Down Expand Up @@ -1539,6 +1558,7 @@ iq_conn *iq_accept(iq_engine *e,
free(c);
return NULL;
}
c->cptls.handshake_properties.collected_extensions = iq_collected_extensions;

ngtcp2_conn_set_tls_native_handle(c->conn, &c->cptls);
ngtcp2_ccerr_default(&c->last_error);
Expand Down
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions tests/Ioxide.Tests.E2E/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ private static int Main()
QuicSendStreamTests.Register(runner);
QuicTimerTests.Register(runner);
QuicSniHostileTests.Register(runner);
QuicTransportParameterTests.Register(runner);
QuicDemuxRoutingTests.Register(runner);
QuicMigrationTests.Register(runner);
QuicFleetJoinTests.Register(runner);
Expand Down
46 changes: 33 additions & 13 deletions tests/Ioxide.Tests.E2E/Protocols/QuicSniHostileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ private static void RegisterSecondHello(Runner runner)

/// <summary>Default localhost certificate, plus alpha.test and beta.test by name - the same
/// shape <see cref="QuicSniTests"/> uses, so a name here is one the engine really holds.</summary>
private static QuicEngine NewEngine()
internal static QuicEngine NewEngine()
{
(string cert, string key) = TestCert.Ensure();
(string alphaCert, string alphaKey) = TestCert.EnsureNamed("alpha.test");
Expand All @@ -220,7 +220,7 @@ private static QuicEngine NewEngine()
return engine;
}

private static int Serve(QuicEngine engine)
internal static int Serve(QuicEngine engine)
{
// The engine outlives the test body only as far as the reactor does; Runner stops every
// test server when the test ends, and nothing here ever completes a handshake anyway.
Expand All @@ -233,6 +233,19 @@ private static int Serve(QuicEngine engine)
return udpPort;
}

/// <summary>Which quic_transport_parameters extension a hand-built ClientHello carries.</summary>
internal enum TransportParameters
{
/// <summary>The RFC 9001 extension, 0x39, as every real client sends it.</summary>
V1,

/// <summary>The same parameters under the draft-era codepoint 0xffa5 only.</summary>
DraftOnly,

/// <summary>No transport parameters at all.</summary>
None,
}

/// <summary>
/// One ClientHello, differing from the next only in its ServerNameList. Everything else - the
/// cipher suite, the groups, the signature algorithms, the ALPN, the QUIC transport parameters
Expand All @@ -241,7 +254,10 @@ private static int Serve(QuicEngine engine)
/// <param name="names">(name, name_type) pairs for the server_name extension; empty omits it.</param>
/// <param name="withKeyShare">false sends an EMPTY client_shares list, which is what forces the
/// server to answer with a HelloRetryRequest instead of a ServerHello.</param>
internal static byte[] Hello(RawInitial raw, (string Name, byte Type)[] names, bool withKeyShare = true)
/// <param name="transportParameters">Which transport parameters extension to carry, if any; the
/// parameters themselves are the same whichever codepoint carries them.</param>
internal static byte[] Hello(RawInitial raw, (string Name, byte Type)[] names, bool withKeyShare = true,
TransportParameters transportParameters = TransportParameters.V1)
{
var b = new Buf();
b.U8(0x01); // client_hello
Expand Down Expand Up @@ -299,16 +315,20 @@ internal static byte[] Hello(RawInitial raw, (string Name, byte Type)[] names, b
b.U8(2).U16(0x0304);
b.Fill(v, 2);

b.U16(0x0039); // quic_transport_parameters (v1)
int tp = b.Mark(2);
b.U8(0x0f).U8(raw.Scid.Length).Raw(raw.Scid); // initial_source_connection_id
b.U8(0x04).U8(4).Raw([0x80, 0x10, 0x00, 0x00]); // initial_max_data
b.U8(0x05).U8(4).Raw([0x80, 0x01, 0x00, 0x00]); // initial_max_stream_data_bidi_local
b.U8(0x06).U8(4).Raw([0x80, 0x01, 0x00, 0x00]); // ...bidi_remote
b.U8(0x07).U8(4).Raw([0x80, 0x01, 0x00, 0x00]); // ...uni
b.U8(0x08).U8(1).U8(0x10); // initial_max_streams_bidi
b.U8(0x09).U8(1).U8(0x10); // initial_max_streams_uni
b.Fill(tp, 2);
if (transportParameters != TransportParameters.None)
{
// quic_transport_parameters (v1), or the draft codepoint a pre-RFC client sent instead
b.U16(transportParameters == TransportParameters.V1 ? 0x0039 : 0xffa5);
int tp = b.Mark(2);
b.U8(0x0f).U8(raw.Scid.Length).Raw(raw.Scid); // initial_source_connection_id
b.U8(0x04).U8(4).Raw([0x80, 0x10, 0x00, 0x00]); // initial_max_data
b.U8(0x05).U8(4).Raw([0x80, 0x01, 0x00, 0x00]); // initial_max_stream_data_bidi_local
b.U8(0x06).U8(4).Raw([0x80, 0x01, 0x00, 0x00]); // ...bidi_remote
b.U8(0x07).U8(4).Raw([0x80, 0x01, 0x00, 0x00]); // ...uni
b.U8(0x08).U8(1).U8(0x10); // initial_max_streams_bidi
b.U8(0x09).U8(1).U8(0x10); // initial_max_streams_uni
b.Fill(tp, 2);
}

b.Fill(exts, 2);
b.Fill(msg, 3);
Expand Down
53 changes: 53 additions & 0 deletions tests/Ioxide.Tests.E2E/Protocols/QuicTransportParameterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
namespace Ioxide.Tests;

/// <summary>
/// The quic_transport_parameters extension a ClientHello must carry (RFC 9001 8.2): a hello without
/// it, or with only the draft codepoint, is refused with missing_extension, and the server it was
/// sent to goes on serving.
/// </summary>
/// <remarks>
/// The hellos are built by hand (<see cref="RawInitial"/>, <see cref="QuicSniHostileTests.Hello"/>),
/// because no client in this tree can leave the extension out. ngtcp2's picotls glue accepts such a
/// hello, so before the shim refused it the handshake went on without the client's transport
/// parameters, the server never learned its negotiated version, and its first 1-RTT packet aborted
/// the process on ngtcp2's assert(conn->negotiated_version) - this test host included.
///
/// Each refusal is followed by a well-formed hello to the same server, so a server that is gone, or
/// that refuses everything, fails the test rather than passing it.
/// </remarks>
internal static class QuicTransportParameterTests
{
public static void Register(Runner runner)
{
foreach ((string label, QuicSniHostileTests.TransportParameters carried) in new[]
{
("without quic_transport_parameters", QuicSniHostileTests.TransportParameters.None),
("with only the draft quic_transport_parameters (0xffa5)", QuicSniHostileTests.TransportParameters.DraftOnly),
})
{
QuicSniHostileTests.TransportParameters transportParameters = carried;
runner.Test($"quic: a ClientHello {label} is refused, and the server goes on serving", () =>
{
int udpPort = QuicSniHostileTests.Serve(QuicSniHostileTests.NewEngine());

RawInitial.Reply refused;
using (var raw = new RawInitial(udpPort))
{
refused = raw.Exchange(QuicSniHostileTests.Hello(raw, [("alpha.test", 0)],
transportParameters: transportParameters));
}

Assert.True(refused.Closed && !refused.SawServerHello,
$"a ClientHello {label} must be refused with missing_extension (RFC 9001 8.2); " +
$"instead the server {QuicSniHostileTests.Describe(refused)}");

using var control = new RawInitial(udpPort);
RawInitial.Reply answered = control.Exchange(QuicSniHostileTests.Hello(control, [("alpha.test", 0)]));

Assert.True(answered.SawServerHello && !answered.Closed,
$"after refusing a ClientHello {label}, the same server should still answer a " +
$"well-formed one with a ServerHello; it {QuicSniHostileTests.Describe(answered)}");
});
}
}
}
Loading