header_parser.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. package wire
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "github.com/lucas-clemente/quic-go/internal/protocol"
  7. "github.com/lucas-clemente/quic-go/internal/utils"
  8. "github.com/lucas-clemente/quic-go/qerr"
  9. )
  10. // The InvariantHeader is the version independent part of the header
  11. type InvariantHeader struct {
  12. IsLongHeader bool
  13. Version protocol.VersionNumber
  14. SrcConnectionID protocol.ConnectionID
  15. DestConnectionID protocol.ConnectionID
  16. typeByte byte
  17. }
  18. // ParseInvariantHeader parses the version independent part of the header
  19. func ParseInvariantHeader(b *bytes.Reader, shortHeaderConnIDLen int) (*InvariantHeader, error) {
  20. typeByte, err := b.ReadByte()
  21. if err != nil {
  22. return nil, err
  23. }
  24. h := &InvariantHeader{typeByte: typeByte}
  25. h.IsLongHeader = typeByte&0x80 > 0
  26. // If this is not a Long Header, it could either be a Public Header or a Short Header.
  27. if !h.IsLongHeader {
  28. // In the Public Header 0x8 is the Connection ID Flag.
  29. // In the IETF Short Header:
  30. // * 0x8 it is the gQUIC Demultiplexing bit, and always 0.
  31. // * 0x20 and 0x10 are always 1.
  32. var connIDLen int
  33. if typeByte&0x8 > 0 { // Public Header containing a connection ID
  34. connIDLen = 8
  35. }
  36. if typeByte&0x38 == 0x30 { // Short Header
  37. connIDLen = shortHeaderConnIDLen
  38. }
  39. if connIDLen > 0 {
  40. h.DestConnectionID, err = protocol.ReadConnectionID(b, connIDLen)
  41. if err != nil {
  42. return nil, err
  43. }
  44. }
  45. return h, nil
  46. }
  47. // Long Header
  48. v, err := utils.BigEndian.ReadUint32(b)
  49. if err != nil {
  50. return nil, err
  51. }
  52. h.Version = protocol.VersionNumber(v)
  53. connIDLenByte, err := b.ReadByte()
  54. if err != nil {
  55. return nil, err
  56. }
  57. dcil, scil := decodeConnIDLen(connIDLenByte)
  58. h.DestConnectionID, err = protocol.ReadConnectionID(b, dcil)
  59. if err != nil {
  60. return nil, err
  61. }
  62. h.SrcConnectionID, err = protocol.ReadConnectionID(b, scil)
  63. if err != nil {
  64. return nil, err
  65. }
  66. return h, nil
  67. }
  68. // Parse parses the version dependent part of the header
  69. func (iv *InvariantHeader) Parse(b *bytes.Reader, sentBy protocol.Perspective, ver protocol.VersionNumber) (*Header, error) {
  70. if iv.IsLongHeader {
  71. if iv.Version == 0 { // Version Negotiation Packet
  72. return iv.parseVersionNegotiationPacket(b)
  73. }
  74. return iv.parseLongHeader(b, sentBy, ver)
  75. }
  76. // The Public Header never uses 6 byte packet numbers.
  77. // Therefore, the third and fourth bit will never be 11.
  78. // For the Short Header, the third and fourth bit are always 11.
  79. if iv.typeByte&0x30 != 0x30 {
  80. if sentBy == protocol.PerspectiveServer && iv.typeByte&0x1 > 0 {
  81. return iv.parseVersionNegotiationPacket(b)
  82. }
  83. return iv.parsePublicHeader(b, sentBy, ver)
  84. }
  85. return iv.parseShortHeader(b, ver)
  86. }
  87. func (iv *InvariantHeader) toHeader() *Header {
  88. return &Header{
  89. IsLongHeader: iv.IsLongHeader,
  90. DestConnectionID: iv.DestConnectionID,
  91. SrcConnectionID: iv.SrcConnectionID,
  92. Version: iv.Version,
  93. }
  94. }
  95. func (iv *InvariantHeader) parseVersionNegotiationPacket(b *bytes.Reader) (*Header, error) {
  96. h := iv.toHeader()
  97. h.VersionFlag = true
  98. if b.Len() == 0 {
  99. return nil, qerr.Error(qerr.InvalidVersionNegotiationPacket, "empty version list")
  100. }
  101. h.IsVersionNegotiation = true
  102. h.SupportedVersions = make([]protocol.VersionNumber, b.Len()/4)
  103. for i := 0; b.Len() > 0; i++ {
  104. v, err := utils.BigEndian.ReadUint32(b)
  105. if err != nil {
  106. return nil, qerr.InvalidVersionNegotiationPacket
  107. }
  108. h.SupportedVersions[i] = protocol.VersionNumber(v)
  109. }
  110. return h, nil
  111. }
  112. func (iv *InvariantHeader) parseLongHeader(b *bytes.Reader, sentBy protocol.Perspective, v protocol.VersionNumber) (*Header, error) {
  113. h := iv.toHeader()
  114. h.Type = protocol.PacketType(iv.typeByte & 0x7f)
  115. if h.Type != protocol.PacketTypeInitial && h.Type != protocol.PacketTypeRetry && h.Type != protocol.PacketType0RTT && h.Type != protocol.PacketTypeHandshake {
  116. return nil, qerr.Error(qerr.InvalidPacketHeader, fmt.Sprintf("Received packet with invalid packet type: %d", h.Type))
  117. }
  118. if h.Type == protocol.PacketTypeRetry {
  119. odcilByte, err := b.ReadByte()
  120. if err != nil {
  121. return nil, err
  122. }
  123. odcil := decodeSingleConnIDLen(odcilByte & 0xf)
  124. h.OrigDestConnectionID, err = protocol.ReadConnectionID(b, odcil)
  125. if err != nil {
  126. return nil, err
  127. }
  128. h.Token = make([]byte, b.Len())
  129. if _, err := io.ReadFull(b, h.Token); err != nil {
  130. return nil, err
  131. }
  132. return h, nil
  133. }
  134. if h.Type == protocol.PacketTypeInitial && v.UsesTokenInHeader() {
  135. tokenLen, err := utils.ReadVarInt(b)
  136. if err != nil {
  137. return nil, err
  138. }
  139. if tokenLen > uint64(b.Len()) {
  140. return nil, io.EOF
  141. }
  142. h.Token = make([]byte, tokenLen)
  143. if _, err := io.ReadFull(b, h.Token); err != nil {
  144. return nil, err
  145. }
  146. }
  147. if v.UsesLengthInHeader() {
  148. pl, err := utils.ReadVarInt(b)
  149. if err != nil {
  150. return nil, err
  151. }
  152. h.PayloadLen = protocol.ByteCount(pl)
  153. }
  154. if v.UsesVarintPacketNumbers() {
  155. pn, pnLen, err := utils.ReadVarIntPacketNumber(b)
  156. if err != nil {
  157. return nil, err
  158. }
  159. h.PacketNumber = pn
  160. h.PacketNumberLen = pnLen
  161. } else {
  162. pn, err := utils.BigEndian.ReadUint32(b)
  163. if err != nil {
  164. return nil, err
  165. }
  166. h.PacketNumber = protocol.PacketNumber(pn)
  167. h.PacketNumberLen = protocol.PacketNumberLen4
  168. }
  169. if h.Type == protocol.PacketType0RTT && v == protocol.Version44 && sentBy == protocol.PerspectiveServer {
  170. h.DiversificationNonce = make([]byte, 32)
  171. if _, err := io.ReadFull(b, h.DiversificationNonce); err != nil {
  172. if err == io.ErrUnexpectedEOF {
  173. return nil, io.EOF
  174. }
  175. return nil, err
  176. }
  177. }
  178. return h, nil
  179. }
  180. func (iv *InvariantHeader) parseShortHeader(b *bytes.Reader, v protocol.VersionNumber) (*Header, error) {
  181. h := iv.toHeader()
  182. h.KeyPhase = int(iv.typeByte&0x40) >> 6
  183. if v.UsesVarintPacketNumbers() {
  184. pn, pnLen, err := utils.ReadVarIntPacketNumber(b)
  185. if err != nil {
  186. return nil, err
  187. }
  188. h.PacketNumber = pn
  189. h.PacketNumberLen = pnLen
  190. } else {
  191. switch iv.typeByte & 0x3 {
  192. case 0x0:
  193. h.PacketNumberLen = protocol.PacketNumberLen1
  194. case 0x1:
  195. h.PacketNumberLen = protocol.PacketNumberLen2
  196. case 0x2:
  197. h.PacketNumberLen = protocol.PacketNumberLen4
  198. default:
  199. return nil, errInvalidPacketNumberLen
  200. }
  201. p, err := utils.BigEndian.ReadUintN(b, uint8(h.PacketNumberLen))
  202. if err != nil {
  203. return nil, err
  204. }
  205. h.PacketNumber = protocol.PacketNumber(p)
  206. }
  207. return h, nil
  208. }
  209. func (iv *InvariantHeader) parsePublicHeader(b *bytes.Reader, sentBy protocol.Perspective, ver protocol.VersionNumber) (*Header, error) {
  210. h := iv.toHeader()
  211. h.IsPublicHeader = true
  212. h.ResetFlag = iv.typeByte&0x2 > 0
  213. if h.ResetFlag {
  214. return h, nil
  215. }
  216. h.VersionFlag = iv.typeByte&0x1 > 0
  217. if h.VersionFlag && sentBy == protocol.PerspectiveClient {
  218. v, err := utils.BigEndian.ReadUint32(b)
  219. if err != nil {
  220. return nil, err
  221. }
  222. h.Version = protocol.VersionNumber(v)
  223. }
  224. // Contrary to what the gQUIC wire spec says, the 0x4 bit only indicates the presence of the diversification nonce for packets sent by the server.
  225. // It doesn't have any meaning when sent by the client.
  226. if sentBy == protocol.PerspectiveServer && iv.typeByte&0x4 > 0 {
  227. h.DiversificationNonce = make([]byte, 32)
  228. if _, err := io.ReadFull(b, h.DiversificationNonce); err != nil {
  229. if err == io.ErrUnexpectedEOF {
  230. return nil, io.EOF
  231. }
  232. return nil, err
  233. }
  234. }
  235. switch iv.typeByte & 0x30 {
  236. case 0x00:
  237. h.PacketNumberLen = protocol.PacketNumberLen1
  238. case 0x10:
  239. h.PacketNumberLen = protocol.PacketNumberLen2
  240. case 0x20:
  241. h.PacketNumberLen = protocol.PacketNumberLen4
  242. }
  243. pn, err := utils.BigEndian.ReadUintN(b, uint8(h.PacketNumberLen))
  244. if err != nil {
  245. return nil, err
  246. }
  247. h.PacketNumber = protocol.PacketNumber(pn)
  248. return h, nil
  249. }