2018-01-03 19:19:49 +00:00
|
|
|
package wire
|
2017-12-12 02:51:45 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
2018-01-03 19:19:49 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2017-12-12 02:51:45 +00:00
|
|
|
|
2018-01-03 19:19:49 +00:00
|
|
|
"github.com/lucas-clemente/quic-go/internal/protocol"
|
|
|
|
"github.com/lucas-clemente/quic-go/internal/utils"
|
2017-12-12 02:51:45 +00:00
|
|
|
"github.com/lucas-clemente/quic-go/qerr"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
errResetAndVersionFlagSet = errors.New("PublicHeader: Reset Flag and Version Flag should not be set at the same time")
|
2018-01-03 19:19:49 +00:00
|
|
|
errReceivedOmittedConnectionID = qerr.Error(qerr.InvalidPacketHeader, "receiving packets with omitted ConnectionID is not supported")
|
2017-12-12 02:51:45 +00:00
|
|
|
errInvalidConnectionID = qerr.Error(qerr.InvalidPacketHeader, "connection ID cannot be 0")
|
|
|
|
errGetLengthNotForVersionNegotiation = errors.New("PublicHeader: GetLength cannot be called for VersionNegotiation packets")
|
|
|
|
)
|
|
|
|
|
2018-01-03 19:19:49 +00:00
|
|
|
// writePublicHeader writes a Public Header.
|
|
|
|
func (h *Header) writePublicHeader(b *bytes.Buffer, pers protocol.Perspective, _ protocol.VersionNumber) error {
|
2017-12-12 02:51:45 +00:00
|
|
|
if h.VersionFlag && h.ResetFlag {
|
|
|
|
return errResetAndVersionFlagSet
|
|
|
|
}
|
|
|
|
|
2018-01-03 19:19:49 +00:00
|
|
|
publicFlagByte := uint8(0x00)
|
2017-12-12 02:51:45 +00:00
|
|
|
if h.VersionFlag {
|
|
|
|
publicFlagByte |= 0x01
|
|
|
|
}
|
|
|
|
if h.ResetFlag {
|
|
|
|
publicFlagByte |= 0x02
|
|
|
|
}
|
2018-01-03 19:19:49 +00:00
|
|
|
if !h.OmitConnectionID {
|
2017-12-12 02:51:45 +00:00
|
|
|
publicFlagByte |= 0x08
|
|
|
|
}
|
|
|
|
if len(h.DiversificationNonce) > 0 {
|
|
|
|
if len(h.DiversificationNonce) != 32 {
|
|
|
|
return errors.New("invalid diversification nonce length")
|
|
|
|
}
|
|
|
|
publicFlagByte |= 0x04
|
|
|
|
}
|
|
|
|
// only set PacketNumberLen bits if a packet number will be written
|
|
|
|
if h.hasPacketNumber(pers) {
|
|
|
|
switch h.PacketNumberLen {
|
|
|
|
case protocol.PacketNumberLen1:
|
|
|
|
publicFlagByte |= 0x00
|
|
|
|
case protocol.PacketNumberLen2:
|
|
|
|
publicFlagByte |= 0x10
|
|
|
|
case protocol.PacketNumberLen4:
|
|
|
|
publicFlagByte |= 0x20
|
|
|
|
case protocol.PacketNumberLen6:
|
|
|
|
publicFlagByte |= 0x30
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b.WriteByte(publicFlagByte)
|
|
|
|
|
2018-01-03 19:19:49 +00:00
|
|
|
if !h.OmitConnectionID {
|
|
|
|
utils.BigEndian.WriteUint64(b, uint64(h.ConnectionID))
|
2017-12-12 02:51:45 +00:00
|
|
|
}
|
|
|
|
if h.VersionFlag && pers == protocol.PerspectiveClient {
|
2018-01-03 19:19:49 +00:00
|
|
|
utils.BigEndian.WriteUint32(b, uint32(h.Version))
|
2017-12-12 02:51:45 +00:00
|
|
|
}
|
|
|
|
if len(h.DiversificationNonce) > 0 {
|
|
|
|
b.Write(h.DiversificationNonce)
|
|
|
|
}
|
|
|
|
// if we're a server, and the VersionFlag is set, we must not include anything else in the packet
|
|
|
|
if !h.hasPacketNumber(pers) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch h.PacketNumberLen {
|
|
|
|
case protocol.PacketNumberLen1:
|
|
|
|
b.WriteByte(uint8(h.PacketNumber))
|
|
|
|
case protocol.PacketNumberLen2:
|
2018-01-03 19:19:49 +00:00
|
|
|
utils.BigEndian.WriteUint16(b, uint16(h.PacketNumber))
|
2017-12-12 02:51:45 +00:00
|
|
|
case protocol.PacketNumberLen4:
|
2018-01-03 19:19:49 +00:00
|
|
|
utils.BigEndian.WriteUint32(b, uint32(h.PacketNumber))
|
2017-12-12 02:51:45 +00:00
|
|
|
case protocol.PacketNumberLen6:
|
2018-01-03 19:19:49 +00:00
|
|
|
utils.BigEndian.WriteUint48(b, uint64(h.PacketNumber)&(1<<48-1))
|
2017-12-12 02:51:45 +00:00
|
|
|
default:
|
2018-01-03 19:19:49 +00:00
|
|
|
return errors.New("PublicHeader: PacketNumberLen not set")
|
2017-12-12 02:51:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-01-03 19:19:49 +00:00
|
|
|
// parsePublicHeader parses a QUIC packet's Public Header.
|
2017-12-12 02:51:45 +00:00
|
|
|
// The packetSentBy is the perspective of the peer that sent this PublicHeader, i.e. if we're the server, packetSentBy should be PerspectiveClient.
|
2018-01-03 19:19:49 +00:00
|
|
|
func parsePublicHeader(b *bytes.Reader, packetSentBy protocol.Perspective) (*Header, error) {
|
|
|
|
header := &Header{}
|
2017-12-12 02:51:45 +00:00
|
|
|
|
|
|
|
// First byte
|
|
|
|
publicFlagByte, err := b.ReadByte()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
header.ResetFlag = publicFlagByte&0x02 > 0
|
2018-01-03 19:19:49 +00:00
|
|
|
header.VersionFlag = publicFlagByte&0x01 > 0
|
2017-12-12 02:51:45 +00:00
|
|
|
|
|
|
|
// TODO: activate this check once Chrome sends the correct value
|
|
|
|
// see https://github.com/lucas-clemente/quic-go/issues/232
|
|
|
|
// if publicFlagByte&0x04 > 0 {
|
|
|
|
// return nil, errors.New("diversification nonces should only be sent by servers")
|
|
|
|
// }
|
|
|
|
|
2018-01-03 19:19:49 +00:00
|
|
|
header.OmitConnectionID = publicFlagByte&0x08 == 0
|
|
|
|
if header.OmitConnectionID && packetSentBy == protocol.PerspectiveClient {
|
|
|
|
return nil, errReceivedOmittedConnectionID
|
2017-12-12 02:51:45 +00:00
|
|
|
}
|
|
|
|
if header.hasPacketNumber(packetSentBy) {
|
|
|
|
switch publicFlagByte & 0x30 {
|
|
|
|
case 0x30:
|
|
|
|
header.PacketNumberLen = protocol.PacketNumberLen6
|
|
|
|
case 0x20:
|
|
|
|
header.PacketNumberLen = protocol.PacketNumberLen4
|
|
|
|
case 0x10:
|
|
|
|
header.PacketNumberLen = protocol.PacketNumberLen2
|
|
|
|
case 0x00:
|
|
|
|
header.PacketNumberLen = protocol.PacketNumberLen1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connection ID
|
2018-01-03 19:19:49 +00:00
|
|
|
if !header.OmitConnectionID {
|
2017-12-12 02:51:45 +00:00
|
|
|
var connID uint64
|
2018-01-03 19:19:49 +00:00
|
|
|
connID, err = utils.BigEndian.ReadUint64(b)
|
2017-12-12 02:51:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
header.ConnectionID = protocol.ConnectionID(connID)
|
|
|
|
if header.ConnectionID == 0 {
|
|
|
|
return nil, errInvalidConnectionID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if packetSentBy == protocol.PerspectiveServer && publicFlagByte&0x04 > 0 {
|
|
|
|
// TODO: remove the if once the Google servers send the correct value
|
|
|
|
// assume that a packet doesn't contain a diversification nonce if the version flag or the reset flag is set, no matter what the public flag says
|
|
|
|
// see https://github.com/lucas-clemente/quic-go/issues/232
|
|
|
|
if !header.VersionFlag && !header.ResetFlag {
|
|
|
|
header.DiversificationNonce = make([]byte, 32)
|
2018-01-03 19:19:49 +00:00
|
|
|
if _, err := io.ReadFull(b, header.DiversificationNonce); err != nil {
|
2017-12-12 02:51:45 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Version (optional)
|
2018-01-03 19:19:49 +00:00
|
|
|
if !header.ResetFlag && header.VersionFlag {
|
|
|
|
if packetSentBy == protocol.PerspectiveServer { // parse the version negotiaton packet
|
|
|
|
if b.Len() == 0 {
|
|
|
|
return nil, qerr.Error(qerr.InvalidVersionNegotiationPacket, "empty version list")
|
|
|
|
}
|
|
|
|
if b.Len()%4 != 0 {
|
|
|
|
return nil, qerr.InvalidVersionNegotiationPacket
|
|
|
|
}
|
|
|
|
header.IsVersionNegotiation = true
|
|
|
|
header.SupportedVersions = make([]protocol.VersionNumber, 0)
|
|
|
|
for {
|
2017-12-12 02:51:45 +00:00
|
|
|
var versionTag uint32
|
2018-01-03 19:19:49 +00:00
|
|
|
versionTag, err = utils.BigEndian.ReadUint32(b)
|
2017-12-12 02:51:45 +00:00
|
|
|
if err != nil {
|
2018-01-03 19:19:49 +00:00
|
|
|
break
|
2017-12-12 02:51:45 +00:00
|
|
|
}
|
2018-01-03 19:19:49 +00:00
|
|
|
v := protocol.VersionNumber(versionTag)
|
|
|
|
header.SupportedVersions = append(header.SupportedVersions, v)
|
2017-12-12 02:51:45 +00:00
|
|
|
}
|
2018-01-03 19:19:49 +00:00
|
|
|
// a version negotiation packet doesn't have a packet number
|
|
|
|
return header, nil
|
2017-12-12 02:51:45 +00:00
|
|
|
}
|
2018-01-03 19:19:49 +00:00
|
|
|
// packet was sent by the client. Read the version number
|
|
|
|
var versionTag uint32
|
|
|
|
versionTag, err = utils.BigEndian.ReadUint32(b)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
header.Version = protocol.VersionNumber(versionTag)
|
2017-12-12 02:51:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Packet number
|
|
|
|
if header.hasPacketNumber(packetSentBy) {
|
2018-01-03 19:19:49 +00:00
|
|
|
packetNumber, err := utils.BigEndian.ReadUintN(b, uint8(header.PacketNumberLen))
|
2017-12-12 02:51:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
header.PacketNumber = protocol.PacketNumber(packetNumber)
|
|
|
|
}
|
|
|
|
return header, nil
|
|
|
|
}
|
|
|
|
|
2018-01-03 19:19:49 +00:00
|
|
|
// getPublicHeaderLength gets the length of the publicHeader in bytes.
|
2017-12-12 02:51:45 +00:00
|
|
|
// It can only be called for regular packets.
|
2018-01-03 19:19:49 +00:00
|
|
|
func (h *Header) getPublicHeaderLength(pers protocol.Perspective) (protocol.ByteCount, error) {
|
2017-12-12 02:51:45 +00:00
|
|
|
if h.VersionFlag && h.ResetFlag {
|
|
|
|
return 0, errResetAndVersionFlagSet
|
|
|
|
}
|
|
|
|
if h.VersionFlag && pers == protocol.PerspectiveServer {
|
|
|
|
return 0, errGetLengthNotForVersionNegotiation
|
|
|
|
}
|
|
|
|
|
|
|
|
length := protocol.ByteCount(1) // 1 byte for public flags
|
|
|
|
if h.hasPacketNumber(pers) {
|
|
|
|
if h.PacketNumberLen != protocol.PacketNumberLen1 && h.PacketNumberLen != protocol.PacketNumberLen2 && h.PacketNumberLen != protocol.PacketNumberLen4 && h.PacketNumberLen != protocol.PacketNumberLen6 {
|
|
|
|
return 0, errPacketNumberLenNotSet
|
|
|
|
}
|
|
|
|
length += protocol.ByteCount(h.PacketNumberLen)
|
|
|
|
}
|
2018-01-03 19:19:49 +00:00
|
|
|
if !h.OmitConnectionID {
|
2017-12-12 02:51:45 +00:00
|
|
|
length += 8 // 8 bytes for the connection ID
|
|
|
|
}
|
|
|
|
// Version Number in packets sent by the client
|
|
|
|
if h.VersionFlag {
|
|
|
|
length += 4
|
|
|
|
}
|
|
|
|
length += protocol.ByteCount(len(h.DiversificationNonce))
|
|
|
|
return length, nil
|
|
|
|
}
|
|
|
|
|
2018-01-03 19:19:49 +00:00
|
|
|
// hasPacketNumber determines if this Public Header will contain a packet number
|
2017-12-12 02:51:45 +00:00
|
|
|
// this depends on the ResetFlag, the VersionFlag and who sent the packet
|
2018-01-03 19:19:49 +00:00
|
|
|
func (h *Header) hasPacketNumber(packetSentBy protocol.Perspective) bool {
|
2017-12-12 02:51:45 +00:00
|
|
|
if h.ResetFlag {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if h.VersionFlag && packetSentBy == protocol.PerspectiveServer {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2018-01-03 19:19:49 +00:00
|
|
|
|
|
|
|
func (h *Header) logPublicHeader() {
|
|
|
|
connID := "(omitted)"
|
|
|
|
if !h.OmitConnectionID {
|
|
|
|
connID = fmt.Sprintf("%#x", h.ConnectionID)
|
|
|
|
}
|
|
|
|
ver := "(unset)"
|
|
|
|
if h.Version != 0 {
|
|
|
|
ver = fmt.Sprintf("%s", h.Version)
|
|
|
|
}
|
|
|
|
utils.Debugf(" Public Header{ConnectionID: %s, PacketNumber: %#x, PacketNumberLen: %d, Version: %s, DiversificationNonce: %#v}", connID, h.PacketNumber, h.PacketNumberLen, ver, h.DiversificationNonce)
|
|
|
|
}
|