implement parsing and writing of QUIC v2 packet headers

This commit is contained in:
Marten Seemann 2022-05-23 19:36:14 +02:00
parent 909a3a9093
commit f3744f6535
6 changed files with 164 additions and 46 deletions

View file

@ -14,7 +14,7 @@ import (
// writePacket returns a new raw packet with the specified header and payload
func writePacket(hdr *wire.ExtendedHeader, data []byte) []byte {
buf := &bytes.Buffer{}
hdr.Write(buf, protocol.VersionTLS)
hdr.Write(buf, hdr.Version)
return append(buf.Bytes(), data...)
}

View file

@ -127,18 +127,32 @@ func (h *ExtendedHeader) Write(b *bytes.Buffer, ver protocol.VersionNumber) erro
return h.writeShortHeader(b, ver)
}
func (h *ExtendedHeader) writeLongHeader(b *bytes.Buffer, _ protocol.VersionNumber) error {
func (h *ExtendedHeader) writeLongHeader(b *bytes.Buffer, version protocol.VersionNumber) error {
var packetType uint8
//nolint:exhaustive
switch h.Type {
case protocol.PacketTypeInitial:
packetType = 0x0
case protocol.PacketType0RTT:
packetType = 0x1
case protocol.PacketTypeHandshake:
packetType = 0x2
case protocol.PacketTypeRetry:
packetType = 0x3
if version == protocol.Version2 {
//nolint:exhaustive
switch h.Type {
case protocol.PacketTypeInitial:
packetType = 0b01
case protocol.PacketType0RTT:
packetType = 0b10
case protocol.PacketTypeHandshake:
packetType = 0b11
case protocol.PacketTypeRetry:
packetType = 0b00
}
} else {
//nolint:exhaustive
switch h.Type {
case protocol.PacketTypeInitial:
packetType = 0b00
case protocol.PacketType0RTT:
packetType = 0b01
case protocol.PacketTypeHandshake:
packetType = 0b10
case protocol.PacketTypeRetry:
packetType = 0b11
}
}
firstByte := 0xc0 | packetType<<4
if h.Type != protocol.PacketTypeRetry {

View file

@ -95,6 +95,7 @@ var _ = Describe("Header", func() {
PacketNumber: 0xdecafbad,
PacketNumberLen: protocol.PacketNumberLen4,
}).Write(buf, versionIETFHeader)).To(Succeed())
Expect(buf.Bytes()[0]>>4&0b11 == 0)
expectedSubstring := append(encodeVarInt(uint64(len(token))), token...)
Expect(buf.Bytes()).To(ContainSubstring(string(expectedSubstring)))
})
@ -119,21 +120,76 @@ var _ = Describe("Header", func() {
token := []byte("Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.")
Expect((&ExtendedHeader{Header: Header{
IsLongHeader: true,
Version: 0x1020304,
Version: protocol.Version1,
Type: protocol.PacketTypeRetry,
Token: token,
}}).Write(buf, versionIETFHeader)).To(Succeed())
expected := []byte{
0xc0 | 0x3<<4,
0x1, 0x2, 0x3, 0x4, // version number
0x0, // dest connection ID length
0x0, // src connection ID length
}
expected := []byte{0xc0 | 0b11<<4}
expected = appendVersion(expected, protocol.Version1)
expected = append(expected, 0x0) // dest connection ID length
expected = append(expected, 0x0) // src connection ID length
expected = append(expected, token...)
Expect(buf.Bytes()).To(Equal(expected))
})
})
Context("long header, version 2", func() {
It("writes an Initial", func() {
Expect((&ExtendedHeader{
Header: Header{
IsLongHeader: true,
Version: protocol.Version2,
Type: protocol.PacketTypeInitial,
},
PacketNumber: 0xdecafbad,
PacketNumberLen: protocol.PacketNumberLen4,
}).Write(buf, protocol.Version2)).To(Succeed())
Expect(buf.Bytes()[0]>>4&0b11 == 0b01)
})
It("writes a Retry packet", func() {
token := []byte("Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.")
Expect((&ExtendedHeader{Header: Header{
IsLongHeader: true,
Version: protocol.Version2,
Type: protocol.PacketTypeRetry,
Token: token,
}}).Write(buf, versionIETFHeader)).To(Succeed())
expected := []byte{0xc0 | 0b11<<4}
expected = appendVersion(expected, protocol.Version2)
expected = append(expected, 0x0) // dest connection ID length
expected = append(expected, 0x0) // src connection ID length
expected = append(expected, token...)
Expect(buf.Bytes()).To(Equal(expected))
})
It("writes a Handshake Packet", func() {
Expect((&ExtendedHeader{
Header: Header{
IsLongHeader: true,
Version: protocol.Version2,
Type: protocol.PacketTypeHandshake,
},
PacketNumber: 0xdecafbad,
PacketNumberLen: protocol.PacketNumberLen4,
}).Write(buf, protocol.Version2)).To(Succeed())
Expect(buf.Bytes()[0]>>4&0b11 == 0b11)
})
It("writes a 0-RTT Packet", func() {
Expect((&ExtendedHeader{
Header: Header{
IsLongHeader: true,
Version: protocol.Version2,
Type: protocol.PacketType0RTT,
},
PacketNumber: 0xdecafbad,
PacketNumberLen: protocol.PacketNumberLen4,
}).Write(buf, protocol.Version2)).To(Succeed())
Expect(buf.Bytes()[0]>>4&0b11 == 0b10)
})
})
Context("short header", func() {
It("writes a header with connection ID", func() {
Expect((&ExtendedHeader{

View file

@ -53,10 +53,14 @@ func Is0RTTPacket(b []byte) bool {
if b[0]&0x80 == 0 {
return false
}
if !protocol.IsSupportedVersion(protocol.SupportedVersions, protocol.VersionNumber(binary.BigEndian.Uint32(b[1:5]))) {
version := protocol.VersionNumber(binary.BigEndian.Uint32(b[1:5]))
if !protocol.IsSupportedVersion(protocol.SupportedVersions, version) {
return false
}
return b[0]&0x30>>4 == 0x1
if version == protocol.Version2 {
return b[0]>>4&0b11 == 0b10
}
return b[0]>>4&0b11 == 0b01
}
var ErrUnsupportedVersion = errors.New("unsupported version")
@ -179,15 +183,28 @@ func (h *Header) parseLongHeader(b *bytes.Reader) error {
return ErrUnsupportedVersion
}
switch (h.typeByte & 0x30) >> 4 {
case 0x0:
h.Type = protocol.PacketTypeInitial
case 0x1:
h.Type = protocol.PacketType0RTT
case 0x2:
h.Type = protocol.PacketTypeHandshake
case 0x3:
h.Type = protocol.PacketTypeRetry
if h.Version == protocol.Version2 {
switch h.typeByte >> 4 & 0b11 {
case 0b00:
h.Type = protocol.PacketTypeRetry
case 0b01:
h.Type = protocol.PacketTypeInitial
case 0b10:
h.Type = protocol.PacketType0RTT
case 0b11:
h.Type = protocol.PacketTypeHandshake
}
} else {
switch h.typeByte >> 4 & 0b11 {
case 0b00:
h.Type = protocol.PacketTypeInitial
case 0b01:
h.Type = protocol.PacketType0RTT
case 0b10:
h.Type = protocol.PacketTypeHandshake
case 0b11:
h.Type = protocol.PacketTypeRetry
}
}
if h.Type == protocol.PacketTypeRetry {

View file

@ -11,13 +11,6 @@ import (
)
var _ = Describe("Header Parsing", func() {
appendVersion := func(data []byte, v protocol.VersionNumber) []byte {
offset := len(data)
data = append(data, []byte{0, 0, 0, 0}...)
binary.BigEndian.PutUint32(data[offset:], uint32(v))
return data
}
Context("Parsing the Connection ID", func() {
It("parses the connection ID of a long header packet", func() {
buf := &bytes.Buffer{}
@ -94,19 +87,27 @@ var _ = Describe("Header Parsing", func() {
})
Context("identifying 0-RTT packets", func() {
var zeroRTTHeader []byte
BeforeEach(func() {
zeroRTTHeader = make([]byte, 5)
zeroRTTHeader[0] = 0x80 | 0x1<<4
It("recognizes 0-RTT packets, for QUIC v1", func() {
zeroRTTHeader := make([]byte, 5)
zeroRTTHeader[0] = 0x80 | 0b01<<4
binary.BigEndian.PutUint32(zeroRTTHeader[1:], uint32(protocol.Version1))
})
It("recognizes 0-RTT packets", func() {
Expect(Is0RTTPacket(zeroRTTHeader)).To(BeTrue())
Expect(Is0RTTPacket(zeroRTTHeader[:4])).To(BeFalse()) // too short
Expect(Is0RTTPacket([]byte{zeroRTTHeader[0], 1, 2, 3, 4})).To(BeFalse()) // unknown version
Expect(Is0RTTPacket([]byte{zeroRTTHeader[0] | 0x80, 1, 2, 3, 4})).To(BeFalse()) // short header
Expect(Is0RTTPacket(append(zeroRTTHeader, []byte("foobar")...))).To(BeTrue())
})
It("recognizes 0-RTT packets, for QUIC v2", func() {
zeroRTTHeader := make([]byte, 5)
zeroRTTHeader[0] = 0x80 | 0b10<<4
binary.BigEndian.PutUint32(zeroRTTHeader[1:], uint32(protocol.Version2))
Expect(Is0RTTPacket(zeroRTTHeader)).To(BeTrue())
Expect(Is0RTTPacket(zeroRTTHeader[:4])).To(BeFalse()) // too short
Expect(Is0RTTPacket([]byte{zeroRTTHeader[0], 1, 2, 3, 4})).To(BeFalse()) // unknown version
Expect(Is0RTTPacket([]byte{zeroRTTHeader[0] | 0x80, 1, 2, 3, 4})).To(BeFalse()) // short header
Expect(Is0RTTPacket(append(zeroRTTHeader, []byte("foobar")...))).To(BeTrue())
})
})
@ -245,8 +246,8 @@ var _ = Describe("Header Parsing", func() {
Expect(b.Len()).To(BeZero())
})
It("parses a Retry packet", func() {
data := []byte{0xc0 | 0x3<<4 | (10 - 3) /* connection ID length */}
It("parses a Retry packet, for QUIC v1", func() {
data := []byte{0xc0 | 0b11<<4 | (10 - 3) /* connection ID length */}
data = appendVersion(data, protocol.Version1)
data = append(data, []byte{6}...) // dest conn ID len
data = append(data, []byte{6, 5, 4, 3, 2, 1}...) // dest conn ID
@ -257,6 +258,27 @@ var _ = Describe("Header Parsing", func() {
hdr, pdata, rest, err := ParsePacket(data, 0)
Expect(err).ToNot(HaveOccurred())
Expect(hdr.Type).To(Equal(protocol.PacketTypeRetry))
Expect(hdr.Version).To(Equal(protocol.Version1))
Expect(hdr.DestConnectionID).To(Equal(protocol.ConnectionID{6, 5, 4, 3, 2, 1}))
Expect(hdr.SrcConnectionID).To(Equal(protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}))
Expect(hdr.Token).To(Equal([]byte("foobar")))
Expect(pdata).To(Equal(data))
Expect(rest).To(BeEmpty())
})
It("parses a Retry packet, for QUIC v2", func() {
data := []byte{0xc0 | 0b00<<4 | (10 - 3) /* connection ID length */}
data = appendVersion(data, protocol.Version2)
data = append(data, []byte{6}...) // dest conn ID len
data = append(data, []byte{6, 5, 4, 3, 2, 1}...) // dest conn ID
data = append(data, []byte{10}...) // src conn ID len
data = append(data, []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}...) // source connection ID
data = append(data, []byte{'f', 'o', 'o', 'b', 'a', 'r'}...) // token
data = append(data, []byte{16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}...)
hdr, pdata, rest, err := ParsePacket(data, 0)
Expect(err).ToNot(HaveOccurred())
Expect(hdr.Type).To(Equal(protocol.PacketTypeRetry))
Expect(hdr.Version).To(Equal(protocol.Version2))
Expect(hdr.DestConnectionID).To(Equal(protocol.ConnectionID{6, 5, 4, 3, 2, 1}))
Expect(hdr.SrcConnectionID).To(Equal(protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}))
Expect(hdr.Token).To(Equal([]byte("foobar")))

View file

@ -2,8 +2,10 @@ package wire
import (
"bytes"
"encoding/binary"
"testing"
"github.com/lucas-clemente/quic-go/internal/protocol"
"github.com/lucas-clemente/quic-go/quicvarint"
. "github.com/onsi/ginkgo"
@ -20,3 +22,10 @@ func encodeVarInt(i uint64) []byte {
quicvarint.Write(b, i)
return b.Bytes()
}
func appendVersion(data []byte, v protocol.VersionNumber) []byte {
offset := len(data)
data = append(data, []byte{0, 0, 0, 0}...)
binary.BigEndian.PutUint32(data[offset:], uint32(v))
return data
}