diff --git a/deps/ngtcp2/ngtcp2/lib/ngtcp2_conn.c b/deps/ngtcp2/ngtcp2/lib/ngtcp2_conn.c index 374274b567c2..61f1ebe1d40d 100644 --- a/deps/ngtcp2/ngtcp2/lib/ngtcp2_conn.c +++ b/deps/ngtcp2/ngtcp2/lib/ngtcp2_conn.c @@ -6808,10 +6808,18 @@ conn_recv_handshake_pkt(ngtcp2_conn *conn, const ngtcp2_path *path, if (nread2 < 0) { return nread2; } + + return (ngtcp2_ssize)pktlen; } - /* Discard 0-RTT packet if we don't have a key to decrypt it. */ - return (ngtcp2_ssize)pktlen; + /* Discard 0-RTT packet if we don't have a key to decrypt it and + the ClientHello has been processed. Until then (the + ClientHello spans several Initial packets, or the TLS stack + interrupted the handshake), the 0-RTT key might still be + installed, so buffer the packet. */ + if (!conn->hs_pktns || conn->hs_pktns->crypto.rx.ckm) { + return (ngtcp2_ssize)pktlen; + } } /* Buffer re-ordered 0-RTT packet. */ diff --git a/doc/api/quic.md b/doc/api/quic.md index 234be7238cf2..c4bff19a6113 100644 --- a/doc/api/quic.md +++ b/doc/api/quic.md @@ -602,7 +602,9 @@ is not specified. * {string} The default TLS 1.3 key-exchange group list used when -[`sessionOptions.groups`][] is not specified. +[`sessionOptions.groups`][] is not specified. The list prefers the +`X25519MLKEM768` hybrid post-quantum group and sends key shares for both +`X25519MLKEM768` and `X25519`. ## Class: `QuicEndpoint` @@ -3284,11 +3286,16 @@ is available. Set to `false` to disable early data support. * Type: {string} -The list of supported TLS 1.3 cipher groups. +The colon-separated list of TLS 1.3 key-exchange groups, in the +OpenSSL group list format. **Default:** [`quic.constants.DEFAULT_GROUPS`][]. #### `sessionOptions.keylog` @@ -4901,6 +4908,7 @@ throughput issues caused by flow control. [`maxDatagramFrameSize`]: #transportparamsmaxdatagramframesize [`net.BlockList`]: net.md#class-netblocklist [`quic.connect()`]: #quicconnectaddress-options +[`quic.constants.DEFAULT_GROUPS`]: #quicconstantsdefault_groups [`quic.listen()`]: #quiclistenonsession-options [`session.close()`]: #sessioncloseoptions [`session.createBidirectionalStream()`]: #sessioncreatebidirectionalstreamoptions diff --git a/src/quic/tlscontext.h b/src/quic/tlscontext.h index db4c7e97843c..a85163842f29 100644 --- a/src/quic/tlscontext.h +++ b/src/quic/tlscontext.h @@ -211,7 +211,8 @@ class TLSContext final : public MemoryRetainer, "TLS_AES_256_GCM_SHA384:" "TLS_CHACHA20_POLY1305_" "SHA256:TLS_AES_128_CCM_SHA256"; - static constexpr auto DEFAULT_GROUPS = "X25519:P-256:P-384:P-521"; + static constexpr auto DEFAULT_GROUPS = + "?*X25519MLKEM768:*X25519:P-256:P-384:P-521"; struct Options final : public MemoryRetainer { // The SNI servername to use for this session. This option is only used by diff --git a/test/parallel/test-quic-exports.mjs b/test/parallel/test-quic-exports.mjs index 71771624e31e..043db3032694 100644 --- a/test/parallel/test-quic-exports.mjs +++ b/test/parallel/test-quic-exports.mjs @@ -27,7 +27,8 @@ assert.strictEqual(quic.constants.cc.BBR, 'bbr'); assert.strictEqual(quic.constants.DEFAULT_CIPHERS, 'TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:' + 'TLS_CHACHA20_POLY1305_SHA256:TLS_AES_128_CCM_SHA256'); -assert.strictEqual(quic.constants.DEFAULT_GROUPS, 'X25519:P-256:P-384:P-521'); +assert.strictEqual(quic.constants.DEFAULT_GROUPS, + '?*X25519MLKEM768:*X25519:P-256:P-384:P-521'); // Ensure the constants are.. well, constant. assert.throws(() => { quic.constants.cc.RENO = 'foo'; }, TypeError); diff --git a/test/parallel/test-quic-tls-default-groups.mjs b/test/parallel/test-quic-tls-default-groups.mjs new file mode 100644 index 000000000000..4c208ad21408 --- /dev/null +++ b/test/parallel/test-quic-tls-default-groups.mjs @@ -0,0 +1,41 @@ +// Flags: --experimental-quic --no-warnings + +// Test: the default groups let a client handshake with a server that +// accepts only X25519MLKEM768 and with a server that accepts only X25519. + +import { hasQuic, skip, mustCall } from '../common/index.mjs'; +import assert from 'node:assert'; +import * as fixtures from '../common/fixtures.mjs'; + +if (!hasQuic) { + skip('QUIC is not enabled'); +} + +const { listen, connect } = await import('node:quic'); +const { createPrivateKey } = await import('node:crypto'); + +const key = createPrivateKey(fixtures.readKey('agent1-key.pem')); +const cert = fixtures.readKey('agent1-cert.pem'); + +for (const groups of ['X25519MLKEM768', 'X25519']) { + const serverEndpoint = await listen(mustCall(async (serverSession) => { + await serverSession.opened; + serverSession.close(); + }), { + sni: { '*': { keys: [key], certs: [cert] } }, + alpn: ['quic-test'], + groups, + }); + + const clientSession = await connect(serverEndpoint.address, { + alpn: 'quic-test', + verifyPeer: 'manual', + servername: 'localhost', + }); + + const info = await clientSession.opened; + assert.strictEqual(info.cipherVersion, 'TLSv1.3'); + + await clientSession.closed; + await serverEndpoint.close(); +} diff --git a/test/parallel/test-quic-zero-rtt-split-client-hello.mjs b/test/parallel/test-quic-zero-rtt-split-client-hello.mjs new file mode 100644 index 000000000000..66126c4f8d3e --- /dev/null +++ b/test/parallel/test-quic-zero-rtt-split-client-hello.mjs @@ -0,0 +1,61 @@ +// Flags: --experimental-quic --no-warnings + +// Test: 0-RTT data is accepted when the ClientHello spans more than one +// Initial packet. The X25519MLKEM768 key share makes the ClientHello too +// large for one packet, and the 0-RTT packet is coalesced with the last +// Initial packet while the server's handshake is paused for the new +// session callback. A datagram is used because it is not retransmitted. + +import { hasQuic, skip, mustCall } from '../common/index.mjs'; +import assert from 'node:assert'; + +if (!hasQuic) { + skip('QUIC is not enabled'); +} + +const { listen, connect } = await import('../common/quic.mjs'); + +const groups = 'X25519MLKEM768'; +const transportParams = { maxDatagramFrameSize: 1200 }; + +const gotTicket = Promise.withResolvers(); +const gotToken = Promise.withResolvers(); +const serverGotDatagram = Promise.withResolvers(); + +let serverSessionCount = 0; +const serverEndpoint = await listen(mustCall((serverSession) => { + if (++serverSessionCount === 2) { + serverSession.ondatagram = mustCall((data, early) => { + serverGotDatagram.resolve({ data: Buffer.from(data), early }); + }); + } +}, 2), { groups, transportParams }); + +const cs1 = await connect(serverEndpoint.address, { + groups, + transportParams, + onsessionticket: mustCall((ticket) => gotTicket.resolve(ticket)), + onnewtoken: mustCall((token) => gotToken.resolve(token)), +}); +await cs1.opened; +const [sessionTicket, token] = + await Promise.all([gotTicket.promise, gotToken.promise]); +await cs1.close(); + +const cs2 = await connect(serverEndpoint.address, { + groups, + transportParams, + sessionTicket, + token, +}); +await cs2.sendDatagram(new Uint8Array([0xCA, 0xFE])); + +const info = await cs2.opened; +assert.strictEqual(info.earlyDataAccepted, true); + +const { data, early } = await serverGotDatagram.promise; +assert.deepStrictEqual(data, Buffer.from([0xCA, 0xFE])); +assert.strictEqual(early, true); + +await cs2.close(); +await serverEndpoint.close();