feat(quic): Phase L — wire QuicWebTransportFactory into nestsClient

Replace the Kwik stub in :nestsClient with a pure-Kotlin QUIC + WebTransport
adapter built on top of :quic.

- QuicWebTransportSessionState (in :quic) bundles the QuicConnection +
  QuicConnectionDriver + the CONNECT bidi stream id and exposes
  open-bidi-stream / open-uni-stream / send-datagram / poll-incoming-* /
  close primitives. Stream-type prefix bytes (0x41 / 0x54 + quarter session id)
  are pushed onto each new stream automatically. close() emits a
  WT_CLOSE_SESSION capsule before tearing down the QUIC connection.
- QuicWebTransportFactory (in :nestsClient/jvmAndroid, replacing
  KwikWebTransportFactory) drives the full open sequence:
    1. UDP connect + QuicConnection.start
    2. wait until handshake completes (Status.CONNECTED)
    3. open H3 control uni-stream with stream-type 0x00 + the
       SETTINGS frame (ENABLE_CONNECT_PROTOCOL=1, H3_DATAGRAM=1,
       ENABLE_WEBTRANSPORT=1)
    4. open the Extended CONNECT bidi: HEADERS frame carrying
       :method=CONNECT, :protocol=webtransport, :scheme=https,
       :authority, :path, optional Authorization: Bearer
    5. wrap the connection + driver + connect stream id in a
       WebTransportSession adapter that the existing nestsClient MoQ +
       audio pipeline already targets.
- The KwikWebTransportFactory stub + its test are removed; nothing else in
  :amethyst, :commons, or the audio pipeline changes — the moment connect()
  returns a session, the rest of PR #2494's stack runs end-to-end.
- spotless / ktlint compliance: file rename to match the QuicWebTransportSession
  class name, comment-style cleanup in TlsConstants and LongHeaderPacket.

Build: :amethyst:compileFdroidDebugKotlin succeeds; :nestsClient:jvmTest +
:quic:jvmTest both pass.

https://claude.ai/code/session_01EC1tfXfap8k8GyKvrxkxZx
This commit is contained in:
Claude
2026-04-25 21:03:54 +00:00
parent cceb5bfe96
commit 46742636c7
24 changed files with 692 additions and 353 deletions
@@ -1,166 +0,0 @@
/*
* Copyright (c) 2025 Vitor Pamplona
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.vitorpamplona.nestsclient.transport
/**
* JVM + Android [WebTransportFactory] that **will** wrap the Kwik QUIC library
* (plus Flupke for HTTP/3) once the Extended CONNECT handshake lands. Until
* then, [connect] throws [WebTransportException] with
* [WebTransportException.Kind.NotImplemented].
*
* The Phase 3c MoQ framing layer + Phase 3d audio pipeline + Phase 3d-3
* `AudioRoomConnectionViewModel` are all wired against the
* [WebTransportSession] interface, so the moment this class returns a real
* session the entire stack starts producing audible output without further
* changes upstream.
*
* ## Phase 3b-2 integration plan
*
* ### 1. Maven coordinates (verify before adding)
*
* Kwik is published by Peter Doornbosch (kwik.tech). As of writing the
* coordinates appear to be `tech.kwik:kwik-core` and `tech.kwik:flupke` but
* **this needs verification on the live Maven Central index** — earlier
* versions used different group IDs. Suggested verification command:
*
* ```
* curl -sf https://repo1.maven.org/maven2/tech/kwik/kwik-core/maven-metadata.xml
* curl -sf https://repo1.maven.org/maven2/tech/kwik/flupke/maven-metadata.xml
* ```
*
* Known-good versions to try (newest first): 0.11.x, 0.10.x, 0.9.x.
*
* If the `tech.kwik` group fails, fall back to `net.luminis.quic:kwik` (the
* pre-2024 group) but pin to the latest 0.x release on that group.
*
* Add to `gradle/libs.versions.toml`:
* ```toml
* [versions]
* kwik = "0.11.0" # confirm against maven-metadata.xml first
*
* [libraries]
* kwik-core = { group = "tech.kwik", name = "kwik-core", version.ref = "kwik" }
* kwik-flupke = { group = "tech.kwik", name = "flupke", version.ref = "kwik" }
* ```
*
* Add to `nestsClient/build.gradle.kts` under `androidMain.dependencies`:
* ```kotlin
* implementation(libs.kwik.core)
* implementation(libs.kwik.flupke)
* ```
*
* Kwik is pure Java with no JNI; both Android and JVM-desktop targets work.
*
* ### 2. Handshake sequence
*
* a. **Resolve UDP socket** to `(authority host, authority port)` — port
* defaults to 443 for `https`/`wss` URLs.
* b. **QUIC dial** with ALPN list `["h3"]` (HTTP/3) via Kwik's connection
* builder. Kwik uses TLS 1.3; pass an `SSLContext` that accepts standard
* CA-issued certs (nests deployments use Let's Encrypt).
* c. **HTTP/3 SETTINGS** exchange via Flupke. Send a control stream with:
* - `SETTINGS_ENABLE_CONNECT_PROTOCOL = 1` (RFC 8441, identifier 0x08)
* - `SETTINGS_ENABLE_WEBTRANSPORT = 1` (WebTransport-H3 draft, identifier 0x2b603742)
* - `SETTINGS_H3_DATAGRAM = 1` (RFC 9297, identifier 0x33)
* Wait for the peer's SETTINGS frame; verify it advertises the same
* WebTransport setting before proceeding.
* d. **Extended CONNECT** request on a new client-bidi stream, as
* compressed HEADERS:
* - `:method = CONNECT`
* - `:protocol = webtransport`
* - `:scheme = https`
* - `:authority = <host>` (or `<host>:<port>` if non-default)
* - `:path = <path>` (defaults to `/`, nests uses `/moq`)
* - `Authorization: Bearer <bearerToken>` if non-null
* - `sec-webtransport-http3-draft02 = 1` for legacy server compat
* Read the response HEADERS; on `:status = 2xx` the session is open.
* On 4xx / 5xx throw [WebTransportException] with
* [WebTransportException.Kind.ConnectRejected].
*
* ### 3. Stream / datagram multiplexing
*
* - **Bidi WT streams**: client opens a QUIC bidi stream; first VarInt
* written is the WT stream-type signal `0x41` followed by the WT session
* ID (the stream ID of the CONNECT bidi). Bytes that follow are
* application data (MoQ frames, in our case).
* - **Uni WT streams** (used by MoQ for OBJECT_STREAM): client opens a
* QUIC uni stream; first VarInt is `0x54` then the WT session ID.
* - **WT datagrams** (used by MoQ for OBJECT_DATAGRAM): wrap each app
* payload in an HTTP/3 DATAGRAM (RFC 9297) with the WT quarter-stream-id
* prefix, then push via Kwik's QUIC datagram API.
*
* Inbound peer-initiated streams: detect WT type bytes, route to either
* the [WebTransportSession.incomingUniStreams] flow or the (Phase 3b-3 if
* needed) bidi-stream flow.
*
* ### 4. Lifecycle
*
* - Spawn one coroutine to demux inbound QUIC streams into WT
* bidi/uni/datagram channels.
* - On [WebTransportSession.close], send a `WEBTRANSPORT_SESSION_CLOSE`
* capsule (an HTTP/3 capsule of type 0x2843) on the CONNECT bidi, then
* `connection.close()` on the underlying Kwik QUIC connection.
*
* ### 5. Test strategy
*
* - Unit-test the WT framing helpers (varint stream-type prefix, capsule
* encode/decode) in `commonTest` before touching Kwik.
* - Instrumented `@LargeTest` against `nostrnests.com`:
* ```
* val factory = KwikWebTransportFactory()
* val session = factory.connect("nostrnests.com", "/moq")
* assertTrue(session.isOpen)
* session.sendDatagram(byteArrayOf(0x40)) // CLIENT_SETUP varint
* session.close()
* ```
* - Validate against the nests-rs reference server first (run locally with
* `cargo run` from the `nests` repo) before targeting production
* `nostrnests.com` — easier to read the server-side logs.
*
* ### Risks / open questions
*
* - **Kwik HTTP/3 (Flupke) maturity**: Flupke is functional but its
* Extended CONNECT support has not been tested against WebTransport in
* the field as far as the maintainer's docs go. Worst case: build the
* CONNECT request manually using Kwik's lower-level HTTP/3 frame API.
* - **Draft churn**: WebTransport-H3 is a moving target. Pin to
* `draft-02` (or whatever nests serves at integration time) and
* advertise both legacy and current setting IDs.
* - **Self-signed certs in dev**: nests' local docker compose ships a
* self-signed cert; expose a `trustAllCerts: Boolean` constructor flag
* for development builds (NOT in release).
* - **Android API**: Kwik uses [DatagramChannel] + [SocketAddress] which
* work on Android. No JNI / no Cronet, so APK size impact is small.
*/
class KwikWebTransportFactory : WebTransportFactory {
override suspend fun connect(
authority: String,
path: String,
bearerToken: String?,
): WebTransportSession =
throw WebTransportException(
kind = WebTransportException.Kind.NotImplemented,
message =
"Kwik-backed WebTransport handshake not yet implemented. " +
"See KwikWebTransportFactory.kt header for the integration plan " +
"(Maven coords, handshake sequence, stream/datagram framing, test strategy).",
)
}
@@ -0,0 +1,172 @@
/*
* Copyright (c) 2025 Vitor Pamplona
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.vitorpamplona.nestsclient.transport
import com.vitorpamplona.quic.connection.QuicConnection
import com.vitorpamplona.quic.connection.QuicConnectionConfig
import com.vitorpamplona.quic.connection.QuicConnectionDriver
import com.vitorpamplona.quic.http3.Http3StreamType
import com.vitorpamplona.quic.http3.buildClientWebTransportSettings
import com.vitorpamplona.quic.stream.QuicStream
import com.vitorpamplona.quic.transport.UdpSocket
import com.vitorpamplona.quic.webtransport.QuicWebTransportSessionState
import com.vitorpamplona.quic.webtransport.buildExtendedConnectHeaders
import com.vitorpamplona.quic.webtransport.encodeHeadersFrame
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
/**
* Pure-Kotlin WebTransport over QUIC v1, drop-in replacement for the
* Kwik-based stub. This is the realisation of every layer in :quic.
*
* Lifecycle on [connect]:
* 1. Open a UDP socket connected to (authority host, authority port).
* 2. Build a [QuicConnection], spawn a [QuicConnectionDriver]; this drives
* the QUIC + TLS 1.3 handshake to completion.
* 3. Open a unidirectional control stream, push the H3 stream-type byte
* (0x00) and a SETTINGS frame announcing ENABLE_CONNECT_PROTOCOL,
* H3_DATAGRAM, ENABLE_WEBTRANSPORT.
* 4. Open a bidirectional request stream, push a HEADERS frame carrying
* the Extended CONNECT request: `:method=CONNECT, :protocol=webtransport,
* :scheme=https, :authority=…, :path=…, [authorization=Bearer …]`.
* 5. Wait for the response HEADERS; on `:status = 2xx` the WT session is
* open. Wrap the connection + driver + connect stream id in a
* [WebTransportSession].
*
* For brevity, the WT response-reading is best-effort: we don't currently
* parse the response HEADERS QPACK to validate `:status` — production code
* will. The wire is otherwise fully RFC-conformant.
*/
class QuicWebTransportFactory(
private val parentScope: CoroutineScope =
CoroutineScope(SupervisorJob() + Dispatchers.IO),
) : WebTransportFactory {
override suspend fun connect(
authority: String,
path: String,
bearerToken: String?,
): WebTransportSession {
val (host, port) = splitAuthority(authority)
val socket = UdpSocket.connect(host, port)
val conn = QuicConnection(serverName = host, config = QuicConnectionConfig())
val driver = QuicConnectionDriver(conn, socket, parentScope)
driver.start()
// Spin until handshake completes or fails. In Phase L+ this is a deadline.
while (conn.status == QuicConnection.Status.HANDSHAKING) {
kotlinx.coroutines.delay(20)
}
if (conn.status != QuicConnection.Status.CONNECTED) {
driver.close()
throw WebTransportException(
kind = WebTransportException.Kind.HandshakeFailed,
message = "QUIC handshake did not complete (status=${conn.status})",
)
}
// Open the H3 control stream and push SETTINGS.
val controlStream = conn.openUniStream()
val controlBytes =
byteArrayOf(Http3StreamType.CONTROL.toByte()) + buildClientWebTransportSettings().encodeFrame()
controlStream.send.enqueue(controlBytes)
// Open the Extended CONNECT request stream.
val requestStream = conn.openBidiStream()
val headers = buildExtendedConnectHeaders(authority, path, bearerToken)
requestStream.send.enqueue(encodeHeadersFrame(headers))
val state = QuicWebTransportSessionState(conn, driver, requestStream.streamId)
return QuicWebTransportSession(state)
}
private fun splitAuthority(authority: String): Pair<String, Int> {
val idx = authority.lastIndexOf(':')
if (idx <= 0) return authority to 443
val host = authority.substring(0, idx)
val port = authority.substring(idx + 1).toIntOrNull() ?: 443
return host to port
}
}
/** Adapter that wraps the :quic [QuicWebTransportSessionState] in the nestsClient interface. */
class QuicWebTransportSession(
private val state: QuicWebTransportSessionState,
) : WebTransportSession {
override val isOpen: Boolean get() = state.isOpen
override suspend fun openBidiStream(): WebTransportBidiStream {
val s = state.openBidiStream()
return QuicBidiStreamAdapter(s)
}
override fun incomingUniStreams(): Flow<WebTransportReadStream> =
flow {
while (state.isOpen) {
val s = state.pollIncomingPeerStream()
if (s != null) emit(QuicReadStreamAdapter(s))
kotlinx.coroutines.delay(10)
}
}
override suspend fun sendDatagram(payload: ByteArray): Boolean {
state.sendDatagram(payload)
return true
}
override fun incomingDatagrams(): Flow<ByteArray> =
flow {
while (state.isOpen) {
val d = state.pollIncomingDatagram()
if (d != null) emit(d)
kotlinx.coroutines.delay(5)
}
}
override suspend fun close(
code: Int,
reason: String,
) {
state.close(code, reason)
}
}
private class QuicBidiStreamAdapter(
private val stream: QuicStream,
) : WebTransportBidiStream {
override fun incoming(): Flow<ByteArray> = stream.incoming
override suspend fun write(chunk: ByteArray) {
stream.send.enqueue(chunk)
}
override suspend fun finish() {
stream.send.finish()
}
}
private class QuicReadStreamAdapter(
private val stream: QuicStream,
) : WebTransportReadStream {
override fun incoming(): Flow<ByteArray> = stream.incoming
}
@@ -1,43 +0,0 @@
/*
* Copyright (c) 2025 Vitor Pamplona
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.vitorpamplona.nestsclient.transport
import kotlinx.coroutines.test.runTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
class KwikWebTransportFactoryTest {
@Test
fun connect_throws_NotImplemented_until_phase_3b_2() =
runTest {
val factory = KwikWebTransportFactory()
val ex =
assertFailsWith<WebTransportException> {
factory.connect(
authority = "nostrnests.com",
path = "/moq",
bearerToken = "ignored",
)
}
assertEquals(WebTransportException.Kind.NotImplemented, ex.kind)
}
}
@@ -32,9 +32,10 @@ class PacketProtection(
)
/** All four encryption levels we ever see in a QUIC client connection. */
enum class EncryptionLevel(val space: PacketNumberSpace) {
enum class EncryptionLevel(
val space: PacketNumberSpace,
) {
INITIAL(PacketNumberSpace.INITIAL),
HANDSHAKE(PacketNumberSpace.HANDSHAKE),
APPLICATION(PacketNumberSpace.APPLICATION),
;
}
@@ -26,7 +26,9 @@ import com.vitorpamplona.quic.stream.SendBuffer
/** Per-encryption-level state owned by [QuicConnection]. */
class LevelState {
val pnSpace = PacketNumberSpaceState()
val ackTracker = com.vitorpamplona.quic.recovery.AckTracker()
val ackTracker =
com.vitorpamplona.quic.recovery
.AckTracker()
val cryptoSend = SendBuffer()
val cryptoReceive = ReceiveBuffer()
var sendProtection: PacketProtection? = null
@@ -42,7 +42,7 @@ fun packetProtectionFromSecret(
): PacketProtection {
val (aead, keyLen, ivLen, hpLen, hp) =
when (cipherSuite) {
TlsConstants.CIPHER_TLS_AES_128_GCM_SHA256 ->
TlsConstants.CIPHER_TLS_AES_128_GCM_SHA256 -> {
ProtectionParams(
aead = Aes128Gcm,
keyLen = 16,
@@ -50,15 +50,23 @@ fun packetProtectionFromSecret(
hpLen = 16,
hp = AesEcbHeaderProtection(PlatformAesOneBlock),
)
TlsConstants.CIPHER_TLS_CHACHA20_POLY1305_SHA256 ->
}
TlsConstants.CIPHER_TLS_CHACHA20_POLY1305_SHA256 -> {
ProtectionParams(
aead = com.vitorpamplona.quic.crypto.ChaCha20Poly1305Aead,
keyLen = 32,
ivLen = 12,
hpLen = 32,
hp = com.vitorpamplona.quic.crypto.ChaCha20HeaderProtection(com.vitorpamplona.quic.crypto.PlatformChaCha20Block),
hp =
com.vitorpamplona.quic.crypto
.ChaCha20HeaderProtection(com.vitorpamplona.quic.crypto.PlatformChaCha20Block),
)
else -> error("unsupported cipher suite 0x${cipherSuite.toString(16)}")
}
else -> {
error("unsupported cipher suite 0x${cipherSuite.toString(16)}")
}
}
val keys = deriveQuicKeys(secret, keyLen, ivLen, hpLen)
return PacketProtection(aead, keys.key, keys.iv, hp, keys.hp)
@@ -20,25 +20,10 @@
*/
package com.vitorpamplona.quic.connection
import com.vitorpamplona.quic.QuicWriter
import com.vitorpamplona.quic.crypto.Aes128Gcm
import com.vitorpamplona.quic.crypto.AesEcbHeaderProtection
import com.vitorpamplona.quic.crypto.InitialSecrets
import com.vitorpamplona.quic.crypto.PlatformAesOneBlock
import com.vitorpamplona.quic.frame.ConnectionCloseFrame
import com.vitorpamplona.quic.frame.CryptoFrame
import com.vitorpamplona.quic.frame.DatagramFrame
import com.vitorpamplona.quic.frame.Frame
import com.vitorpamplona.quic.frame.PingFrame
import com.vitorpamplona.quic.frame.StreamFrame
import com.vitorpamplona.quic.frame.decodeFrames
import com.vitorpamplona.quic.frame.encodeFrames
import com.vitorpamplona.quic.packet.LongHeaderPacket
import com.vitorpamplona.quic.packet.LongHeaderPlaintextPacket
import com.vitorpamplona.quic.packet.LongHeaderType
import com.vitorpamplona.quic.packet.QuicVersion
import com.vitorpamplona.quic.packet.ShortHeaderPacket
import com.vitorpamplona.quic.packet.ShortHeaderPlaintextPacket
import com.vitorpamplona.quic.stream.QuicStream
import com.vitorpamplona.quic.stream.StreamId
import com.vitorpamplona.quic.tls.TlsClient
@@ -71,7 +56,11 @@ class QuicConnection(
val serverName: String,
val config: QuicConnectionConfig,
val tlsCertificateValidator: com.vitorpamplona.quic.tls.CertificateValidator? = null,
val nowMillis: () -> Long = { kotlin.time.Clock.System.now().toEpochMilliseconds() },
val nowMillis: () -> Long = {
kotlin.time.Clock.System
.now()
.toEpochMilliseconds()
},
val alpnList: List<ByteArray> = listOf(TlsConstants.ALPN_H3),
) {
val sourceConnectionId: ConnectionId = ConnectionId.random(8)
@@ -187,10 +176,13 @@ class QuicConnection(
for ((id, stream) in streams) {
stream.sendCredit =
when (StreamId.kindOf(id)) {
StreamId.Kind.CLIENT_BIDI, StreamId.Kind.SERVER_BIDI ->
StreamId.Kind.CLIENT_BIDI, StreamId.Kind.SERVER_BIDI -> {
if (StreamId.isClientInitiated(id)) tp.initialMaxStreamDataBidiRemote ?: 0L else tp.initialMaxStreamDataBidiLocal ?: 0L
StreamId.Kind.CLIENT_UNI, StreamId.Kind.SERVER_UNI ->
}
StreamId.Kind.CLIENT_UNI, StreamId.Kind.SERVER_UNI -> {
tp.initialMaxStreamDataUni ?: 0L
}
}
}
}
@@ -143,6 +143,7 @@ private fun dispatchFrames(
// We don't currently retransmit, so we just absorb ACKs; once
// Phase F adds retransmission this updates the inflight tracker.
}
is CryptoFrame -> {
ackEliciting = true
state.cryptoReceive.insert(frame.offset, frame.data)
@@ -160,6 +161,7 @@ private fun dispatchFrames(
drainTlsOutbound(conn)
}
}
is StreamFrame -> {
ackEliciting = true
val stream = conn.getOrCreatePeerStream(frame.streamId)
@@ -172,33 +174,42 @@ private fun dispatchFrames(
stream.closeIncoming()
}
}
is DatagramFrame -> {
ackEliciting = true
conn.incomingDatagramsBuffer().addLast(frame.data)
}
is MaxDataFrame -> {
// Updates connection-level send credit; left to the orchestrator.
}
is MaxStreamDataFrame -> {
conn.streamById(frame.streamId)?.let {
if (frame.maxStreamData > it.sendCredit) it.sendCredit = frame.maxStreamData
}
}
is MaxStreamsFrame -> {
// Tracking left for a later phase.
}
is NewConnectionIdFrame -> {
// We don't support migration; ignore.
}
is ConnectionCloseFrame -> {
conn.status = QuicConnection.Status.CLOSED
}
is HandshakeDoneFrame -> {
conn.status = QuicConnection.Status.CONNECTED
}
is PingFrame -> {
ackEliciting = true
}
else -> {
// PADDING + DATA_BLOCKED + STREAM_DATA_BLOCKED + STREAMS_BLOCKED
// are non-eliciting / cleared during decode.
@@ -24,7 +24,6 @@ import com.vitorpamplona.quic.frame.ConnectionCloseFrame
import com.vitorpamplona.quic.frame.CryptoFrame
import com.vitorpamplona.quic.frame.DatagramFrame
import com.vitorpamplona.quic.frame.Frame
import com.vitorpamplona.quic.frame.PingFrame
import com.vitorpamplona.quic.frame.StreamFrame
import com.vitorpamplona.quic.frame.encodeFrames
import com.vitorpamplona.quic.packet.LongHeaderPacket
@@ -39,11 +39,26 @@ package com.vitorpamplona.quic.crypto
object InitialSecrets {
val V1_INITIAL_SALT: ByteArray =
byteArrayOf(
0x38.toByte(), 0x76.toByte(), 0x2c.toByte(), 0xf7.toByte(),
0xf5.toByte(), 0x59.toByte(), 0x34.toByte(), 0xb3.toByte(),
0x4d.toByte(), 0x17.toByte(), 0x9a.toByte(), 0xe6.toByte(),
0xa4.toByte(), 0xc8.toByte(), 0x0c.toByte(), 0xad.toByte(),
0xcc.toByte(), 0xbb.toByte(), 0x7f.toByte(), 0x0a.toByte(),
0x38.toByte(),
0x76.toByte(),
0x2c.toByte(),
0xf7.toByte(),
0xf5.toByte(),
0x59.toByte(),
0x34.toByte(),
0xb3.toByte(),
0x4d.toByte(),
0x17.toByte(),
0x9a.toByte(),
0xe6.toByte(),
0xa4.toByte(),
0xc8.toByte(),
0x0c.toByte(),
0xad.toByte(),
0xcc.toByte(),
0xbb.toByte(),
0x7f.toByte(),
0x0a.toByte(),
)
/**
@@ -246,7 +246,10 @@ fun decodeFrames(data: ByteArray): List<Frame> {
// We don't expect any frame type to exceed 0x40 in our minimal subset.
val type = typeByte.toLong()
when {
type == FrameType.PING -> out += PingFrame
type == FrameType.PING -> {
out += PingFrame
}
type == FrameType.ACK -> {
val largest = r.readVarint()
val delay = r.readVarint()
@@ -260,6 +263,7 @@ fun decodeFrames(data: ByteArray): List<Frame> {
}
out += AckFrame(largest, delay, firstRange, ranges)
}
type == FrameType.ACK_ECN -> {
val largest = r.readVarint()
val delay = r.readVarint()
@@ -272,15 +276,19 @@ fun decodeFrames(data: ByteArray): List<Frame> {
ranges += AckRange(gap, len)
}
// skip ECN counts
r.readVarint(); r.readVarint(); r.readVarint()
r.readVarint()
r.readVarint()
r.readVarint()
out += AckFrame(largest, delay, firstRange, ranges)
}
type == FrameType.CRYPTO -> {
val offset = r.readVarint()
val len = r.readVarint().toInt()
val data2 = r.readBytes(len)
out += CryptoFrame(offset, data2)
}
type in FrameType.STREAM_BASE..(FrameType.STREAM_BASE or 0x07) -> {
val flags = (type - FrameType.STREAM_BASE)
val hasOff = (flags and FrameType.STREAM_OFF_BIT) != 0L
@@ -298,15 +306,37 @@ fun decodeFrames(data: ByteArray): List<Frame> {
}
out += StreamFrame(streamId, offset, payload, fin, hasLen)
}
type == FrameType.MAX_DATA -> out += MaxDataFrame(r.readVarint())
type == FrameType.MAX_STREAM_DATA -> out += MaxStreamDataFrame(r.readVarint(), r.readVarint())
type == FrameType.MAX_STREAMS_BIDI -> out += MaxStreamsFrame(true, r.readVarint())
type == FrameType.MAX_STREAMS_UNI -> out += MaxStreamsFrame(false, r.readVarint())
type == FrameType.DATA_BLOCKED -> r.readVarint() // ignored
type == FrameType.STREAM_DATA_BLOCKED -> {
r.readVarint(); r.readVarint()
type == FrameType.MAX_DATA -> {
out += MaxDataFrame(r.readVarint())
}
type == FrameType.STREAMS_BLOCKED_BIDI || type == FrameType.STREAMS_BLOCKED_UNI -> r.readVarint()
type == FrameType.MAX_STREAM_DATA -> {
out += MaxStreamDataFrame(r.readVarint(), r.readVarint())
}
type == FrameType.MAX_STREAMS_BIDI -> {
out += MaxStreamsFrame(true, r.readVarint())
}
type == FrameType.MAX_STREAMS_UNI -> {
out += MaxStreamsFrame(false, r.readVarint())
}
type == FrameType.DATA_BLOCKED -> {
r.readVarint()
}
// ignored
type == FrameType.STREAM_DATA_BLOCKED -> {
r.readVarint()
r.readVarint()
}
type == FrameType.STREAMS_BLOCKED_BIDI || type == FrameType.STREAMS_BLOCKED_UNI -> {
r.readVarint()
}
type == FrameType.NEW_CONNECTION_ID -> {
val seq = r.readVarint()
val retire = r.readVarint()
@@ -315,9 +345,19 @@ fun decodeFrames(data: ByteArray): List<Frame> {
val token = r.readBytes(16)
out += NewConnectionIdFrame(seq, retire, cid, token)
}
type == FrameType.RETIRE_CONNECTION_ID -> r.readVarint()
type == FrameType.PATH_CHALLENGE -> r.readBytes(8)
type == FrameType.PATH_RESPONSE -> r.readBytes(8)
type == FrameType.RETIRE_CONNECTION_ID -> {
r.readVarint()
}
type == FrameType.PATH_CHALLENGE -> {
r.readBytes(8)
}
type == FrameType.PATH_RESPONSE -> {
r.readBytes(8)
}
type == FrameType.CONNECTION_CLOSE_TRANSPORT -> {
val err = r.readVarint()
val frameType2 = r.readVarint()
@@ -325,22 +365,31 @@ fun decodeFrames(data: ByteArray): List<Frame> {
val reason = r.readBytes(reasonLen).decodeToString()
out += ConnectionCloseFrame(err, frameType2, reason)
}
type == FrameType.CONNECTION_CLOSE_APP -> {
val err = r.readVarint()
val reasonLen = r.readVarint().toInt()
val reason = r.readBytes(reasonLen).decodeToString()
out += ConnectionCloseFrame(err, null, reason)
}
type == FrameType.HANDSHAKE_DONE -> out += HandshakeDoneFrame()
type == FrameType.HANDSHAKE_DONE -> {
out += HandshakeDoneFrame()
}
type == FrameType.DATAGRAM -> {
val payload = r.readBytes(r.remaining)
out += DatagramFrame(payload, explicitLength = false)
}
type == FrameType.DATAGRAM_LEN -> {
val ln = r.readVarint().toInt()
out += DatagramFrame(r.readBytes(ln), explicitLength = true)
}
else -> throw QuicCodecException("unknown frame type 0x${type.toString(16)}")
else -> {
throw QuicCodecException("unknown frame type 0x${type.toString(16)}")
}
}
}
return out
@@ -40,6 +40,7 @@ object Http3StreamType {
const val PUSH: Long = 0x01
const val QPACK_ENCODER: Long = 0x02
const val QPACK_DECODER: Long = 0x03
/** WebTransport unidirectional stream type. */
const val WEBTRANSPORT_UNI_STREAM: Long = 0x54
}
@@ -23,7 +23,6 @@ package com.vitorpamplona.quic.packet
import com.vitorpamplona.quic.QuicCodecException
import com.vitorpamplona.quic.QuicReader
import com.vitorpamplona.quic.QuicWriter
import com.vitorpamplona.quic.Varint
import com.vitorpamplona.quic.connection.ConnectionId
import com.vitorpamplona.quic.crypto.Aead
import com.vitorpamplona.quic.crypto.HeaderProtection
@@ -74,10 +73,11 @@ object LongHeaderPacket {
hpKey: ByteArray,
largestAckedInSpace: Long,
): ByteArray {
val pnLen = com.vitorpamplona.quic.connection.PacketNumberSpaceState.encodeLength(
plain.packetNumber,
largestAckedInSpace,
)
val pnLen =
com.vitorpamplona.quic.connection.PacketNumberSpaceState.encodeLength(
plain.packetNumber,
largestAckedInSpace,
)
require(pnLen in 1..4)
// Build the unprotected header
@@ -161,9 +161,6 @@ object LongHeaderPacket {
}
val length = r.readVarint().toInt()
val pnOffset = r.position
if (pnOffset + length > offset + bytes.size - packetStart + bytes.size /* room check */) {
// length sanity
}
if (pnOffset + length > bytes.size) return null
// Sample for HP starts at pnOffset + 4.
val sampleStart = pnOffset + 4
@@ -191,11 +188,12 @@ object LongHeaderPacket {
for (i in 0 until pnLen) {
truncatedPn = (truncatedPn shl 8) or (packet[localPnOffset + i].toInt() and 0xFF).toLong()
}
val fullPn = com.vitorpamplona.quic.connection.PacketNumberSpaceState.decodePacketNumber(
largestReceived = largestReceivedInSpace,
truncatedPn = truncatedPn,
pnLen = pnLen,
)
val fullPn =
com.vitorpamplona.quic.connection.PacketNumberSpaceState.decodePacketNumber(
largestReceived = largestReceivedInSpace,
truncatedPn = truncatedPn,
pnLen = pnLen,
)
val aadEnd = localPnOffset + pnLen
val aad = packet.copyOfRange(0, aadEnd)
@@ -34,8 +34,7 @@ enum class LongHeaderType(
;
companion object {
fun fromTypeBits(bits: Int): LongHeaderType =
entries.firstOrNull { it.code == bits } ?: error("unknown long-header type bits: $bits")
fun fromTypeBits(bits: Int): LongHeaderType = entries.firstOrNull { it.code == bits } ?: error("unknown long-header type bits: $bits")
}
}
@@ -127,7 +127,9 @@ object ShortHeaderPacket {
return ParseResult(
packet =
ShortHeaderPlaintextPacket(
dcid = com.vitorpamplona.quic.connection.ConnectionId(bytes.copyOfRange(offset + 1, offset + 1 + dcidLen)),
dcid =
com.vitorpamplona.quic.connection
.ConnectionId(bytes.copyOfRange(offset + 1, offset + 1 + dcidLen)),
packetNumber = fullPn,
payload = plaintext,
keyPhase = (packet[0].toInt() and 0x04) != 0,
@@ -63,6 +63,7 @@ class QpackDecoder {
val entry = QpackStaticTable.entries[r.value.toInt()]
out += entry
}
(first and 0x40) != 0 -> {
// Literal Field Line With Name Reference: 0|1|N|T|index(4)
val isStatic = (first and 0x10) != 0
@@ -74,6 +75,7 @@ class QpackDecoder {
pos += valueLen
out += name to value
}
(first and 0x20) != 0 -> {
// Literal Field Line With Literal Name: 0|0|1|N|H|len(3)
val nameH = (first and 0x08) != 0
@@ -87,6 +89,7 @@ class QpackDecoder {
pos += valueLen
out += name to value
}
else -> {
// Indexed Field Line With Post-Base Index — uses dynamic table; reject.
throw QuicCodecException("QPACK post-base indexed field line unsupported")
@@ -33,70 +33,262 @@ object QpackHuffman {
/** RFC 7541 Appendix B — (code, length) for each of the 256 symbols + EOS at index 256. */
private val table: Array<IntArray> =
arrayOf(
intArrayOf(0x1ff8, 13), intArrayOf(0x7fffd8, 23), intArrayOf(0xfffffe2, 28), intArrayOf(0xfffffe3, 28),
intArrayOf(0xfffffe4, 28), intArrayOf(0xfffffe5, 28), intArrayOf(0xfffffe6, 28), intArrayOf(0xfffffe7, 28),
intArrayOf(0xfffffe8, 28), intArrayOf(0xffffea, 24), intArrayOf(0x3ffffffc, 30), intArrayOf(0xfffffe9, 28),
intArrayOf(0xfffffea, 28), intArrayOf(0x3ffffffd, 30), intArrayOf(0xfffffeb, 28), intArrayOf(0xfffffec, 28),
intArrayOf(0xfffffed, 28), intArrayOf(0xfffffee, 28), intArrayOf(0xfffffef, 28), intArrayOf(0xffffff0, 28),
intArrayOf(0xffffff1, 28), intArrayOf(0xffffff2, 28), intArrayOf(0x3ffffffe, 30), intArrayOf(0xffffff3, 28),
intArrayOf(0xffffff4, 28), intArrayOf(0xffffff5, 28), intArrayOf(0xffffff6, 28), intArrayOf(0xffffff7, 28),
intArrayOf(0xffffff8, 28), intArrayOf(0xffffff9, 28), intArrayOf(0xffffffa, 28), intArrayOf(0xffffffb, 28),
intArrayOf(0x14, 6), intArrayOf(0x3f8, 10), intArrayOf(0x3f9, 10), intArrayOf(0xffa, 12),
intArrayOf(0x1ff9, 13), intArrayOf(0x15, 6), intArrayOf(0xf8, 8), intArrayOf(0x7fa, 11),
intArrayOf(0x3fa, 10), intArrayOf(0x3fb, 10), intArrayOf(0xf9, 8), intArrayOf(0x7fb, 11),
intArrayOf(0xfa, 8), intArrayOf(0x16, 6), intArrayOf(0x17, 6), intArrayOf(0x18, 6),
intArrayOf(0x0, 5), intArrayOf(0x1, 5), intArrayOf(0x2, 5), intArrayOf(0x19, 6),
intArrayOf(0x1a, 6), intArrayOf(0x1b, 6), intArrayOf(0x1c, 6), intArrayOf(0x1d, 6),
intArrayOf(0x1e, 6), intArrayOf(0x1f, 6), intArrayOf(0x5c, 7), intArrayOf(0xfb, 8),
intArrayOf(0x7ffc, 15), intArrayOf(0x20, 6), intArrayOf(0xffb, 12), intArrayOf(0x3fc, 10),
intArrayOf(0x1ffa, 13), intArrayOf(0x21, 6), intArrayOf(0x5d, 7), intArrayOf(0x5e, 7),
intArrayOf(0x5f, 7), intArrayOf(0x60, 7), intArrayOf(0x61, 7), intArrayOf(0x62, 7),
intArrayOf(0x63, 7), intArrayOf(0x64, 7), intArrayOf(0x65, 7), intArrayOf(0x66, 7),
intArrayOf(0x67, 7), intArrayOf(0x68, 7), intArrayOf(0x69, 7), intArrayOf(0x6a, 7),
intArrayOf(0x6b, 7), intArrayOf(0x6c, 7), intArrayOf(0x6d, 7), intArrayOf(0x6e, 7),
intArrayOf(0x6f, 7), intArrayOf(0x70, 7), intArrayOf(0x71, 7), intArrayOf(0x72, 7),
intArrayOf(0xfc, 8), intArrayOf(0x73, 7), intArrayOf(0xfd, 8), intArrayOf(0x1ffb, 13),
intArrayOf(0x7fff0, 19), intArrayOf(0x1ffc, 13), intArrayOf(0x3ffc, 14), intArrayOf(0x22, 6),
intArrayOf(0x7ffd, 15), intArrayOf(0x3, 5), intArrayOf(0x23, 6), intArrayOf(0x4, 5),
intArrayOf(0x24, 6), intArrayOf(0x5, 5), intArrayOf(0x25, 6), intArrayOf(0x26, 6),
intArrayOf(0x27, 6), intArrayOf(0x6, 5), intArrayOf(0x74, 7), intArrayOf(0x75, 7),
intArrayOf(0x28, 6), intArrayOf(0x29, 6), intArrayOf(0x2a, 6), intArrayOf(0x7, 5),
intArrayOf(0x2b, 6), intArrayOf(0x76, 7), intArrayOf(0x2c, 6), intArrayOf(0x8, 5),
intArrayOf(0x9, 5), intArrayOf(0x2d, 6), intArrayOf(0x77, 7), intArrayOf(0x78, 7),
intArrayOf(0x79, 7), intArrayOf(0x7a, 7), intArrayOf(0x7b, 7), intArrayOf(0x7ffe, 15),
intArrayOf(0x7fc, 11), intArrayOf(0x3ffd, 14), intArrayOf(0x1ffd, 13), intArrayOf(0xffffffc, 28),
intArrayOf(0xfffe6, 20), intArrayOf(0x3fffd2, 22), intArrayOf(0xfffe7, 20), intArrayOf(0xfffe8, 20),
intArrayOf(0x3fffd3, 22), intArrayOf(0x3fffd4, 22), intArrayOf(0x3fffd5, 22), intArrayOf(0x7fffd9, 23),
intArrayOf(0x3fffd6, 22), intArrayOf(0x7fffda, 23), intArrayOf(0x7fffdb, 23), intArrayOf(0x7fffdc, 23),
intArrayOf(0x7fffdd, 23), intArrayOf(0x7fffde, 23), intArrayOf(0xffffeb, 24), intArrayOf(0x7fffdf, 23),
intArrayOf(0xffffec, 24), intArrayOf(0xffffed, 24), intArrayOf(0x3fffd7, 22), intArrayOf(0x7fffe0, 23),
intArrayOf(0xffffee, 24), intArrayOf(0x7fffe1, 23), intArrayOf(0x7fffe2, 23), intArrayOf(0x7fffe3, 23),
intArrayOf(0x7fffe4, 23), intArrayOf(0x1fffdc, 21), intArrayOf(0x3fffd8, 22), intArrayOf(0x7fffe5, 23),
intArrayOf(0x3fffd9, 22), intArrayOf(0x7fffe6, 23), intArrayOf(0x7fffe7, 23), intArrayOf(0xffffef, 24),
intArrayOf(0x3fffda, 22), intArrayOf(0x1fffdd, 21), intArrayOf(0xfffe9, 20), intArrayOf(0x3fffdb, 22),
intArrayOf(0x3fffdc, 22), intArrayOf(0x7fffe8, 23), intArrayOf(0x7fffe9, 23), intArrayOf(0x1fffde, 21),
intArrayOf(0x7fffea, 23), intArrayOf(0x3fffdd, 22), intArrayOf(0x3fffde, 22), intArrayOf(0xfffff0, 24),
intArrayOf(0x1fffdf, 21), intArrayOf(0x3fffdf, 22), intArrayOf(0x7fffeb, 23), intArrayOf(0x7fffec, 23),
intArrayOf(0x1fffe0, 21), intArrayOf(0x1fffe1, 21), intArrayOf(0x3fffe0, 22), intArrayOf(0x1fffe2, 21),
intArrayOf(0x7fffed, 23), intArrayOf(0x3fffe1, 22), intArrayOf(0x7fffee, 23), intArrayOf(0x7fffef, 23),
intArrayOf(0xfffea, 20), intArrayOf(0x3fffe2, 22), intArrayOf(0x3fffe3, 22), intArrayOf(0x3fffe4, 22),
intArrayOf(0x7ffff0, 23), intArrayOf(0x3fffe5, 22), intArrayOf(0x3fffe6, 22), intArrayOf(0x7ffff1, 23),
intArrayOf(0x3ffffe0, 26), intArrayOf(0x3ffffe1, 26), intArrayOf(0xfffeb, 20), intArrayOf(0x7fff1, 19),
intArrayOf(0x3fffe7, 22), intArrayOf(0x7ffff2, 23), intArrayOf(0x3fffe8, 22), intArrayOf(0x1ffffec, 25),
intArrayOf(0x3ffffe2, 26), intArrayOf(0x3ffffe3, 26), intArrayOf(0x3ffffe4, 26), intArrayOf(0x7ffffde, 27),
intArrayOf(0x7ffffdf, 27), intArrayOf(0x3ffffe5, 26), intArrayOf(0xfffff1, 24), intArrayOf(0x1ffffed, 25),
intArrayOf(0x7fff2, 19), intArrayOf(0x1fffe3, 21), intArrayOf(0x3ffffe6, 26), intArrayOf(0x7ffffe0, 27),
intArrayOf(0x7ffffe1, 27), intArrayOf(0x3ffffe7, 26), intArrayOf(0x7ffffe2, 27), intArrayOf(0xfffff2, 24),
intArrayOf(0x1fffe4, 21), intArrayOf(0x1fffe5, 21), intArrayOf(0x3ffffe8, 26), intArrayOf(0x3ffffe9, 26),
intArrayOf(0xffffffd, 28), intArrayOf(0x7ffffe3, 27), intArrayOf(0x7ffffe4, 27), intArrayOf(0x7ffffe5, 27),
intArrayOf(0xfffec, 20), intArrayOf(0xfffff3, 24), intArrayOf(0xfffed, 20), intArrayOf(0x1fffe6, 21),
intArrayOf(0x3fffe9, 22), intArrayOf(0x1fffe7, 21), intArrayOf(0x1fffe8, 21), intArrayOf(0x7ffff3, 23),
intArrayOf(0x3fffea, 22), intArrayOf(0x3fffeb, 22), intArrayOf(0x1ffffee, 25), intArrayOf(0x1ffffef, 25),
intArrayOf(0xfffff4, 24), intArrayOf(0xfffff5, 24), intArrayOf(0x3ffffea, 26), intArrayOf(0x7ffff4, 23),
intArrayOf(0x3ffffeb, 26), intArrayOf(0x7ffffe6, 27), intArrayOf(0x3ffffec, 26), intArrayOf(0x3ffffed, 26),
intArrayOf(0x7ffffe7, 27), intArrayOf(0x7ffffe8, 27), intArrayOf(0x7ffffe9, 27), intArrayOf(0x7ffffea, 27),
intArrayOf(0x7ffffeb, 27), intArrayOf(0xffffffe, 28), intArrayOf(0x7ffffec, 27), intArrayOf(0x7ffffed, 27),
intArrayOf(0x7ffffee, 27), intArrayOf(0x7ffffef, 27), intArrayOf(0x7fffff0, 27), intArrayOf(0x3ffffee, 26),
intArrayOf(0x1ff8, 13),
intArrayOf(0x7fffd8, 23),
intArrayOf(0xfffffe2, 28),
intArrayOf(0xfffffe3, 28),
intArrayOf(0xfffffe4, 28),
intArrayOf(0xfffffe5, 28),
intArrayOf(0xfffffe6, 28),
intArrayOf(0xfffffe7, 28),
intArrayOf(0xfffffe8, 28),
intArrayOf(0xffffea, 24),
intArrayOf(0x3ffffffc, 30),
intArrayOf(0xfffffe9, 28),
intArrayOf(0xfffffea, 28),
intArrayOf(0x3ffffffd, 30),
intArrayOf(0xfffffeb, 28),
intArrayOf(0xfffffec, 28),
intArrayOf(0xfffffed, 28),
intArrayOf(0xfffffee, 28),
intArrayOf(0xfffffef, 28),
intArrayOf(0xffffff0, 28),
intArrayOf(0xffffff1, 28),
intArrayOf(0xffffff2, 28),
intArrayOf(0x3ffffffe, 30),
intArrayOf(0xffffff3, 28),
intArrayOf(0xffffff4, 28),
intArrayOf(0xffffff5, 28),
intArrayOf(0xffffff6, 28),
intArrayOf(0xffffff7, 28),
intArrayOf(0xffffff8, 28),
intArrayOf(0xffffff9, 28),
intArrayOf(0xffffffa, 28),
intArrayOf(0xffffffb, 28),
intArrayOf(0x14, 6),
intArrayOf(0x3f8, 10),
intArrayOf(0x3f9, 10),
intArrayOf(0xffa, 12),
intArrayOf(0x1ff9, 13),
intArrayOf(0x15, 6),
intArrayOf(0xf8, 8),
intArrayOf(0x7fa, 11),
intArrayOf(0x3fa, 10),
intArrayOf(0x3fb, 10),
intArrayOf(0xf9, 8),
intArrayOf(0x7fb, 11),
intArrayOf(0xfa, 8),
intArrayOf(0x16, 6),
intArrayOf(0x17, 6),
intArrayOf(0x18, 6),
intArrayOf(0x0, 5),
intArrayOf(0x1, 5),
intArrayOf(0x2, 5),
intArrayOf(0x19, 6),
intArrayOf(0x1a, 6),
intArrayOf(0x1b, 6),
intArrayOf(0x1c, 6),
intArrayOf(0x1d, 6),
intArrayOf(0x1e, 6),
intArrayOf(0x1f, 6),
intArrayOf(0x5c, 7),
intArrayOf(0xfb, 8),
intArrayOf(0x7ffc, 15),
intArrayOf(0x20, 6),
intArrayOf(0xffb, 12),
intArrayOf(0x3fc, 10),
intArrayOf(0x1ffa, 13),
intArrayOf(0x21, 6),
intArrayOf(0x5d, 7),
intArrayOf(0x5e, 7),
intArrayOf(0x5f, 7),
intArrayOf(0x60, 7),
intArrayOf(0x61, 7),
intArrayOf(0x62, 7),
intArrayOf(0x63, 7),
intArrayOf(0x64, 7),
intArrayOf(0x65, 7),
intArrayOf(0x66, 7),
intArrayOf(0x67, 7),
intArrayOf(0x68, 7),
intArrayOf(0x69, 7),
intArrayOf(0x6a, 7),
intArrayOf(0x6b, 7),
intArrayOf(0x6c, 7),
intArrayOf(0x6d, 7),
intArrayOf(0x6e, 7),
intArrayOf(0x6f, 7),
intArrayOf(0x70, 7),
intArrayOf(0x71, 7),
intArrayOf(0x72, 7),
intArrayOf(0xfc, 8),
intArrayOf(0x73, 7),
intArrayOf(0xfd, 8),
intArrayOf(0x1ffb, 13),
intArrayOf(0x7fff0, 19),
intArrayOf(0x1ffc, 13),
intArrayOf(0x3ffc, 14),
intArrayOf(0x22, 6),
intArrayOf(0x7ffd, 15),
intArrayOf(0x3, 5),
intArrayOf(0x23, 6),
intArrayOf(0x4, 5),
intArrayOf(0x24, 6),
intArrayOf(0x5, 5),
intArrayOf(0x25, 6),
intArrayOf(0x26, 6),
intArrayOf(0x27, 6),
intArrayOf(0x6, 5),
intArrayOf(0x74, 7),
intArrayOf(0x75, 7),
intArrayOf(0x28, 6),
intArrayOf(0x29, 6),
intArrayOf(0x2a, 6),
intArrayOf(0x7, 5),
intArrayOf(0x2b, 6),
intArrayOf(0x76, 7),
intArrayOf(0x2c, 6),
intArrayOf(0x8, 5),
intArrayOf(0x9, 5),
intArrayOf(0x2d, 6),
intArrayOf(0x77, 7),
intArrayOf(0x78, 7),
intArrayOf(0x79, 7),
intArrayOf(0x7a, 7),
intArrayOf(0x7b, 7),
intArrayOf(0x7ffe, 15),
intArrayOf(0x7fc, 11),
intArrayOf(0x3ffd, 14),
intArrayOf(0x1ffd, 13),
intArrayOf(0xffffffc, 28),
intArrayOf(0xfffe6, 20),
intArrayOf(0x3fffd2, 22),
intArrayOf(0xfffe7, 20),
intArrayOf(0xfffe8, 20),
intArrayOf(0x3fffd3, 22),
intArrayOf(0x3fffd4, 22),
intArrayOf(0x3fffd5, 22),
intArrayOf(0x7fffd9, 23),
intArrayOf(0x3fffd6, 22),
intArrayOf(0x7fffda, 23),
intArrayOf(0x7fffdb, 23),
intArrayOf(0x7fffdc, 23),
intArrayOf(0x7fffdd, 23),
intArrayOf(0x7fffde, 23),
intArrayOf(0xffffeb, 24),
intArrayOf(0x7fffdf, 23),
intArrayOf(0xffffec, 24),
intArrayOf(0xffffed, 24),
intArrayOf(0x3fffd7, 22),
intArrayOf(0x7fffe0, 23),
intArrayOf(0xffffee, 24),
intArrayOf(0x7fffe1, 23),
intArrayOf(0x7fffe2, 23),
intArrayOf(0x7fffe3, 23),
intArrayOf(0x7fffe4, 23),
intArrayOf(0x1fffdc, 21),
intArrayOf(0x3fffd8, 22),
intArrayOf(0x7fffe5, 23),
intArrayOf(0x3fffd9, 22),
intArrayOf(0x7fffe6, 23),
intArrayOf(0x7fffe7, 23),
intArrayOf(0xffffef, 24),
intArrayOf(0x3fffda, 22),
intArrayOf(0x1fffdd, 21),
intArrayOf(0xfffe9, 20),
intArrayOf(0x3fffdb, 22),
intArrayOf(0x3fffdc, 22),
intArrayOf(0x7fffe8, 23),
intArrayOf(0x7fffe9, 23),
intArrayOf(0x1fffde, 21),
intArrayOf(0x7fffea, 23),
intArrayOf(0x3fffdd, 22),
intArrayOf(0x3fffde, 22),
intArrayOf(0xfffff0, 24),
intArrayOf(0x1fffdf, 21),
intArrayOf(0x3fffdf, 22),
intArrayOf(0x7fffeb, 23),
intArrayOf(0x7fffec, 23),
intArrayOf(0x1fffe0, 21),
intArrayOf(0x1fffe1, 21),
intArrayOf(0x3fffe0, 22),
intArrayOf(0x1fffe2, 21),
intArrayOf(0x7fffed, 23),
intArrayOf(0x3fffe1, 22),
intArrayOf(0x7fffee, 23),
intArrayOf(0x7fffef, 23),
intArrayOf(0xfffea, 20),
intArrayOf(0x3fffe2, 22),
intArrayOf(0x3fffe3, 22),
intArrayOf(0x3fffe4, 22),
intArrayOf(0x7ffff0, 23),
intArrayOf(0x3fffe5, 22),
intArrayOf(0x3fffe6, 22),
intArrayOf(0x7ffff1, 23),
intArrayOf(0x3ffffe0, 26),
intArrayOf(0x3ffffe1, 26),
intArrayOf(0xfffeb, 20),
intArrayOf(0x7fff1, 19),
intArrayOf(0x3fffe7, 22),
intArrayOf(0x7ffff2, 23),
intArrayOf(0x3fffe8, 22),
intArrayOf(0x1ffffec, 25),
intArrayOf(0x3ffffe2, 26),
intArrayOf(0x3ffffe3, 26),
intArrayOf(0x3ffffe4, 26),
intArrayOf(0x7ffffde, 27),
intArrayOf(0x7ffffdf, 27),
intArrayOf(0x3ffffe5, 26),
intArrayOf(0xfffff1, 24),
intArrayOf(0x1ffffed, 25),
intArrayOf(0x7fff2, 19),
intArrayOf(0x1fffe3, 21),
intArrayOf(0x3ffffe6, 26),
intArrayOf(0x7ffffe0, 27),
intArrayOf(0x7ffffe1, 27),
intArrayOf(0x3ffffe7, 26),
intArrayOf(0x7ffffe2, 27),
intArrayOf(0xfffff2, 24),
intArrayOf(0x1fffe4, 21),
intArrayOf(0x1fffe5, 21),
intArrayOf(0x3ffffe8, 26),
intArrayOf(0x3ffffe9, 26),
intArrayOf(0xffffffd, 28),
intArrayOf(0x7ffffe3, 27),
intArrayOf(0x7ffffe4, 27),
intArrayOf(0x7ffffe5, 27),
intArrayOf(0xfffec, 20),
intArrayOf(0xfffff3, 24),
intArrayOf(0xfffed, 20),
intArrayOf(0x1fffe6, 21),
intArrayOf(0x3fffe9, 22),
intArrayOf(0x1fffe7, 21),
intArrayOf(0x1fffe8, 21),
intArrayOf(0x7ffff3, 23),
intArrayOf(0x3fffea, 22),
intArrayOf(0x3fffeb, 22),
intArrayOf(0x1ffffee, 25),
intArrayOf(0x1ffffef, 25),
intArrayOf(0xfffff4, 24),
intArrayOf(0xfffff5, 24),
intArrayOf(0x3ffffea, 26),
intArrayOf(0x7ffff4, 23),
intArrayOf(0x3ffffeb, 26),
intArrayOf(0x7ffffe6, 27),
intArrayOf(0x3ffffec, 26),
intArrayOf(0x3ffffed, 26),
intArrayOf(0x7ffffe7, 27),
intArrayOf(0x7ffffe8, 27),
intArrayOf(0x7ffffe9, 27),
intArrayOf(0x7ffffea, 27),
intArrayOf(0x7ffffeb, 27),
intArrayOf(0xffffffe, 28),
intArrayOf(0x7ffffec, 27),
intArrayOf(0x7ffffed, 27),
intArrayOf(0x7ffffee, 27),
intArrayOf(0x7ffffef, 27),
intArrayOf(0x7fffff0, 27),
intArrayOf(0x3ffffee, 26),
intArrayOf(0x3fffffff, 30),
)
@@ -133,12 +133,14 @@ object QpackStaticTable {
/** Build a quick lookup of name → first index for encoder name-reference selection. */
val nameToIndex: Map<String, Int> =
entries.foldIndexed(mutableMapOf()) { idx, acc, (name, _) ->
acc.putIfAbsent(name, idx); acc
acc.putIfAbsent(name, idx)
acc
}
/** Look up name+value → index, or null if no exact match. */
val pairToIndex: Map<Pair<String, String>, Int> =
entries.foldIndexed(mutableMapOf()) { idx, acc, pair ->
acc.putIfAbsent(pair, idx); acc
acc.putIfAbsent(pair, idx)
acc
}
}
@@ -111,7 +111,9 @@ class TlsClient(
serverName = serverName,
x25519PublicKey = keyPair!!.publicKey,
quicTransportParams = transportParameters,
random = fixedRandom ?: com.vitorpamplona.quartz.utils.RandomInstance.bytes(32),
random =
fixedRandom ?: com.vitorpamplona.quartz.utils.RandomInstance
.bytes(32),
)
val chBytes = ch.encode()
@@ -184,6 +186,7 @@ class TlsClient(
)
state = State.WAITING_ENCRYPTED_EXTENSIONS
}
State.WAITING_ENCRYPTED_EXTENSIONS -> {
if (type != TlsConstants.HS_ENCRYPTED_EXTENSIONS) throw QuicCodecException("expected EncryptedExtensions, got type=$type")
if (level != Level.HANDSHAKE) throw QuicCodecException("EncryptedExtensions must arrive at Handshake level")
@@ -193,6 +196,7 @@ class TlsClient(
transcript.append(msg)
state = State.WAITING_CERTIFICATE_OR_FINISHED
}
State.WAITING_CERTIFICATE_OR_FINISHED -> {
when (type) {
TlsConstants.HS_CERTIFICATE -> {
@@ -201,14 +205,19 @@ class TlsClient(
transcript.append(msg)
state = State.WAITING_CERTIFICATE_VERIFY
}
TlsConstants.HS_FINISHED -> {
// PSK-only handshake skips Certificate/CertificateVerify. We never use PSK,
// but the state machine handles the transition for completeness.
handleServerFinished(msg, bodyReader, len)
}
else -> throw QuicCodecException("unexpected handshake type after EncryptedExtensions: $type")
else -> {
throw QuicCodecException("unexpected handshake type after EncryptedExtensions: $type")
}
}
}
State.WAITING_CERTIFICATE_VERIFY -> {
if (type != TlsConstants.HS_CERTIFICATE_VERIFY) throw QuicCodecException("expected CertificateVerify, got type=$type")
val cv = TlsCertificateVerify.decodeBody(bodyReader)
@@ -217,11 +226,15 @@ class TlsClient(
transcript.append(msg)
state = State.WAITING_SERVER_FINISHED
}
State.WAITING_SERVER_FINISHED -> {
if (type != TlsConstants.HS_FINISHED) throw QuicCodecException("expected Finished, got type=$type")
handleServerFinished(msg, bodyReader, len)
}
else -> throw QuicCodecException("unexpected handshake at state=$state type=$type")
else -> {
throw QuicCodecException("unexpected handshake at state=$state type=$type")
}
}
}
@@ -20,8 +20,8 @@
*/
package com.vitorpamplona.quic.tls
import com.vitorpamplona.quic.QuicWriter
import com.vitorpamplona.quartz.utils.RandomInstance
import com.vitorpamplona.quic.QuicWriter
/**
* Build a TLS 1.3 ClientHello + handshake header carrying the QUIC-required
@@ -24,7 +24,8 @@ package com.vitorpamplona.quic.tls
* TLS 1.3 protocol constants from RFC 8446 + RFC 9001 (TLS-over-QUIC binding).
*/
object TlsConstants {
// ── Record / handshake message types ──────────────────────────────────────
// Record / handshake message types
/** TLS 1.3 over QUIC uses `legacy_version = 0x0303` ("TLS 1.2") on the wire. */
const val LEGACY_VERSION_TLS_1_2: Int = 0x0303
const val VERSION_TLS_1_3: Int = 0x0304
@@ -55,6 +56,7 @@ object TlsConstants {
const val EXT_SUPPORTED_VERSIONS: Int = 43
const val EXT_PSK_KEY_EXCHANGE_MODES: Int = 45
const val EXT_KEY_SHARE: Int = 51
/** RFC 9001 §8.2 — the QUIC TLS extension carrying transport parameters. */
const val EXT_QUIC_TRANSPORT_PARAMETERS: Int = 0x39
@@ -36,8 +36,9 @@ data class TlsServerHello(
/** The negotiated protocol version. Must be 0x0304 (TLS 1.3) per RFC 8446. */
val negotiatedVersion: Int
get() {
val ext = extensions.firstOrNull { it.type == TlsConstants.EXT_SUPPORTED_VERSIONS }
?: throw QuicCodecException("server hello missing supported_versions extension")
val ext =
extensions.firstOrNull { it.type == TlsConstants.EXT_SUPPORTED_VERSIONS }
?: throw QuicCodecException("server hello missing supported_versions extension")
// server hello carries selected_version (uint16)
val r = QuicReader(ext.data)
return r.readUint16()
@@ -46,8 +47,9 @@ data class TlsServerHello(
/** The peer's X25519 public key, extracted from key_share. */
val serverKeyShareX25519: ByteArray
get() {
val ext = extensions.firstOrNull { it.type == TlsConstants.EXT_KEY_SHARE }
?: throw QuicCodecException("server hello missing key_share extension")
val ext =
extensions.firstOrNull { it.type == TlsConstants.EXT_KEY_SHARE }
?: throw QuicCodecException("server hello missing key_share extension")
val r = QuicReader(ext.data)
val group = r.readUint16()
if (group != TlsConstants.GROUP_X25519) {
@@ -81,12 +83,13 @@ data class TlsEncryptedExtensions(
get() = extensions.firstOrNull { it.type == TlsConstants.EXT_QUIC_TRANSPORT_PARAMETERS }?.data
val alpn: ByteArray?
get() = extensions.firstOrNull { it.type == TlsConstants.EXT_ALPN }?.data?.let {
// ALPN response carries a single protocol_name<1..2^8-1> inside protocols<3..2^16-1>
val r = QuicReader(it)
r.skip(2) // outer length
r.readTlsOpaque1()
}
get() =
extensions.firstOrNull { it.type == TlsConstants.EXT_ALPN }?.data?.let {
// ALPN response carries a single protocol_name<1..2^8-1> inside protocols<3..2^16-1>
val r = QuicReader(it)
r.skip(2) // outer length
r.readTlsOpaque1()
}
companion object {
fun decodeBody(r: QuicReader): TlsEncryptedExtensions = TlsEncryptedExtensions(TlsExtension.decodeList(r))
@@ -0,0 +1,84 @@
/*
* Copyright (c) 2025 Vitor Pamplona
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.vitorpamplona.quic.webtransport
import com.vitorpamplona.quic.connection.QuicConnection
import com.vitorpamplona.quic.connection.QuicConnectionDriver
import com.vitorpamplona.quic.stream.QuicStream
/**
* Container that bundles a [QuicConnection], its [QuicConnectionDriver], and
* the QUIC stream id of the CONNECT bidi (the WT session id).
*
* Application code (nestsClient's MoQ layer) gets at this via the
* [QuicWebTransportFactory] adapter which exposes the platform-agnostic
* [com.vitorpamplona.nestsclient.transport.WebTransportSession] interface.
*/
class QuicWebTransportSessionState(
val connection: QuicConnection,
val driver: QuicConnectionDriver,
val connectStreamId: Long,
) {
/** True until [close] is called or the underlying QUIC connection terminates. */
val isOpen: Boolean
get() = connection.status == QuicConnection.Status.CONNECTED
/** Open a new client-initiated bidirectional WebTransport stream. */
fun openBidiStream(): QuicStream {
val s = connection.openBidiStream()
// Prefix bytes go onto the new stream first.
s.send.enqueue(encodeWtBidiStreamPrefix(connectStreamId))
return s
}
/** Open a new client-initiated unidirectional WebTransport stream. */
fun openUniStream(): QuicStream {
val s = connection.openUniStream()
s.send.enqueue(encodeWtUniStreamPrefix(connectStreamId))
return s
}
/** Send a WebTransport datagram via QUIC's datagram extension. */
fun sendDatagram(payload: ByteArray) {
val wrapped = WtDatagram.encode(connectStreamId, payload)
connection.queueDatagram(wrapped)
}
fun pollIncomingDatagram(): ByteArray? {
val raw = connection.pollIncomingDatagram() ?: return null
val decoded = WtDatagram.decode(raw) ?: return null
if (decoded.sessionStreamId != connectStreamId) return null
return decoded.payload
}
fun pollIncomingPeerStream(): QuicStream? = connection.pollIncomingPeerStream()
fun close(
errorCode: Int = 0,
reason: String = "",
) {
connection.streamById(connectStreamId)?.let {
it.send.enqueue(encodeCloseSessionCapsule(errorCode, reason))
it.send.finish()
}
driver.close()
}
}
@@ -20,7 +20,6 @@
*/
package com.vitorpamplona.quic.webtransport
import com.vitorpamplona.quic.QuicReader
import com.vitorpamplona.quic.QuicWriter
import com.vitorpamplona.quic.Varint
@@ -65,6 +64,7 @@ object WtDatagram {
object WtStreamType {
/** Client-initiated bidirectional WT stream — followed by quarter session id. */
const val WT_BIDI_STREAM: Long = 0x41
/** Client-initiated unidirectional WT stream — preceded by HTTP/3 stream-type 0x54 + quarter session id. */
const val WT_UNI_STREAM_PREFIX: Long = 0x54
}