feat(quic): Phase C — long-header packets, frames, short-header packets

End-to-end packet codec for QUIC v1 packets:

- Long-header packet builder + parser (RFC 9000 §17.2) with packet-number
  encoding, header protection (AES-ECB sample mask), and AEAD-GCM payload
  protection. Initial packets carry the optional token field.
- Short-header (1-RTT) packet builder + parser with implicit DCID length.
- Stream reassembly buffer that coalesces out-of-order, overlapping chunks
  into a contiguous prefix; consumed bytes are dropped, future overlaps
  are deduplicated.
- Stream-id helpers (RFC 9000 §2.1) — client/server, bidi/uni discrimination.
- Frame codec for the minimal subset MoQ exercises: PADDING, PING, ACK,
  ACK_ECN, CRYPTO, STREAM (all OFF/LEN/FIN flag combos), MAX_DATA,
  MAX_STREAM_DATA, MAX_STREAMS, NEW_CONNECTION_ID, CONNECTION_CLOSE
  (transport + app), HANDSHAKE_DONE, DATAGRAM (RFC 9221).

Round-trip test against RFC 9001 Appendix A.1's canonical client DCID
encrypts an Initial packet with the canonical protection material, then
decrypts it from the wire bit-for-bit. A wrong-key decrypt returns null
(silent drop per RFC 9001 §5.5). ReceiveBuffer reorders, deduplicates,
coalesces, and drops already-consumed prefixes correctly.

https://claude.ai/code/session_01EC1tfXfap8k8GyKvrxkxZx
This commit is contained in:
Claude
2026-04-25 17:39:06 +00:00
parent 692b034566
commit ac53853057
8 changed files with 1207 additions and 0 deletions
@@ -0,0 +1,132 @@
/*
* 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.packet
import com.vitorpamplona.quic.connection.ConnectionId
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 kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
class InitialPacketRoundTripTest {
/**
* Round-trip an Initial packet through:
* client-side encrypt + HP-apply → wire bytes → server-side HP-strip + decrypt.
*
* Uses RFC 9001 Appendix A.1's canonical client DCID `0x8394c8f03e515708`
* so the protection material matches the canonical vectors.
*/
@Test
fun client_initial_round_trip() {
val dcid = ConnectionId("8394c8f03e515708".hexToByteArray())
val scid = ConnectionId("00".hexToByteArray())
val proto = InitialSecrets.derive(dcid.bytes)
val hp = AesEcbHeaderProtection(PlatformAesOneBlock)
val payload = "deadbeefcafebabe1234567890abcdef".hexToByteArray()
val plain =
LongHeaderPlaintextPacket(
type = LongHeaderType.INITIAL,
dcid = dcid,
scid = scid,
packetNumber = 0L,
payload = payload,
)
val wire =
LongHeaderPacket.build(
plain = plain,
aead = Aes128Gcm,
key = proto.clientKey,
iv = proto.clientIv,
hp = hp,
hpKey = proto.clientHp,
largestAckedInSpace = -1L,
)
// Server side reverses
val parsed =
LongHeaderPacket.parseAndDecrypt(
bytes = wire,
offset = 0,
aead = Aes128Gcm,
key = proto.clientKey,
iv = proto.clientIv,
hp = hp,
hpKey = proto.clientHp,
largestReceivedInSpace = -1L,
)
assertNotNull(parsed)
assertEquals(LongHeaderType.INITIAL, parsed.packet.type)
assertEquals(dcid, parsed.packet.dcid)
assertEquals(scid, parsed.packet.scid)
assertEquals(0L, parsed.packet.packetNumber)
assertContentEquals(payload, parsed.packet.payload)
assertEquals(wire.size, parsed.consumed)
}
/**
* Auth tag failure with a wrong key must surface as null (drop silently).
*/
@Test
fun decrypt_with_wrong_key_returns_null() {
val dcid = ConnectionId("8394c8f03e515708".hexToByteArray())
val scid = ConnectionId(byteArrayOf(0x42))
val proto = InitialSecrets.derive(dcid.bytes)
val wrongProto = InitialSecrets.derive("0000000000000000".hexToByteArray())
val hp = AesEcbHeaderProtection(PlatformAesOneBlock)
val payload = "00112233445566778899aabbccddeeff".hexToByteArray()
val wire =
LongHeaderPacket.build(
plain =
LongHeaderPlaintextPacket(
type = LongHeaderType.INITIAL,
dcid = dcid,
scid = scid,
packetNumber = 0L,
payload = payload,
),
aead = Aes128Gcm,
key = proto.clientKey,
iv = proto.clientIv,
hp = hp,
hpKey = proto.clientHp,
largestAckedInSpace = -1L,
)
val parsed =
LongHeaderPacket.parseAndDecrypt(
bytes = wire,
offset = 0,
aead = Aes128Gcm,
key = wrongProto.clientKey,
iv = wrongProto.clientIv,
hp = hp,
hpKey = wrongProto.clientHp,
largestReceivedInSpace = -1L,
)
// With a wrong HP key the first byte/PN are mis-unmasked and AEAD will
// certainly fail. We expect a clean null.
assertEquals(null, parsed)
}
}
@@ -0,0 +1,75 @@
/*
* 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.stream
import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals
import kotlin.test.assertTrue
class ReceiveBufferTest {
@Test
fun in_order_chunks_pass_through() {
val buf = ReceiveBuffer()
buf.insert(0, byteArrayOf(1, 2, 3))
assertContentEquals(byteArrayOf(1, 2, 3), buf.readContiguous())
buf.insert(3, byteArrayOf(4, 5))
assertContentEquals(byteArrayOf(4, 5), buf.readContiguous())
assertEquals(5, buf.contiguousEnd())
}
@Test
fun reordered_chunks_are_buffered_until_filled() {
val buf = ReceiveBuffer()
buf.insert(2, byteArrayOf(3, 4, 5))
// Gap at 0..1; nothing yet.
assertEquals(0, buf.readContiguous().size)
buf.insert(0, byteArrayOf(1, 2))
// Now fully contiguous up to 5.
assertContentEquals(byteArrayOf(1, 2, 3, 4, 5), buf.readContiguous())
}
@Test
fun overlapping_chunks_are_deduplicated() {
val buf = ReceiveBuffer()
buf.insert(0, byteArrayOf(1, 2, 3, 4))
buf.insert(2, byteArrayOf(3, 4, 5, 6))
assertContentEquals(byteArrayOf(1, 2, 3, 4, 5, 6), buf.readContiguous())
}
@Test
fun fin_propagates_through_buffer() {
val buf = ReceiveBuffer()
buf.insert(0, byteArrayOf(1, 2, 3), fin = true)
assertTrue(buf.finReceived)
}
@Test
fun later_chunk_preceding_already_consumed_data_is_dropped() {
val buf = ReceiveBuffer()
buf.insert(0, byteArrayOf(1, 2, 3))
buf.readContiguous()
// Now readOffset = 3; this chunk overlaps with already-consumed 0..2.
buf.insert(0, byteArrayOf(1, 2, 3, 4, 5))
// The remaining 4..5 should still come through.
assertContentEquals(byteArrayOf(4, 5), buf.readContiguous())
}
}