header_parser_test.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. package wire
  2. import (
  3. "bytes"
  4. "encoding/binary"
  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. . "github.com/onsi/ginkgo"
  10. . "github.com/onsi/gomega"
  11. )
  12. var _ = Describe("Header Parsing", func() {
  13. Context("IETF QUIC Header", func() {
  14. appendPacketNumber := func(data []byte, pn protocol.PacketNumber, pnLen protocol.PacketNumberLen) []byte {
  15. buf := &bytes.Buffer{}
  16. utils.WriteVarIntPacketNumber(buf, pn, pnLen)
  17. return append(data, buf.Bytes()...)
  18. }
  19. Context("Version Negotiation Packets", func() {
  20. It("parses", func() {
  21. srcConnID := protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
  22. destConnID := protocol.ConnectionID{9, 8, 7, 6, 5, 4, 3, 2, 1}
  23. versions := []protocol.VersionNumber{0x22334455, 0x33445566}
  24. data, err := ComposeVersionNegotiation(destConnID, srcConnID, versions)
  25. Expect(err).ToNot(HaveOccurred())
  26. b := bytes.NewReader(data)
  27. iHdr, err := ParseInvariantHeader(b, 0)
  28. Expect(err).ToNot(HaveOccurred())
  29. Expect(iHdr.DestConnectionID).To(Equal(destConnID))
  30. Expect(iHdr.SrcConnectionID).To(Equal(srcConnID))
  31. Expect(iHdr.IsLongHeader).To(BeTrue())
  32. hdr, err := iHdr.Parse(b, protocol.PerspectiveServer, versionIETFFrames)
  33. Expect(err).ToNot(HaveOccurred())
  34. Expect(hdr.IsVersionNegotiation).To(BeTrue())
  35. Expect(hdr.Version).To(BeZero())
  36. Expect(hdr.DestConnectionID).To(Equal(destConnID))
  37. Expect(hdr.SrcConnectionID).To(Equal(srcConnID))
  38. for _, v := range versions {
  39. Expect(hdr.SupportedVersions).To(ContainElement(v))
  40. }
  41. })
  42. It("errors if it contains versions of the wrong length", func() {
  43. connID := protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}
  44. versions := []protocol.VersionNumber{0x22334455, 0x33445566}
  45. data, err := ComposeVersionNegotiation(connID, connID, versions)
  46. Expect(err).ToNot(HaveOccurred())
  47. b := bytes.NewReader(data[:len(data)-2])
  48. iHdr, err := ParseInvariantHeader(b, 0)
  49. Expect(err).ToNot(HaveOccurred())
  50. _, err = iHdr.Parse(b, protocol.PerspectiveServer, versionIETFFrames)
  51. Expect(err).To(MatchError(qerr.InvalidVersionNegotiationPacket))
  52. })
  53. It("errors if the version list is empty", func() {
  54. connID := protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}
  55. versions := []protocol.VersionNumber{0x22334455}
  56. data, err := ComposeVersionNegotiation(connID, connID, versions)
  57. Expect(err).ToNot(HaveOccurred())
  58. // remove 8 bytes (two versions), since ComposeVersionNegotiation also added a reserved version number
  59. b := bytes.NewReader(data[:len(data)-8])
  60. iHdr, err := ParseInvariantHeader(b, 0)
  61. Expect(err).ToNot(HaveOccurred())
  62. _, err = iHdr.Parse(b, protocol.PerspectiveServer, versionIETFFrames)
  63. Expect(err).To(MatchError("InvalidVersionNegotiationPacket: empty version list"))
  64. })
  65. })
  66. Context("Long Headers", func() {
  67. It("parses a Long Header", func() {
  68. destConnID := protocol.ConnectionID{9, 8, 7, 6, 5, 4, 3, 2, 1}
  69. srcConnID := protocol.ConnectionID{0xde, 0xad, 0xbe, 0xef}
  70. data := []byte{
  71. 0x80 ^ uint8(protocol.PacketTypeInitial),
  72. 0x1, 0x2, 0x3, 0x4, // version number
  73. 0x61, // connection ID lengths
  74. }
  75. data = append(data, destConnID...)
  76. data = append(data, srcConnID...)
  77. data = append(data, encodeVarInt(6)...) // token length
  78. data = append(data, []byte("foobar")...) // token
  79. data = append(data, encodeVarInt(0x1337)...) // payload length
  80. // packet number
  81. data = appendPacketNumber(data, 0xbeef, protocol.PacketNumberLen4)
  82. b := bytes.NewReader(data)
  83. iHdr, err := ParseInvariantHeader(b, 0)
  84. Expect(err).ToNot(HaveOccurred())
  85. Expect(iHdr.IsLongHeader).To(BeTrue())
  86. Expect(iHdr.DestConnectionID).To(Equal(destConnID))
  87. Expect(iHdr.SrcConnectionID).To(Equal(srcConnID))
  88. hdr, err := iHdr.Parse(b, protocol.PerspectiveServer, versionIETFHeader)
  89. Expect(err).ToNot(HaveOccurred())
  90. Expect(hdr.Type).To(Equal(protocol.PacketTypeInitial))
  91. Expect(hdr.IsLongHeader).To(BeTrue())
  92. Expect(hdr.DestConnectionID).To(Equal(destConnID))
  93. Expect(hdr.SrcConnectionID).To(Equal(srcConnID))
  94. Expect(hdr.Token).To(Equal([]byte("foobar")))
  95. Expect(hdr.PayloadLen).To(Equal(protocol.ByteCount(0x1337)))
  96. Expect(hdr.PacketNumber).To(Equal(protocol.PacketNumber(0xbeef)))
  97. Expect(hdr.PacketNumberLen).To(Equal(protocol.PacketNumberLen4))
  98. Expect(hdr.Version).To(Equal(protocol.VersionNumber(0x1020304)))
  99. Expect(hdr.IsVersionNegotiation).To(BeFalse())
  100. Expect(b.Len()).To(BeZero())
  101. })
  102. It("parses a Long Header without a destination connection ID", func() {
  103. data := []byte{
  104. 0x80 ^ uint8(protocol.PacketTypeInitial),
  105. 0x1, 0x2, 0x3, 0x4, // version number
  106. 0x01, // connection ID lengths
  107. 0xde, 0xad, 0xbe, 0xef, // source connection ID
  108. }
  109. data = append(data, encodeVarInt(0x42)...) // payload length
  110. data = append(data, []byte{0xde, 0xca, 0xfb, 0xad}...)
  111. b := bytes.NewReader(data)
  112. iHdr, err := ParseInvariantHeader(b, 0)
  113. Expect(err).ToNot(HaveOccurred())
  114. Expect(iHdr.SrcConnectionID).To(Equal(protocol.ConnectionID{0xde, 0xad, 0xbe, 0xef}))
  115. Expect(iHdr.DestConnectionID).To(BeEmpty())
  116. })
  117. It("parses a Long Header without a source connection ID", func() {
  118. data := []byte{
  119. 0x80 ^ uint8(protocol.PacketTypeInitial),
  120. 0x1, 0x2, 0x3, 0x4, // version number
  121. 0x70, // connection ID lengths
  122. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, // source connection ID
  123. }
  124. data = append(data, encodeVarInt(0x42)...) // payload length
  125. data = append(data, []byte{0xde, 0xca, 0xfb, 0xad}...)
  126. b := bytes.NewReader(data)
  127. iHdr, err := ParseInvariantHeader(b, 0)
  128. Expect(err).ToNot(HaveOccurred())
  129. Expect(iHdr.SrcConnectionID).To(BeEmpty())
  130. Expect(iHdr.DestConnectionID).To(Equal(protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}))
  131. })
  132. It("parses a Long Header with a 2 byte packet number", func() {
  133. data := []byte{
  134. 0x80 ^ uint8(protocol.PacketTypeInitial),
  135. 0x1, 0x2, 0x3, 0x4, // version number
  136. 0x0, // connection ID lengths
  137. }
  138. data = append(data, encodeVarInt(0)...) // token length
  139. data = append(data, encodeVarInt(0x42)...) // payload length
  140. data = appendPacketNumber(data, 0x123, protocol.PacketNumberLen2)
  141. b := bytes.NewReader(data)
  142. iHdr, err := ParseInvariantHeader(b, 0)
  143. Expect(err).ToNot(HaveOccurred())
  144. hdr, err := iHdr.Parse(b, protocol.PerspectiveServer, versionIETFHeader)
  145. Expect(err).ToNot(HaveOccurred())
  146. Expect(hdr.PacketNumber).To(Equal(protocol.PacketNumber(0x123)))
  147. Expect(hdr.PacketNumberLen).To(Equal(protocol.PacketNumberLen2))
  148. })
  149. It("parses a Retry packet", func() {
  150. data := []byte{
  151. 0x80 ^ uint8(protocol.PacketTypeRetry),
  152. 0x1, 0x2, 0x3, 0x4, // version number
  153. 0x0, // connection ID lengths
  154. 0x97, // Orig Destination Connection ID length
  155. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, // source connection ID
  156. 'f', 'o', 'o', 'b', 'a', 'r', // token
  157. }
  158. b := bytes.NewReader(data)
  159. iHdr, err := ParseInvariantHeader(b, 0)
  160. Expect(err).ToNot(HaveOccurred())
  161. hdr, err := iHdr.Parse(b, protocol.PerspectiveServer, versionIETFHeader)
  162. Expect(err).ToNot(HaveOccurred())
  163. Expect(hdr.Type).To(Equal(protocol.PacketTypeRetry))
  164. Expect(hdr.OrigDestConnectionID).To(Equal(protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}))
  165. Expect(hdr.Token).To(Equal([]byte("foobar")))
  166. })
  167. It("rejects packets sent with an unknown packet type", func() {
  168. srcConnID := protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}
  169. buf := &bytes.Buffer{}
  170. err := (&Header{
  171. IsLongHeader: true,
  172. Type: 42,
  173. SrcConnectionID: srcConnID,
  174. Version: 0x10203040,
  175. PacketNumber: 1,
  176. PacketNumberLen: protocol.PacketNumberLen1,
  177. }).Write(buf, protocol.PerspectiveClient, protocol.VersionTLS)
  178. Expect(err).ToNot(HaveOccurred())
  179. b := bytes.NewReader(buf.Bytes())
  180. iHdr, err := ParseInvariantHeader(b, 0)
  181. Expect(err).ToNot(HaveOccurred())
  182. _, err = iHdr.Parse(b, protocol.PerspectiveClient, versionIETFHeader)
  183. Expect(err).To(MatchError("InvalidPacketHeader: Received packet with invalid packet type: 42"))
  184. })
  185. It("errors if the token length is too large", func() {
  186. data := []byte{
  187. 0x80 ^ uint8(protocol.PacketTypeInitial),
  188. 0x1, 0x2, 0x3, 0x4, // version number
  189. 0x0, // connection ID lengths
  190. }
  191. data = append(data, encodeVarInt(4)...) // token length: 4 bytes (1 byte too long)
  192. data = append(data, encodeVarInt(0x42)...) // payload length, 1 byte
  193. data = appendPacketNumber(data, 0x123, protocol.PacketNumberLen2) // 2 bytes
  194. b := bytes.NewReader(data)
  195. iHdr, err := ParseInvariantHeader(b, 0)
  196. Expect(err).ToNot(HaveOccurred())
  197. _, err = iHdr.Parse(b, protocol.PerspectiveServer, versionIETFHeader)
  198. Expect(err).To(MatchError(io.EOF))
  199. })
  200. It("errors on EOF, when parsing the invariant header", func() {
  201. data := []byte{
  202. 0x80 ^ uint8(protocol.PacketTypeInitial),
  203. 0x1, 0x2, 0x3, 0x4, // version number
  204. 0x55, // connection ID lengths
  205. 0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0x13, 0x37, // destination connection ID
  206. 0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0x13, 0x37, // source connection ID
  207. }
  208. for i := 0; i < len(data); i++ {
  209. _, err := ParseInvariantHeader(bytes.NewReader(data[:i]), 0)
  210. Expect(err).To(Equal(io.EOF))
  211. }
  212. })
  213. It("errors on EOF, when parsing the header", func() {
  214. data := []byte{
  215. 0x80 ^ uint8(protocol.PacketTypeInitial),
  216. 0x1, 0x2, 0x3, 0x4, // version number
  217. 0x0, // connection ID lengths
  218. }
  219. iHdrLen := len(data)
  220. data = append(data, encodeVarInt(0x1337)...)
  221. data = appendPacketNumber(data, 0xdeadbeef, protocol.PacketNumberLen4)
  222. for i := iHdrLen; i < len(data); i++ {
  223. b := bytes.NewReader(data[:i])
  224. iHdr, err := ParseInvariantHeader(b, 0)
  225. Expect(err).ToNot(HaveOccurred())
  226. _, err = iHdr.Parse(b, protocol.PerspectiveServer, versionIETFHeader)
  227. Expect(err).To(Equal(io.EOF))
  228. }
  229. })
  230. It("errors on EOF, for a Retry packet", func() {
  231. data := []byte{
  232. 0x80 ^ uint8(protocol.PacketTypeRetry),
  233. 0x1, 0x2, 0x3, 0x4, // version number
  234. 0x0, // connection ID lengths
  235. }
  236. iHdrLen := len(data)
  237. data = append(data, []byte{
  238. 0x97, // Orig Destination Connection ID length
  239. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, // source connection ID
  240. }...)
  241. for i := iHdrLen; i < len(data); i++ {
  242. b := bytes.NewReader(data[:i])
  243. iHdr, err := ParseInvariantHeader(b, 0)
  244. Expect(err).ToNot(HaveOccurred())
  245. _, err = iHdr.Parse(b, protocol.PerspectiveServer, versionIETFHeader)
  246. Expect(err).To(Equal(io.EOF))
  247. }
  248. })
  249. })
  250. Context("Short Headers", func() {
  251. It("reads a Short Header with a 8 byte connection ID", func() {
  252. connID := protocol.ConnectionID{0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0x13, 0x37}
  253. data := append([]byte{0x30}, connID...)
  254. data = appendPacketNumber(data, 0x42, protocol.PacketNumberLen1)
  255. b := bytes.NewReader(data)
  256. iHdr, err := ParseInvariantHeader(b, 8)
  257. Expect(err).ToNot(HaveOccurred())
  258. Expect(iHdr.IsLongHeader).To(BeFalse())
  259. Expect(iHdr.DestConnectionID).To(Equal(connID))
  260. hdr, err := iHdr.Parse(b, protocol.PerspectiveClient, versionIETFHeader)
  261. Expect(err).ToNot(HaveOccurred())
  262. Expect(hdr.KeyPhase).To(Equal(0))
  263. Expect(hdr.DestConnectionID).To(Equal(connID))
  264. Expect(hdr.SrcConnectionID).To(BeEmpty())
  265. Expect(hdr.PacketNumber).To(Equal(protocol.PacketNumber(0x42)))
  266. Expect(hdr.IsVersionNegotiation).To(BeFalse())
  267. Expect(b.Len()).To(BeZero())
  268. })
  269. It("reads a Short Header with a 5 byte connection ID", func() {
  270. connID := protocol.ConnectionID{1, 2, 3, 4, 5}
  271. data := append([]byte{0x30}, connID...)
  272. data = appendPacketNumber(data, 0x42, protocol.PacketNumberLen1)
  273. b := bytes.NewReader(data)
  274. iHdr, err := ParseInvariantHeader(b, 5)
  275. Expect(err).ToNot(HaveOccurred())
  276. Expect(iHdr.IsLongHeader).To(BeFalse())
  277. Expect(iHdr.DestConnectionID).To(Equal(connID))
  278. hdr, err := iHdr.Parse(b, protocol.PerspectiveClient, versionIETFHeader)
  279. Expect(err).ToNot(HaveOccurred())
  280. Expect(hdr.KeyPhase).To(Equal(0))
  281. Expect(hdr.DestConnectionID).To(Equal(connID))
  282. Expect(hdr.SrcConnectionID).To(BeEmpty())
  283. Expect(b.Len()).To(BeZero())
  284. })
  285. It("reads the Key Phase Bit", func() {
  286. data := []byte{
  287. 0x30 ^ 0x40,
  288. 0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, // connection ID
  289. }
  290. data = appendPacketNumber(data, 11, protocol.PacketNumberLen1)
  291. b := bytes.NewReader(data)
  292. iHdr, err := ParseInvariantHeader(b, 6)
  293. Expect(err).ToNot(HaveOccurred())
  294. hdr, err := iHdr.Parse(b, protocol.PerspectiveServer, versionIETFHeader)
  295. Expect(err).ToNot(HaveOccurred())
  296. Expect(hdr.IsLongHeader).To(BeFalse())
  297. Expect(hdr.KeyPhase).To(Equal(1))
  298. Expect(b.Len()).To(BeZero())
  299. })
  300. It("reads a header with a 2 byte packet number", func() {
  301. data := []byte{
  302. 0x30 ^ 0x40 ^ 0x1,
  303. 0xde, 0xad, 0xbe, 0xef, // connection ID
  304. }
  305. data = appendPacketNumber(data, 0x1337, protocol.PacketNumberLen2)
  306. b := bytes.NewReader(data)
  307. iHdr, err := ParseInvariantHeader(b, 4)
  308. Expect(err).ToNot(HaveOccurred())
  309. hdr, err := iHdr.Parse(b, protocol.PerspectiveClient, versionIETFHeader)
  310. Expect(err).ToNot(HaveOccurred())
  311. Expect(hdr.IsLongHeader).To(BeFalse())
  312. Expect(hdr.PacketNumber).To(Equal(protocol.PacketNumber(0x1337)))
  313. Expect(hdr.PacketNumberLen).To(Equal(protocol.PacketNumberLen2))
  314. Expect(b.Len()).To(BeZero())
  315. })
  316. It("reads a header with a 4 byte packet number", func() {
  317. data := []byte{
  318. 0x30 ^ 0x40 ^ 0x2,
  319. 0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0x1, 0x2, 0x3, 0x4, // connection ID
  320. }
  321. data = appendPacketNumber(data, 0x99beef, protocol.PacketNumberLen4)
  322. b := bytes.NewReader(data)
  323. iHdr, err := ParseInvariantHeader(b, 10)
  324. Expect(err).ToNot(HaveOccurred())
  325. hdr, err := iHdr.Parse(b, protocol.PerspectiveServer, versionIETFHeader)
  326. Expect(err).ToNot(HaveOccurred())
  327. Expect(hdr.IsLongHeader).To(BeFalse())
  328. Expect(hdr.PacketNumber).To(Equal(protocol.PacketNumber(0x99beef)))
  329. Expect(hdr.PacketNumberLen).To(Equal(protocol.PacketNumberLen4))
  330. Expect(b.Len()).To(BeZero())
  331. })
  332. It("errors on EOF, when parsing the invariant header", func() {
  333. data := []byte{
  334. 0x30 ^ 0x2,
  335. 0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0x13, 0x37, // connection ID
  336. }
  337. for i := 0; i < len(data); i++ {
  338. _, err := ParseInvariantHeader(bytes.NewReader(data[:i]), 8)
  339. Expect(err).To(Equal(io.EOF))
  340. }
  341. })
  342. It("errors on EOF, when parsing the invariant header", func() {
  343. data := []byte{
  344. 0x30 ^ 0x2,
  345. 0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, // connection ID
  346. }
  347. iHdrLen := len(data)
  348. data = appendPacketNumber(data, 0xdeadbeef, protocol.PacketNumberLen4)
  349. for i := iHdrLen; i < len(data); i++ {
  350. b := bytes.NewReader(data[:i])
  351. iHdr, err := ParseInvariantHeader(b, 6)
  352. Expect(err).ToNot(HaveOccurred())
  353. _, err = iHdr.Parse(b, protocol.PerspectiveClient, versionIETFHeader)
  354. Expect(err).To(Equal(io.EOF))
  355. }
  356. })
  357. })
  358. })
  359. Context("gQUIC 44", func() {
  360. Context("Long Headers", func() {
  361. It("parses a Long Header", func() {
  362. destConnID := protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}
  363. srcConnID := protocol.ConnectionID{8, 7, 6, 5, 4, 3, 2, 1}
  364. data := []byte{
  365. 0x80 ^ uint8(protocol.PacketTypeInitial),
  366. 0x1, 0x2, 0x3, 0x4, // version
  367. 0x55, // connection ID lengths
  368. }
  369. data = append(data, destConnID...)
  370. data = append(data, srcConnID...)
  371. data = append(data, []byte{0xde, 0xad, 0xbe, 0xef}...)
  372. b := bytes.NewReader(data)
  373. iHdr, err := ParseInvariantHeader(b, 0)
  374. Expect(err).ToNot(HaveOccurred())
  375. Expect(iHdr.IsLongHeader).To(BeTrue())
  376. Expect(iHdr.Version).To(Equal(protocol.VersionNumber(0x1020304)))
  377. Expect(iHdr.DestConnectionID).To(Equal(destConnID))
  378. Expect(iHdr.SrcConnectionID).To(Equal(srcConnID))
  379. hdr, err := iHdr.Parse(b, protocol.PerspectiveServer, protocol.Version44)
  380. Expect(err).ToNot(HaveOccurred())
  381. Expect(hdr.IsPublicHeader).To(BeFalse())
  382. Expect(hdr.Type).To(Equal(protocol.PacketTypeInitial))
  383. Expect(hdr.PacketNumberLen).To(Equal(protocol.PacketNumberLen4))
  384. Expect(hdr.PacketNumber).To(Equal(protocol.PacketNumber(0xdeadbeef)))
  385. })
  386. It("parses a Long Header containing a Diversification Nonce", func() {
  387. srcConnID := protocol.ConnectionID{8, 7, 6, 5, 4, 3, 2, 1}
  388. divNonce := bytes.Repeat([]byte{'f'}, 32)
  389. data := []byte{
  390. 0x80 ^ uint8(protocol.PacketType0RTT),
  391. 0x1, 0x2, 0x3, 0x4, // version
  392. 0x5, // connection ID lengths
  393. }
  394. data = append(data, srcConnID...)
  395. data = append(data, []byte{0xde, 0xad, 0xbe, 0xef}...)
  396. data = append(data, divNonce...)
  397. b := bytes.NewReader(data)
  398. iHdr, err := ParseInvariantHeader(b, 0)
  399. Expect(err).ToNot(HaveOccurred())
  400. Expect(iHdr.IsLongHeader).To(BeTrue())
  401. Expect(iHdr.Version).To(Equal(protocol.VersionNumber(0x1020304)))
  402. Expect(iHdr.SrcConnectionID).To(Equal(srcConnID))
  403. hdr, err := iHdr.Parse(b, protocol.PerspectiveServer, protocol.Version44)
  404. Expect(err).ToNot(HaveOccurred())
  405. Expect(hdr.IsPublicHeader).To(BeFalse())
  406. Expect(hdr.Type).To(Equal(protocol.PacketType0RTT))
  407. Expect(hdr.PacketNumberLen).To(Equal(protocol.PacketNumberLen4))
  408. Expect(hdr.PacketNumber).To(Equal(protocol.PacketNumber(0xdeadbeef)))
  409. Expect(hdr.DiversificationNonce).To(Equal(divNonce))
  410. })
  411. It("errors on EOF, for Long Headers containing a Diversification Nonce", func() {
  412. data := []byte{
  413. 0x80 ^ uint8(protocol.PacketType0RTT),
  414. 0x1, 0x2, 0x3, 0x4, // version
  415. 0x5,
  416. 0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0x13, 0x37, // connection ID
  417. }
  418. iHdrLen := len(data)
  419. data = append(data, []byte{0xde, 0xca, 0xfb, 0xad}...) // packet number
  420. data = append(data, bytes.Repeat([]byte{'d'}, 32)...)
  421. for i := iHdrLen; i < len(data); i++ {
  422. b := bytes.NewReader(data[:i])
  423. iHdr, err := ParseInvariantHeader(b, 8)
  424. Expect(err).ToNot(HaveOccurred())
  425. Expect(iHdr.IsLongHeader).To(BeTrue())
  426. _, err = iHdr.Parse(b, protocol.PerspectiveServer, protocol.Version44)
  427. Expect(err).To(Equal(io.EOF))
  428. }
  429. })
  430. })
  431. Context("Short Headers", func() {
  432. It("parses a Short Header with a 1 byte packet number", func() {
  433. destConnID := protocol.ConnectionID{8, 7, 6, 5, 4, 3, 2, 1}
  434. data := []byte{0x30}
  435. data = append(data, destConnID...)
  436. data = append(data, 0x42) // packet number
  437. b := bytes.NewReader(data)
  438. iHdr, err := ParseInvariantHeader(b, 8)
  439. Expect(err).ToNot(HaveOccurred())
  440. Expect(iHdr.IsLongHeader).To(BeFalse())
  441. Expect(iHdr.DestConnectionID).To(Equal(destConnID))
  442. hdr, err := iHdr.Parse(b, protocol.PerspectiveServer, protocol.Version44)
  443. Expect(err).ToNot(HaveOccurred())
  444. Expect(hdr.IsPublicHeader).To(BeFalse())
  445. Expect(hdr.PacketNumberLen).To(Equal(protocol.PacketNumberLen1))
  446. Expect(hdr.PacketNumber).To(Equal(protocol.PacketNumber(0x42)))
  447. })
  448. It("parses a Short Header with a 2 byte packet number", func() {
  449. data := []byte{0x30 ^ 0x1, 0xca, 0xfe}
  450. b := bytes.NewReader(data)
  451. iHdr, err := ParseInvariantHeader(b, 0)
  452. Expect(err).ToNot(HaveOccurred())
  453. Expect(iHdr.IsLongHeader).To(BeFalse())
  454. Expect(iHdr.DestConnectionID.Len()).To(BeZero())
  455. hdr, err := iHdr.Parse(b, protocol.PerspectiveServer, protocol.Version44)
  456. Expect(err).ToNot(HaveOccurred())
  457. Expect(hdr.IsPublicHeader).To(BeFalse())
  458. Expect(hdr.PacketNumberLen).To(Equal(protocol.PacketNumberLen2))
  459. Expect(hdr.PacketNumber).To(Equal(protocol.PacketNumber(0xcafe)))
  460. })
  461. It("parses a Short Header with a 4 byte packet number", func() {
  462. data := []byte{0x30 ^ 0x2, 0xde, 0xad, 0xbe, 0xef}
  463. b := bytes.NewReader(data)
  464. iHdr, err := ParseInvariantHeader(b, 0)
  465. Expect(err).ToNot(HaveOccurred())
  466. Expect(iHdr.IsLongHeader).To(BeFalse())
  467. Expect(iHdr.DestConnectionID.Len()).To(BeZero())
  468. hdr, err := iHdr.Parse(b, protocol.PerspectiveServer, protocol.Version44)
  469. Expect(err).ToNot(HaveOccurred())
  470. Expect(hdr.IsPublicHeader).To(BeFalse())
  471. Expect(hdr.PacketNumberLen).To(Equal(protocol.PacketNumberLen4))
  472. Expect(hdr.PacketNumber).To(Equal(protocol.PacketNumber(0xdeadbeef)))
  473. })
  474. It("errors on an invalid packet number length flag", func() {
  475. data := []byte{0x30 ^ 0x3, 0xde, 0xad, 0xbe, 0xef}
  476. b := bytes.NewReader(data)
  477. iHdr, err := ParseInvariantHeader(b, 0)
  478. Expect(err).ToNot(HaveOccurred())
  479. Expect(iHdr.IsLongHeader).To(BeFalse())
  480. Expect(iHdr.DestConnectionID.Len()).To(BeZero())
  481. _, err = iHdr.Parse(b, protocol.PerspectiveServer, protocol.Version44)
  482. Expect(err).To(MatchError(errInvalidPacketNumberLen))
  483. })
  484. It("errors on EOF", func() {
  485. data := []byte{0x30 ^ 0x2, 0xde, 0xad, 0xbe, 0xef}
  486. iHdrLen := 1
  487. for i := iHdrLen; i < len(data); i++ {
  488. b := bytes.NewReader(data[:i])
  489. iHdr, err := ParseInvariantHeader(b, 0)
  490. Expect(err).ToNot(HaveOccurred())
  491. Expect(iHdr.IsLongHeader).To(BeFalse())
  492. _, err = iHdr.Parse(b, protocol.PerspectiveServer, protocol.Version44)
  493. Expect(err).To(Equal(io.EOF))
  494. }
  495. })
  496. })
  497. })
  498. Context("Public Header", func() {
  499. It("accepts a sample client header", func() {
  500. data := []byte{
  501. 0x9,
  502. 0x4c, 0xfa, 0x9f, 0x9b, 0x66, 0x86, 0x19, 0xf6,
  503. }
  504. data = append(data, []byte{0xde, 0xad, 0xbe, 0xef}...)
  505. data = append(data, 0x1) // packet number
  506. b := bytes.NewReader(data)
  507. iHdr, err := ParseInvariantHeader(b, 0)
  508. Expect(err).ToNot(HaveOccurred())
  509. Expect(iHdr.IsLongHeader).To(BeFalse())
  510. hdr, err := iHdr.Parse(b, protocol.PerspectiveClient, versionPublicHeader)
  511. Expect(err).ToNot(HaveOccurred())
  512. Expect(hdr.VersionFlag).To(BeTrue())
  513. Expect(hdr.IsVersionNegotiation).To(BeFalse())
  514. Expect(hdr.ResetFlag).To(BeFalse())
  515. connID := protocol.ConnectionID{0x4c, 0xfa, 0x9f, 0x9b, 0x66, 0x86, 0x19, 0xf6}
  516. Expect(hdr.DestConnectionID).To(Equal(connID))
  517. Expect(hdr.SrcConnectionID).To(BeEmpty())
  518. Expect(hdr.Version).To(Equal(protocol.VersionNumber(0xdeadbeef)))
  519. Expect(hdr.SupportedVersions).To(BeEmpty())
  520. Expect(hdr.PacketNumber).To(Equal(protocol.PacketNumber(1)))
  521. Expect(b.Len()).To(BeZero())
  522. })
  523. It("accepts an omitted connection ID", func() {
  524. b := bytes.NewReader([]byte{0x0, 0x1})
  525. iHdr, err := ParseInvariantHeader(b, 8)
  526. Expect(err).ToNot(HaveOccurred())
  527. Expect(iHdr.IsLongHeader).To(BeFalse())
  528. Expect(iHdr.DestConnectionID).To(BeEmpty())
  529. Expect(iHdr.SrcConnectionID).To(BeEmpty())
  530. hdr, err := iHdr.Parse(b, protocol.PerspectiveClient, versionPublicHeader)
  531. Expect(err).ToNot(HaveOccurred())
  532. Expect(hdr.PacketNumber).To(Equal(protocol.PacketNumber(1)))
  533. Expect(b.Len()).To(BeZero())
  534. })
  535. It("parses a PUBLIC_RESET packet", func() {
  536. b := bytes.NewReader([]byte{0xa, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8})
  537. iHdr, err := ParseInvariantHeader(b, 4)
  538. Expect(err).ToNot(HaveOccurred())
  539. Expect(iHdr.IsLongHeader).To(BeFalse())
  540. hdr, err := iHdr.Parse(b, protocol.PerspectiveServer, versionPublicHeader)
  541. Expect(err).ToNot(HaveOccurred())
  542. Expect(hdr.ResetFlag).To(BeTrue())
  543. Expect(hdr.VersionFlag).To(BeFalse())
  544. Expect(hdr.IsVersionNegotiation).To(BeFalse())
  545. connID := protocol.ConnectionID{0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8}
  546. Expect(hdr.SrcConnectionID).To(BeEmpty())
  547. Expect(hdr.DestConnectionID).To(Equal(connID))
  548. })
  549. It("reads a diversification nonce sent by the server", func() {
  550. divNonce := []byte{0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f}
  551. Expect(divNonce).To(HaveLen(32))
  552. b := bytes.NewReader(append(append([]byte{0x0c, 0xf6, 0x19, 0x86, 0x66, 0x9b, 0x9f, 0xfa, 0x4c}, divNonce...), 0x37))
  553. iHdr, err := ParseInvariantHeader(b, 7)
  554. Expect(err).ToNot(HaveOccurred())
  555. Expect(iHdr.IsLongHeader).To(BeFalse())
  556. hdr, err := iHdr.Parse(b, protocol.PerspectiveServer, versionPublicHeader)
  557. Expect(err).ToNot(HaveOccurred())
  558. Expect(hdr.DestConnectionID).ToNot(BeEmpty())
  559. Expect(hdr.SrcConnectionID).To(BeEmpty())
  560. Expect(hdr.DiversificationNonce).To(Equal(divNonce))
  561. Expect(b.Len()).To(BeZero())
  562. })
  563. It("errors on EOF", func() {
  564. data := []byte{
  565. 0x10 ^ 0x8 ^ 0x4,
  566. 0xf6, 0x19, 0x86, 0x66, 0x9b, 0x9f, 0xfa, 0x4c,
  567. }
  568. iHdrLen := len(data)
  569. data = append(data, bytes.Repeat([]byte{0}, 32)...) // add a diversification nonce
  570. data = append(data, []byte{0x13, 37}...) // packet number
  571. for i := iHdrLen; i < len(data); i++ {
  572. b := bytes.NewReader(data[:i])
  573. iHdr, err := ParseInvariantHeader(b, 5)
  574. Expect(err).ToNot(HaveOccurred())
  575. _, err = iHdr.Parse(b, protocol.PerspectiveServer, versionPublicHeader)
  576. Expect(err).To(Equal(io.EOF))
  577. }
  578. })
  579. Context("version negotiation packets", func() {
  580. appendVersion := func(data []byte, v protocol.VersionNumber) []byte {
  581. data = append(data, []byte{0, 0, 0, 0}...)
  582. binary.BigEndian.PutUint32(data[len(data)-4:], uint32(v))
  583. return data
  584. }
  585. It("parses", func() {
  586. connID := protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}
  587. versions := []protocol.VersionNumber{0x13, 0x37}
  588. b := bytes.NewReader(ComposeGQUICVersionNegotiation(connID, versions))
  589. iHdr, err := ParseInvariantHeader(b, 6)
  590. Expect(err).ToNot(HaveOccurred())
  591. hdr, err := iHdr.Parse(b, protocol.PerspectiveServer, versionPublicHeader)
  592. Expect(err).ToNot(HaveOccurred())
  593. Expect(hdr.DestConnectionID).To(Equal(connID))
  594. Expect(hdr.SrcConnectionID).To(BeEmpty())
  595. Expect(hdr.VersionFlag).To(BeTrue())
  596. Expect(hdr.Version).To(BeZero()) // unitialized
  597. Expect(hdr.IsVersionNegotiation).To(BeTrue())
  598. // in addition to the versions, the supported versions might contain a reserved version number
  599. for _, version := range versions {
  600. Expect(hdr.SupportedVersions).To(ContainElement(version))
  601. }
  602. Expect(b.Len()).To(BeZero())
  603. })
  604. It("errors if it doesn't contain any versions", func() {
  605. b := bytes.NewReader([]byte{0x9, 0xf6, 0x19, 0x86, 0x66, 0x9b, 0x9f, 0xfa, 0x4c})
  606. iHdr, err := ParseInvariantHeader(b, 4)
  607. Expect(err).ToNot(HaveOccurred())
  608. _, err = iHdr.Parse(b, protocol.PerspectiveServer, versionPublicHeader)
  609. Expect(err).To(MatchError("InvalidVersionNegotiationPacket: empty version list"))
  610. })
  611. It("reads version negotiation packets containing unsupported versions", func() {
  612. data := []byte{0x9, 0xf6, 0x19, 0x86, 0x66, 0x9b, 0x9f, 0xfa, 0x4c}
  613. data = appendVersion(data, 1) // unsupported version
  614. data = appendVersion(data, protocol.SupportedVersions[0])
  615. data = appendVersion(data, 99) // unsupported version
  616. b := bytes.NewReader(data)
  617. iHdr, err := ParseInvariantHeader(b, 0)
  618. Expect(err).ToNot(HaveOccurred())
  619. hdr, err := iHdr.Parse(b, protocol.PerspectiveServer, versionPublicHeader)
  620. Expect(err).ToNot(HaveOccurred())
  621. Expect(hdr.VersionFlag).To(BeTrue())
  622. Expect(hdr.IsVersionNegotiation).To(BeTrue())
  623. Expect(hdr.SupportedVersions).To(Equal([]protocol.VersionNumber{1, protocol.SupportedVersions[0], 99}))
  624. Expect(b.Len()).To(BeZero())
  625. })
  626. It("errors on invalid version tags", func() {
  627. data := ComposeGQUICVersionNegotiation(protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}, protocol.SupportedVersions)
  628. data = append(data, []byte{0x13, 0x37}...)
  629. b := bytes.NewReader(data)
  630. iHdr, err := ParseInvariantHeader(b, 0)
  631. Expect(err).ToNot(HaveOccurred())
  632. _, err = iHdr.Parse(b, protocol.PerspectiveServer, versionPublicHeader)
  633. Expect(err).To(MatchError(qerr.InvalidVersionNegotiationPacket))
  634. })
  635. })
  636. Context("Packet Number lengths", func() {
  637. It("accepts 1-byte packet numbers", func() {
  638. b := bytes.NewReader([]byte{0x08, 0x4c, 0xfa, 0x9f, 0x9b, 0x66, 0x86, 0x19, 0xf6, 0xde})
  639. iHdr, err := ParseInvariantHeader(b, 0)
  640. Expect(err).ToNot(HaveOccurred())
  641. hdr, err := iHdr.Parse(b, protocol.PerspectiveServer, versionPublicHeader)
  642. Expect(err).ToNot(HaveOccurred())
  643. Expect(hdr.PacketNumber).To(Equal(protocol.PacketNumber(0xde)))
  644. Expect(hdr.PacketNumberLen).To(Equal(protocol.PacketNumberLen1))
  645. Expect(b.Len()).To(BeZero())
  646. })
  647. It("accepts 2-byte packet numbers", func() {
  648. b := bytes.NewReader([]byte{0x18, 0x4c, 0xfa, 0x9f, 0x9b, 0x66, 0x86, 0x19, 0xf6, 0xde, 0xca})
  649. iHdr, err := ParseInvariantHeader(b, 0)
  650. Expect(err).ToNot(HaveOccurred())
  651. hdr, err := iHdr.Parse(b, protocol.PerspectiveServer, versionPublicHeader)
  652. Expect(err).ToNot(HaveOccurred())
  653. Expect(hdr.PacketNumber).To(Equal(protocol.PacketNumber(0xdeca)))
  654. Expect(hdr.PacketNumberLen).To(Equal(protocol.PacketNumberLen2))
  655. Expect(b.Len()).To(BeZero())
  656. })
  657. It("accepts 4-byte packet numbers", func() {
  658. b := bytes.NewReader([]byte{0x28, 0x4c, 0xfa, 0x9f, 0x9b, 0x66, 0x86, 0x19, 0xf6, 0xad, 0xfb, 0xca, 0xde})
  659. iHdr, err := ParseInvariantHeader(b, 0)
  660. Expect(err).ToNot(HaveOccurred())
  661. hdr, err := iHdr.Parse(b, protocol.PerspectiveServer, versionPublicHeader)
  662. Expect(err).ToNot(HaveOccurred())
  663. Expect(hdr.PacketNumber).To(Equal(protocol.PacketNumber(0xadfbcade)))
  664. Expect(hdr.PacketNumberLen).To(Equal(protocol.PacketNumberLen4))
  665. Expect(b.Len()).To(BeZero())
  666. })
  667. })
  668. })
  669. })