diff --git a/src/protocols/ioxide.ngtcp2/native/ioxide_ngtcp2_shim.c b/src/protocols/ioxide.ngtcp2/native/ioxide_ngtcp2_shim.c index d25f3777..ffb2fff8 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); @@ -1730,11 +1750,20 @@ ngtcp2_ssize iq_conn_close(iq_conn *c, uint64_t app_error_code, /* Engine-initiated close: the peer is told which TRANSPORT error ended this, translated from the * ngtcp2 library error code, instead of the connection simply going quiet and the peer waiting out * its idle timeout. Returns <= 0 when there is nothing to send - notably in draining and closing - * state, where a CONNECTION_CLOSE would be a reply to one the peer already sent. */ + * state, where a CONNECTION_CLOSE would be a reply to one the peer already sent. + * A handshake the TLS stack refused is the exception: ngtcp2_ccerr_set_liberr infers PROTOCOL_VIOLATION + * for NGTCP2_ERR_CRYPTO, as for any error it has no case for, while the alert the stack raised is held by + * the conn. RFC 9001 4.8 has it sent as CRYPTO_ERROR (0x100 + alert), as ngtcp2's own examples do. The + * managed side passes some codes negated, so either sign is taken. */ ngtcp2_ssize iq_conn_close_liberr(iq_conn *c, int liberr, uint8_t *dest, size_t destlen, uint64_t ts) { - ngtcp2_ccerr_set_liberr(&c->last_error, liberr, NULL, 0); + uint8_t alert = ngtcp2_conn_get_tls_alert2(c->conn); + if ((liberr == NGTCP2_ERR_CRYPTO || liberr == -NGTCP2_ERR_CRYPTO) && alert != 0) { + ngtcp2_ccerr_set_tls_alert(&c->last_error, alert, NULL, 0); + } else { + ngtcp2_ccerr_set_liberr(&c->last_error, liberr, NULL, 0); + } return ngtcp2_conn_write_connection_close(c->conn, &c->path, NULL, dest, destlen, &c->last_error, ts); } 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..be4b4df1 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..ecca1256 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..dc370202 100644 --- a/tests/Ioxide.Tests.E2E/Protocols/QuicSniHostileTests.cs +++ b/tests/Ioxide.Tests.E2E/Protocols/QuicSniHostileTests.cs @@ -30,10 +30,9 @@ namespace Ioxide.Tests; /// refusal mean something is that the same hello with one host_name - and the same hello whose /// second list entry is of another name_type - are both served by the same server. /// -/// No test asserts WHICH alert was sent, because the shim cannot carry one: it closes with -/// ngtcp2_ccerr_set_liberr(NGTCP2_ERR_CRYPTO), which infers PROTOCOL_VIOLATION, so every TLS alert -/// this server raises reaches the peer as the same code. What is asserted is the branch taken - -/// closed, retried, or answered - which is the part the alert would only have annotated. +/// A refusal is asserted to close with the TLS alert as CRYPTO_ERROR (RFC 9001 4.8): the exact alert +/// where the shim chooses it (decode_error for two host names), and a CRYPTO_ERROR of any alert where +/// picotls does. Before #638 every alert reached the peer as PROTOCOL_VIOLATION. /// internal static class QuicSniHostileTests { @@ -60,8 +59,8 @@ private static void RegisterServerNameList(Runner runner) using var raw = new RawInitial(udpPort); RawInitial.Reply reply = raw.Exchange(Hello(raw, [("alpha.test", 0), ("beta.test", 0)])); - Assert.True(reply.Closed, - "a ClientHello carrying two host_name entries must be refused, and the peer told so; " + + Assert.True(reply.Closed && reply.CloseError == CryptoError(Alert.DecodeError), + "a ClientHello carrying two host_name entries must be refused with decode_error; " + $"instead the server {Describe(reply)}"); Assert.True(!reply.SawServerHello, "a ClientHello carrying two host_name entries was answered with a ServerHello - the " + @@ -110,8 +109,8 @@ private static void RegisterServerNameList(Runner runner) using var raw = new RawInitial(udpPort); RawInitial.Reply reply = raw.Exchange(Hello(raw, [("alpha.test\0evil.test", 0)])); - Assert.True(reply.Closed && !reply.SawServerHello, - $"a host name containing a NUL must not start a handshake; the server {Describe(reply)}"); + Assert.True(reply.Closed && !reply.SawServerHello && IsCryptoError(reply.CloseError), + $"a host name containing a NUL must be refused with a TLS alert; the server {Describe(reply)}"); }); runner.Test("sni/quic: an empty host name is refused", () => @@ -124,8 +123,8 @@ private static void RegisterServerNameList(Runner runner) using var raw = new RawInitial(udpPort); RawInitial.Reply reply = raw.Exchange(Hello(raw, [("", 0)])); - Assert.True(reply.Closed && !reply.SawServerHello, - $"an empty host_name must not start a handshake; the server {Describe(reply)}"); + Assert.True(reply.Closed && !reply.SawServerHello && IsCryptoError(reply.CloseError), + $"an empty host_name must be refused with a TLS alert; the server {Describe(reply)}"); }); runner.Test("sni/quic: a ClientHello with no server_name extension is served", () => @@ -176,7 +175,7 @@ private static void RegisterSecondHello(Runner runner) RawInitial.Reply second = raw.Exchange(Hello(raw, [("beta.test", 0)])); - Assert.True(second.Closed && !second.SawServerHello, + Assert.True(second.Closed && !second.SawServerHello && IsCryptoError(second.CloseError), "the second ClientHello asked for beta.test where the first asked for alpha.test, and " + "the certificate was already chosen from the first - the server should have refused " + $"the change instead of continuing; it {Describe(second)}"); @@ -208,7 +207,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 +219,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 +232,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 +253,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, string alpn = "h3") { var b = new Buf(); b.U8(0x01); // client_hello @@ -280,7 +295,8 @@ internal static byte[] Hello(RawInitial raw, (string Name, byte Type)[] names, b b.U16(0x0010); // application_layer_protocol_negotiation int a = b.Mark(2); int alist = b.Mark(2); - b.U8(2).Raw("h3"u8); + byte[] protocol = Encoding.ASCII.GetBytes(alpn); + b.U8(protocol.Length).Raw(protocol); b.Fill(alist, 2); b.Fill(a, 2); @@ -299,22 +315,39 @@ 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); return b.ToArray(); } + /// TLS alerts (RFC 8446 6) this file expects a refusal to carry. + internal static class Alert + { + public const byte DecodeError = 50; + public const byte MissingExtension = 109; + public const byte NoApplicationProtocol = 120; + } + + /// The QUIC transport error a TLS alert is sent as (RFC 9001 4.8). + internal static ulong CryptoError(byte alert) => 0x100ul + alert; + + internal static bool IsCryptoError(ulong code) => code is >= 0x100 and <= 0x1ff; + /// What the server actually did, for a failure message that names it. internal static string Describe(RawInitial.Reply r) { diff --git a/tests/Ioxide.Tests.E2E/Protocols/QuicTransportParameterTests.cs b/tests/Ioxide.Tests.E2E/Protocols/QuicTransportParameterTests.cs new file mode 100644 index 00000000..4ae59a7c --- /dev/null +++ b/tests/Ioxide.Tests.E2E/Protocols/QuicTransportParameterTests.cs @@ -0,0 +1,68 @@ +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 + && refused.CloseError == QuicSniHostileTests.CryptoError(QuicSniHostileTests.Alert.MissingExtension), + $"a ClientHello {label} must be refused with missing_extension, CRYPTO_ERROR 0x16d (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)}"); + }); + } + + runner.Test("quic: a ClientHello offering no ALPN the server serves is refused with no_application_protocol", () => + { + // RFC 9001 8.1. The alert is the shim's own choice (iq_on_client_hello), and the close carries it. + int udpPort = QuicSniHostileTests.Serve(QuicSniHostileTests.NewEngine()); + + using var raw = new RawInitial(udpPort); + RawInitial.Reply refused = raw.Exchange(QuicSniHostileTests.Hello(raw, [("alpha.test", 0)], alpn: "h2")); + + Assert.True(refused.Closed && !refused.SawServerHello + && refused.CloseError == QuicSniHostileTests.CryptoError(QuicSniHostileTests.Alert.NoApplicationProtocol), + $"a ClientHello offering only h2 to an h3 server must be refused with no_application_protocol, " + + $"CRYPTO_ERROR 0x178; instead the server {QuicSniHostileTests.Describe(refused)}"); + }); + } +}