forked from quic-go/quic-go
parse arbitrary length Connection IDs in Version Negotiation packets
This commit is contained in:
@@ -3,6 +3,7 @@ package wire
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/rand"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
|
||||
"github.com/lucas-clemente/quic-go/internal/protocol"
|
||||
@@ -10,32 +11,30 @@ import (
|
||||
)
|
||||
|
||||
// ParseVersionNegotiationPacket parses a Version Negotiation packet.
|
||||
func ParseVersionNegotiationPacket(b *bytes.Reader) (*Header, []protocol.VersionNumber, error) {
|
||||
hdr, err := parseHeader(b, 0)
|
||||
func ParseVersionNegotiationPacket(b []byte) (dest, src protocol.ArbitraryLenConnectionID, _ []protocol.VersionNumber, _ error) {
|
||||
n, dest, src, err := ParseArbitraryLenConnectionIDs(b)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
if b.Len() == 0 {
|
||||
b = b[n:]
|
||||
if len(b) == 0 {
|
||||
//nolint:stylecheck
|
||||
return nil, nil, errors.New("Version Negotiation packet has empty version list")
|
||||
return nil, nil, nil, errors.New("Version Negotiation packet has empty version list")
|
||||
}
|
||||
if b.Len()%4 != 0 {
|
||||
if len(b)%4 != 0 {
|
||||
//nolint:stylecheck
|
||||
return nil, nil, errors.New("Version Negotiation packet has a version list with an invalid length")
|
||||
return nil, nil, nil, errors.New("Version Negotiation packet has a version list with an invalid length")
|
||||
}
|
||||
versions := make([]protocol.VersionNumber, b.Len()/4)
|
||||
for i := 0; b.Len() > 0; i++ {
|
||||
v, err := utils.BigEndian.ReadUint32(b)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
versions[i] = protocol.VersionNumber(v)
|
||||
versions := make([]protocol.VersionNumber, len(b)/4)
|
||||
for i := 0; len(b) > 0; i++ {
|
||||
versions[i] = protocol.VersionNumber(binary.BigEndian.Uint32(b[:4]))
|
||||
b = b[4:]
|
||||
}
|
||||
return hdr, versions, nil
|
||||
return dest, src, versions, nil
|
||||
}
|
||||
|
||||
// ComposeVersionNegotiation composes a Version Negotiation
|
||||
func ComposeVersionNegotiation(destConnID, srcConnID protocol.ConnectionID, versions []protocol.VersionNumber) []byte {
|
||||
func ComposeVersionNegotiation(destConnID, srcConnID protocol.ArbitraryLenConnectionID, versions []protocol.VersionNumber) []byte {
|
||||
greasedVersions := protocol.GetGreasedVersions(versions)
|
||||
expectedLen := 1 /* type byte */ + 4 /* version field */ + 1 /* dest connection ID length field */ + destConnID.Len() + 1 /* src connection ID length field */ + srcConnID.Len() + len(greasedVersions)*4
|
||||
buf := bytes.NewBuffer(make([]byte, 0, expectedLen))
|
||||
@@ -44,9 +43,9 @@ func ComposeVersionNegotiation(destConnID, srcConnID protocol.ConnectionID, vers
|
||||
buf.WriteByte(r[0] | 0x80)
|
||||
utils.BigEndian.WriteUint32(buf, 0) // version 0
|
||||
buf.WriteByte(uint8(destConnID.Len()))
|
||||
buf.Write(destConnID)
|
||||
buf.Write(destConnID.Bytes())
|
||||
buf.WriteByte(uint8(srcConnID.Len()))
|
||||
buf.Write(srcConnID)
|
||||
buf.Write(srcConnID.Bytes())
|
||||
for _, v := range greasedVersions {
|
||||
utils.BigEndian.WriteUint32(buf, uint32(v))
|
||||
}
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
package wire
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
mrand "math/rand"
|
||||
|
||||
"golang.org/x/exp/rand"
|
||||
|
||||
"github.com/lucas-clemente/quic-go/internal/protocol"
|
||||
. "github.com/onsi/ginkgo"
|
||||
@@ -10,9 +12,16 @@ import (
|
||||
)
|
||||
|
||||
var _ = Describe("Version Negotiation Packets", func() {
|
||||
randConnID := func(l int) protocol.ArbitraryLenConnectionID {
|
||||
b := make(protocol.ArbitraryLenConnectionID, l)
|
||||
_, err := mrand.Read(b)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
return b
|
||||
}
|
||||
|
||||
It("parses a Version Negotiation packet", func() {
|
||||
srcConnID := protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
|
||||
destConnID := protocol.ConnectionID{9, 8, 7, 6, 5, 4, 3, 2, 1}
|
||||
srcConnID := randConnID(rand.Intn(255) + 1)
|
||||
destConnID := randConnID(rand.Intn(255) + 1)
|
||||
versions := []protocol.VersionNumber{0x22334455, 0x33445566}
|
||||
data := []byte{0x80, 0, 0, 0, 0}
|
||||
data = append(data, uint8(len(destConnID)))
|
||||
@@ -24,44 +33,44 @@ var _ = Describe("Version Negotiation Packets", func() {
|
||||
binary.BigEndian.PutUint32(data[len(data)-4:], uint32(v))
|
||||
}
|
||||
Expect(IsVersionNegotiationPacket(data)).To(BeTrue())
|
||||
hdr, supportedVersions, err := ParseVersionNegotiationPacket(bytes.NewReader(data))
|
||||
dest, src, supportedVersions, err := ParseVersionNegotiationPacket(data)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(hdr.DestConnectionID).To(Equal(destConnID))
|
||||
Expect(hdr.SrcConnectionID).To(Equal(srcConnID))
|
||||
Expect(hdr.IsLongHeader).To(BeTrue())
|
||||
Expect(hdr.Version).To(BeZero())
|
||||
Expect(dest).To(Equal(destConnID))
|
||||
Expect(src).To(Equal(srcConnID))
|
||||
Expect(supportedVersions).To(Equal(versions))
|
||||
})
|
||||
|
||||
It("errors if it contains versions of the wrong length", func() {
|
||||
connID := protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}
|
||||
connID := protocol.ArbitraryLenConnectionID{1, 2, 3, 4, 5, 6, 7, 8}
|
||||
versions := []protocol.VersionNumber{0x22334455, 0x33445566}
|
||||
data := ComposeVersionNegotiation(connID, connID, versions)
|
||||
_, _, err := ParseVersionNegotiationPacket(bytes.NewReader(data[:len(data)-2]))
|
||||
_, _, _, err := ParseVersionNegotiationPacket(data[:len(data)-2])
|
||||
Expect(err).To(MatchError("Version Negotiation packet has a version list with an invalid length"))
|
||||
})
|
||||
|
||||
It("errors if the version list is empty", func() {
|
||||
connID := protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}
|
||||
connID := protocol.ArbitraryLenConnectionID{1, 2, 3, 4, 5, 6, 7, 8}
|
||||
versions := []protocol.VersionNumber{0x22334455}
|
||||
data := ComposeVersionNegotiation(connID, connID, versions)
|
||||
// remove 8 bytes (two versions), since ComposeVersionNegotiation also added a reserved version number
|
||||
data = data[:len(data)-8]
|
||||
_, _, err := ParseVersionNegotiationPacket(bytes.NewReader(data))
|
||||
_, _, _, err := ParseVersionNegotiationPacket(data)
|
||||
Expect(err).To(MatchError("Version Negotiation packet has empty version list"))
|
||||
})
|
||||
|
||||
It("adds a reserved version", func() {
|
||||
srcConnID := protocol.ConnectionID{0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0x13, 0x37}
|
||||
destConnID := protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}
|
||||
srcConnID := protocol.ArbitraryLenConnectionID{0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0x13, 0x37}
|
||||
destConnID := protocol.ArbitraryLenConnectionID{1, 2, 3, 4, 5, 6, 7, 8}
|
||||
versions := []protocol.VersionNumber{1001, 1003}
|
||||
data := ComposeVersionNegotiation(destConnID, srcConnID, versions)
|
||||
Expect(IsLongHeaderPacket(data[0])).To(BeTrue())
|
||||
hdr, supportedVersions, err := ParseVersionNegotiationPacket(bytes.NewReader(data))
|
||||
v, err := ParseVersion(data)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(hdr.DestConnectionID).To(Equal(destConnID))
|
||||
Expect(hdr.SrcConnectionID).To(Equal(srcConnID))
|
||||
Expect(hdr.Version).To(BeZero())
|
||||
Expect(v).To(BeZero())
|
||||
dest, src, supportedVersions, err := ParseVersionNegotiationPacket(data)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(dest).To(Equal(destConnID))
|
||||
Expect(src).To(Equal(srcConnID))
|
||||
// the supported versions should include one reserved version number
|
||||
Expect(supportedVersions).To(HaveLen(len(versions) + 1))
|
||||
for _, v := range versions {
|
||||
|
||||
Reference in New Issue
Block a user