diff --git a/src/protocols/ioxide.ngtcp2/native/ioxide_ngtcp2_shim.c b/src/protocols/ioxide.ngtcp2/native/ioxide_ngtcp2_shim.c index d25f3777..fea475ad 100644 --- a/src/protocols/ioxide.ngtcp2/native/ioxide_ngtcp2_shim.c +++ b/src/protocols/ioxide.ngtcp2/native/ioxide_ngtcp2_shim.c @@ -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 @@ -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); diff --git a/src/protocols/ioxide.ngtcp2/runtimes/linux-arm64/native/libioxide_ngtcp2.so b/src/protocols/ioxide.ngtcp2/runtimes/linux-arm64/native/libioxide_ngtcp2.so index 9981f106..3dd489f7 100755 Binary files a/src/protocols/ioxide.ngtcp2/runtimes/linux-arm64/native/libioxide_ngtcp2.so and b/src/protocols/ioxide.ngtcp2/runtimes/linux-arm64/native/libioxide_ngtcp2.so differ diff --git a/src/protocols/ioxide.ngtcp2/runtimes/linux-x64/native/libioxide_ngtcp2.so b/src/protocols/ioxide.ngtcp2/runtimes/linux-x64/native/libioxide_ngtcp2.so index 909df8e7..9c95f90c 100755 Binary files a/src/protocols/ioxide.ngtcp2/runtimes/linux-x64/native/libioxide_ngtcp2.so and b/src/protocols/ioxide.ngtcp2/runtimes/linux-x64/native/libioxide_ngtcp2.so differ diff --git a/tests/Ioxide.Tests.E2E/Program.cs b/tests/Ioxide.Tests.E2E/Program.cs index 28137e1b..c16feeb0 100644 --- a/tests/Ioxide.Tests.E2E/Program.cs +++ b/tests/Ioxide.Tests.E2E/Program.cs @@ -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); diff --git a/tests/Ioxide.Tests.E2E/Protocols/QuicSniHostileTests.cs b/tests/Ioxide.Tests.E2E/Protocols/QuicSniHostileTests.cs index c536a22b..60908efe 100644 --- a/tests/Ioxide.Tests.E2E/Protocols/QuicSniHostileTests.cs +++ b/tests/Ioxide.Tests.E2E/Protocols/QuicSniHostileTests.cs @@ -208,7 +208,7 @@ private static void RegisterSecondHello(Runner runner) /// Default localhost certificate, plus alpha.test and beta.test by name - the same /// shape uses, so a name here is one the engine really holds. - private static QuicEngine NewEngine() + internal static QuicEngine NewEngine() { (string cert, string key) = TestCert.Ensure(); (string alphaCert, string alphaKey) = TestCert.EnsureNamed("alpha.test"); @@ -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. @@ -233,6 +233,19 @@ private static int Serve(QuicEngine engine) return udpPort; } + /// Which quic_transport_parameters extension a hand-built ClientHello carries. + internal enum TransportParameters + { + /// The RFC 9001 extension, 0x39, as every real client sends it. + V1, + + /// The same parameters under the draft-era codepoint 0xffa5 only. + DraftOnly, + + /// No transport parameters at all. + None, + } + /// /// 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 @@ -241,7 +254,10 @@ private static int Serve(QuicEngine engine) /// (name, name_type) pairs for the server_name extension; empty omits it. /// false sends an EMPTY client_shares list, which is what forces the /// server to answer with a HelloRetryRequest instead of a ServerHello. - internal static byte[] Hello(RawInitial raw, (string Name, byte Type)[] names, bool withKeyShare = true) + /// Which transport parameters extension to carry, if any; the + /// parameters themselves are the same whichever codepoint carries them. + 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 @@ -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); diff --git a/tests/Ioxide.Tests.E2E/Protocols/QuicTransportParameterTests.cs b/tests/Ioxide.Tests.E2E/Protocols/QuicTransportParameterTests.cs new file mode 100644 index 00000000..d0b9eb42 --- /dev/null +++ b/tests/Ioxide.Tests.E2E/Protocols/QuicTransportParameterTests.cs @@ -0,0 +1,53 @@ +namespace Ioxide.Tests; + +/// +/// 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. +/// +/// +/// The hellos are built by hand (, ), +/// 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. +/// +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)}"); + }); + } + } +}