2
0

quic_wire_test.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645
  1. /*
  2. * Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include "internal/packet.h"
  10. #include "internal/quic_wire.h"
  11. #include "internal/quic_wire_pkt.h"
  12. #include "testutil.h"
  13. struct encode_test_case {
  14. int (*serializer)(WPACKET *pkt);
  15. const unsigned char *expect_buf;
  16. size_t expect_buf_len;
  17. /*
  18. * fail: -1 if not truncated (function should test for success), else number
  19. * of bytes to which the input has been truncated (function should test that
  20. * decoding fails)
  21. */
  22. int (*deserializer)(PACKET *pkt, ossl_ssize_t fail);
  23. };
  24. /* 1. PADDING */
  25. static int encode_case_1_enc(WPACKET *pkt)
  26. {
  27. if (!TEST_int_eq(ossl_quic_wire_encode_padding(pkt, 3), 1))
  28. return 0;
  29. return 1;
  30. }
  31. static int encode_case_1_dec(PACKET *pkt, ossl_ssize_t fail)
  32. {
  33. if (fail >= 0)
  34. /* No failure modes for padding */
  35. return 1;
  36. if (!TEST_int_eq(ossl_quic_wire_decode_padding(pkt), 3))
  37. return 0;
  38. return 1;
  39. }
  40. static const unsigned char encode_case_1_expect[] = {
  41. 0, 0, 0
  42. };
  43. /* 2. PING */
  44. static int encode_case_2_enc(WPACKET *pkt)
  45. {
  46. if (!TEST_int_eq(ossl_quic_wire_encode_frame_ping(pkt), 1))
  47. return 0;
  48. return 1;
  49. }
  50. static int encode_case_2_dec(PACKET *pkt, ossl_ssize_t fail)
  51. {
  52. if (!TEST_int_eq(ossl_quic_wire_decode_frame_ping(pkt), fail < 0))
  53. return 0;
  54. return 1;
  55. }
  56. static const unsigned char encode_case_2_expect[] = {
  57. 0x01
  58. };
  59. /* 3. ACK */
  60. static const OSSL_QUIC_ACK_RANGE encode_case_3_ranges[] = {
  61. { 20, 30 },
  62. { 0, 10 }
  63. };
  64. static const OSSL_QUIC_FRAME_ACK encode_case_3_f = {
  65. (OSSL_QUIC_ACK_RANGE *)encode_case_3_ranges,
  66. OSSL_NELEM(encode_case_3_ranges),
  67. { OSSL_TIME_MS },
  68. 60, 70, 80, 1
  69. };
  70. static int encode_case_3_enc(WPACKET *pkt)
  71. {
  72. if (!TEST_int_eq(ossl_quic_wire_encode_frame_ack(pkt, 3, &encode_case_3_f), 1))
  73. return 0;
  74. return 1;
  75. }
  76. static int encode_case_3_dec(PACKET *pkt, ossl_ssize_t fail)
  77. {
  78. OSSL_QUIC_ACK_RANGE ranges[4] = {0};
  79. OSSL_QUIC_FRAME_ACK f = {0};
  80. uint64_t total_ranges = 0, peek_total_ranges = 0;
  81. int ret;
  82. f.ack_ranges = ranges;
  83. f.num_ack_ranges = OSSL_NELEM(ranges);
  84. ret = ossl_quic_wire_peek_frame_ack_num_ranges(pkt, &peek_total_ranges);
  85. if (fail < 0 && !TEST_int_eq(ret, 1))
  86. return 0;
  87. if (!TEST_int_eq(ossl_quic_wire_decode_frame_ack(pkt, 3, &f, &total_ranges), fail < 0))
  88. return 0;
  89. if (ret == 1 && !TEST_uint64_t_eq(peek_total_ranges, 2))
  90. return 0;
  91. if (fail >= 0)
  92. return 1;
  93. if (!TEST_uint64_t_eq(total_ranges, peek_total_ranges))
  94. return 0;
  95. if (!TEST_uint64_t_le(f.num_ack_ranges * sizeof(OSSL_QUIC_ACK_RANGE),
  96. SIZE_MAX)
  97. || !TEST_uint64_t_le(encode_case_3_f.num_ack_ranges
  98. * sizeof(OSSL_QUIC_ACK_RANGE),
  99. SIZE_MAX))
  100. return 0;
  101. if (!TEST_mem_eq(f.ack_ranges,
  102. (size_t)f.num_ack_ranges * sizeof(OSSL_QUIC_ACK_RANGE),
  103. encode_case_3_f.ack_ranges,
  104. (size_t)encode_case_3_f.num_ack_ranges * sizeof(OSSL_QUIC_ACK_RANGE)))
  105. return 0;
  106. if (!TEST_uint64_t_eq(ossl_time2ticks(f.delay_time),
  107. ossl_time2ticks(encode_case_3_f.delay_time)))
  108. return 0;
  109. if (!TEST_true(f.ecn_present))
  110. return 0;
  111. if (!TEST_uint64_t_eq(f.ect0, encode_case_3_f.ect0))
  112. return 0;
  113. if (!TEST_uint64_t_eq(f.ect1, encode_case_3_f.ect1))
  114. return 0;
  115. if (!TEST_uint64_t_eq(f.ecnce, encode_case_3_f.ecnce))
  116. return 0;
  117. return 1;
  118. }
  119. static const unsigned char encode_case_3_expect[] = {
  120. 0x03, /* Type */
  121. 0x1E, /* Largest Acknowledged */
  122. 0x40, 0x7d, /* ACK Delay */
  123. 1, /* ACK Range Count */
  124. 10, /* First ACK Range */
  125. 8, /* Gap */
  126. 10, /* Length */
  127. 0x3c, /* ECT0 Count */
  128. 0x40, 0x46, /* ECT1 Count */
  129. 0x40, 0x50, /* ECNCE Count */
  130. };
  131. /* 4. RESET_STREAM */
  132. static const OSSL_QUIC_FRAME_RESET_STREAM encode_case_4_f = {
  133. 0x1234, 0x9781, 0x11717
  134. };
  135. static int encode_case_4_enc(WPACKET *pkt)
  136. {
  137. if (!TEST_int_eq(ossl_quic_wire_encode_frame_reset_stream(pkt,
  138. &encode_case_4_f), 1))
  139. return 0;
  140. return 1;
  141. }
  142. static int encode_case_4_dec(PACKET *pkt, ossl_ssize_t fail)
  143. {
  144. OSSL_QUIC_FRAME_RESET_STREAM f = {0};
  145. if (!TEST_int_eq(ossl_quic_wire_decode_frame_reset_stream(pkt, &f), fail < 0))
  146. return 0;
  147. if (fail >= 0)
  148. return 1;
  149. if (!TEST_mem_eq(&f, sizeof(f), &encode_case_4_f, sizeof(encode_case_4_f)))
  150. return 0;
  151. return 1;
  152. }
  153. static const unsigned char encode_case_4_expect[] = {
  154. 0x04, /* Type */
  155. 0x52, 0x34, /* Stream ID */
  156. 0x80, 0x00, 0x97, 0x81, /* App Error Code */
  157. 0x80, 0x01, 0x17, 0x17, /* Final Size */
  158. };
  159. /* 5. STOP_SENDING */
  160. static const OSSL_QUIC_FRAME_STOP_SENDING encode_case_5_f = {
  161. 0x1234, 0x9781
  162. };
  163. static int encode_case_5_enc(WPACKET *pkt)
  164. {
  165. if (!TEST_int_eq(ossl_quic_wire_encode_frame_stop_sending(pkt,
  166. &encode_case_5_f), 1))
  167. return 0;
  168. return 1;
  169. }
  170. static int encode_case_5_dec(PACKET *pkt, ossl_ssize_t fail)
  171. {
  172. OSSL_QUIC_FRAME_STOP_SENDING f = {0};
  173. if (!TEST_int_eq(ossl_quic_wire_decode_frame_stop_sending(pkt, &f), fail < 0))
  174. return 0;
  175. if (fail >= 0)
  176. return 1;
  177. if (!TEST_mem_eq(&f, sizeof(f), &encode_case_5_f, sizeof(encode_case_5_f)))
  178. return 0;
  179. return 1;
  180. }
  181. static const unsigned char encode_case_5_expect[] = {
  182. 0x05, /* Type */
  183. 0x52, 0x34, /* Stream ID */
  184. 0x80, 0x00, 0x97, 0x81 /* App Error Code */
  185. };
  186. /* 6. CRYPTO */
  187. static const unsigned char encode_case_6_data[] = {
  188. 93, 18, 17, 102, 33
  189. };
  190. static const OSSL_QUIC_FRAME_CRYPTO encode_case_6_f = {
  191. 0x1234, sizeof(encode_case_6_data), encode_case_6_data
  192. };
  193. static int encode_case_6_enc(WPACKET *pkt)
  194. {
  195. if (!TEST_ptr(ossl_quic_wire_encode_frame_crypto(pkt,
  196. &encode_case_6_f)))
  197. return 0;
  198. return 1;
  199. }
  200. static int encode_case_6_dec(PACKET *pkt, ossl_ssize_t fail)
  201. {
  202. OSSL_QUIC_FRAME_CRYPTO f = {0};
  203. if (!TEST_int_eq(ossl_quic_wire_decode_frame_crypto(pkt, 0, &f), fail < 0))
  204. return 0;
  205. if (fail >= 0)
  206. return 1;
  207. if (!TEST_uint64_t_eq(f.offset, 0x1234))
  208. return 0;
  209. if (!TEST_uint64_t_le(f.len, SIZE_MAX))
  210. return 0;
  211. if (!TEST_mem_eq(f.data, (size_t)f.len,
  212. encode_case_6_data, sizeof(encode_case_6_data)))
  213. return 0;
  214. return 1;
  215. }
  216. static const unsigned char encode_case_6_expect[] = {
  217. 0x06, /* Type */
  218. 0x52, 0x34, /* Offset */
  219. 0x05, /* Length */
  220. 93, 18, 17, 102, 33 /* Data */
  221. };
  222. /* 7. NEW_TOKEN */
  223. static const unsigned char encode_case_7_token[] = {
  224. 0xde, 0x06, 0xcb, 0x76, 0x5d, 0xb1, 0xa7, 0x71,
  225. 0x78, 0x09, 0xbb, 0xe8, 0x50, 0x19, 0x12, 0x9a
  226. };
  227. static int encode_case_7_enc(WPACKET *pkt)
  228. {
  229. if (!TEST_int_eq(ossl_quic_wire_encode_frame_new_token(pkt,
  230. encode_case_7_token,
  231. sizeof(encode_case_7_token)), 1))
  232. return 0;
  233. return 1;
  234. }
  235. static int encode_case_7_dec(PACKET *pkt, ossl_ssize_t fail)
  236. {
  237. const unsigned char *token = NULL;
  238. size_t token_len = 0;
  239. if (!TEST_int_eq(ossl_quic_wire_decode_frame_new_token(pkt,
  240. &token,
  241. &token_len), fail < 0))
  242. return 0;
  243. if (fail >= 0)
  244. return 1;
  245. if (!TEST_mem_eq(token, token_len,
  246. encode_case_7_token, sizeof(encode_case_7_token)))
  247. return 0;
  248. return 1;
  249. }
  250. static const unsigned char encode_case_7_expect[] = {
  251. 0x07, /* Type */
  252. 0x10, /* Length */
  253. 0xde, 0x06, 0xcb, 0x76, 0x5d, 0xb1, 0xa7, 0x71, /* Token */
  254. 0x78, 0x09, 0xbb, 0xe8, 0x50, 0x19, 0x12, 0x9a
  255. };
  256. /* 8. STREAM (no length, no offset, no fin) */
  257. static const unsigned char encode_case_8_data[] = {
  258. 0xde, 0x06, 0xcb, 0x76, 0x5d
  259. };
  260. static const OSSL_QUIC_FRAME_STREAM encode_case_8_f = {
  261. 0x1234, 0, 5, encode_case_8_data, 0, 0
  262. };
  263. static int encode_case_8_enc(WPACKET *pkt)
  264. {
  265. if (!TEST_ptr(ossl_quic_wire_encode_frame_stream(pkt,
  266. &encode_case_8_f)))
  267. return 0;
  268. return 1;
  269. }
  270. static int encode_case_8_dec(PACKET *pkt, ossl_ssize_t fail)
  271. {
  272. OSSL_QUIC_FRAME_STREAM f = {0};
  273. if (fail >= 3)
  274. /*
  275. * This case uses implicit length signalling so truncation will not
  276. * cause it to fail unless the header (which is 3 bytes) is truncated.
  277. */
  278. return 1;
  279. if (!TEST_int_eq(ossl_quic_wire_decode_frame_stream(pkt, 0, &f), fail < 0))
  280. return 0;
  281. if (fail >= 0)
  282. return 1;
  283. if (!TEST_uint64_t_le(f.len, SIZE_MAX))
  284. return 0;
  285. if (!TEST_mem_eq(f.data, (size_t)f.len,
  286. encode_case_8_data, sizeof(encode_case_8_data)))
  287. return 0;
  288. if (!TEST_uint64_t_eq(f.stream_id, 0x1234))
  289. return 0;
  290. if (!TEST_uint64_t_eq(f.offset, 0))
  291. return 0;
  292. if (!TEST_int_eq(f.has_explicit_len, 0))
  293. return 0;
  294. if (!TEST_int_eq(f.is_fin, 0))
  295. return 0;
  296. return 1;
  297. }
  298. static const unsigned char encode_case_8_expect[] = {
  299. 0x08, /* Type (OFF=0, LEN=0, FIN=0) */
  300. 0x52, 0x34, /* Stream ID */
  301. 0xde, 0x06, 0xcb, 0x76, 0x5d /* Data */
  302. };
  303. /* 9. STREAM (length, offset, fin) */
  304. static const unsigned char encode_case_9_data[] = {
  305. 0xde, 0x06, 0xcb, 0x76, 0x5d
  306. };
  307. static const OSSL_QUIC_FRAME_STREAM encode_case_9_f = {
  308. 0x1234, 0x39, 5, encode_case_9_data, 1, 1
  309. };
  310. static int encode_case_9_enc(WPACKET *pkt)
  311. {
  312. if (!TEST_ptr(ossl_quic_wire_encode_frame_stream(pkt,
  313. &encode_case_9_f)))
  314. return 0;
  315. return 1;
  316. }
  317. static int encode_case_9_dec(PACKET *pkt, ossl_ssize_t fail)
  318. {
  319. OSSL_QUIC_FRAME_STREAM f = {0};
  320. if (!TEST_int_eq(ossl_quic_wire_decode_frame_stream(pkt, 0, &f), fail < 0))
  321. return 0;
  322. if (fail >= 0)
  323. return 1;
  324. if (!TEST_uint64_t_le(f.len, SIZE_MAX))
  325. return 0;
  326. if (!TEST_mem_eq(f.data, (size_t)f.len,
  327. encode_case_9_data, sizeof(encode_case_9_data)))
  328. return 0;
  329. if (!TEST_uint64_t_eq(f.stream_id, 0x1234))
  330. return 0;
  331. if (!TEST_uint64_t_eq(f.offset, 0x39))
  332. return 0;
  333. if (!TEST_int_eq(f.has_explicit_len, 1))
  334. return 0;
  335. if (!TEST_int_eq(f.is_fin, 1))
  336. return 0;
  337. return 1;
  338. }
  339. static const unsigned char encode_case_9_expect[] = {
  340. 0x0f, /* Type (OFF=1, LEN=1, FIN=1) */
  341. 0x52, 0x34, /* Stream ID */
  342. 0x39, /* Offset */
  343. 0x05, /* Length */
  344. 0xde, 0x06, 0xcb, 0x76, 0x5d /* Data */
  345. };
  346. /* 10. MAX_DATA */
  347. static int encode_case_10_enc(WPACKET *pkt)
  348. {
  349. if (!TEST_int_eq(ossl_quic_wire_encode_frame_max_data(pkt, 0x1234), 1))
  350. return 0;
  351. return 1;
  352. }
  353. static int encode_case_10_dec(PACKET *pkt, ossl_ssize_t fail)
  354. {
  355. uint64_t max_data = 0;
  356. if (!TEST_int_eq(ossl_quic_wire_decode_frame_max_data(pkt, &max_data), fail < 0))
  357. return 0;
  358. if (fail >= 0)
  359. return 1;
  360. if (!TEST_uint64_t_eq(max_data, 0x1234))
  361. return 0;
  362. return 1;
  363. }
  364. static const unsigned char encode_case_10_expect[] = {
  365. 0x10, /* Type */
  366. 0x52, 0x34, /* Max Data */
  367. };
  368. /* 11. MAX_STREAM_DATA */
  369. static int encode_case_11_enc(WPACKET *pkt)
  370. {
  371. if (!TEST_int_eq(ossl_quic_wire_encode_frame_max_stream_data(pkt,
  372. 0x1234,
  373. 0x9781), 1))
  374. return 0;
  375. return 1;
  376. }
  377. static int encode_case_11_dec(PACKET *pkt, ossl_ssize_t fail)
  378. {
  379. uint64_t stream_id = 0, max_data = 0;
  380. if (!TEST_int_eq(ossl_quic_wire_decode_frame_max_stream_data(pkt,
  381. &stream_id,
  382. &max_data), fail < 0))
  383. return 0;
  384. if (fail >= 0)
  385. return 1;
  386. if (!TEST_uint64_t_eq(stream_id, 0x1234))
  387. return 0;
  388. if (!TEST_uint64_t_eq(max_data, 0x9781))
  389. return 0;
  390. return 1;
  391. }
  392. static const unsigned char encode_case_11_expect[] = {
  393. 0x11, /* Type */
  394. 0x52, 0x34, /* Stream ID */
  395. 0x80, 0x00, 0x97, 0x81, /* Max Data */
  396. };
  397. /* 12. MAX_STREAMS */
  398. static int encode_case_12_enc(WPACKET *pkt)
  399. {
  400. if (!TEST_int_eq(ossl_quic_wire_encode_frame_max_streams(pkt, 0, 0x1234), 1))
  401. return 0;
  402. if (!TEST_int_eq(ossl_quic_wire_encode_frame_max_streams(pkt, 1, 0x9781), 1))
  403. return 0;
  404. return 1;
  405. }
  406. static int encode_case_12_dec(PACKET *pkt, ossl_ssize_t fail)
  407. {
  408. uint64_t max_streams_1 = 0, max_streams_2 = 0,
  409. frame_type_1 = 0, frame_type_2 = 0;
  410. int is_minimal = 1, success_if;
  411. success_if = (fail < 0 || fail >= 1);
  412. if (!TEST_int_eq(ossl_quic_wire_peek_frame_header(pkt, &frame_type_1,
  413. &is_minimal),
  414. success_if))
  415. return 0;
  416. if (!TEST_true(!success_if || is_minimal))
  417. return 0;
  418. success_if = (fail < 0 || fail >= 3);
  419. if (!TEST_int_eq(ossl_quic_wire_decode_frame_max_streams(pkt,
  420. &max_streams_1),
  421. success_if))
  422. return 0;
  423. success_if = (fail < 0 || fail >= 4);
  424. if (!TEST_int_eq(ossl_quic_wire_peek_frame_header(pkt, &frame_type_2,
  425. &is_minimal),
  426. success_if))
  427. return 0;
  428. if (!TEST_true(!success_if || is_minimal))
  429. return 0;
  430. success_if = (fail < 0);
  431. if (!TEST_int_eq(ossl_quic_wire_decode_frame_max_streams(pkt,
  432. &max_streams_2),
  433. success_if))
  434. return 0;
  435. if ((fail < 0 || fail >= 3)
  436. && !TEST_uint64_t_eq(frame_type_1, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI))
  437. return 0;
  438. if ((fail < 0 || fail >= 3)
  439. && !TEST_uint64_t_eq(max_streams_1, 0x1234))
  440. return 0;
  441. if ((fail < 0 || fail >= 8)
  442. && !TEST_uint64_t_eq(frame_type_2, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI))
  443. return 0;
  444. if ((fail < 0 || fail >= 8)
  445. && !TEST_uint64_t_eq(max_streams_2, 0x9781))
  446. return 0;
  447. return 1;
  448. }
  449. static const unsigned char encode_case_12_expect[] = {
  450. 0x12, /* Type (MAX_STREAMS Bidirectional) */
  451. 0x52, 0x34, /* Max Streams */
  452. 0x13, /* Type (MAX_STREAMS Unidirectional) */
  453. 0x80, 0x00, 0x97, 0x81, /* Max Streams */
  454. };
  455. /* 13. DATA_BLOCKED */
  456. static int encode_case_13_enc(WPACKET *pkt)
  457. {
  458. if (!TEST_int_eq(ossl_quic_wire_encode_frame_data_blocked(pkt, 0x1234), 1))
  459. return 0;
  460. return 1;
  461. }
  462. static int encode_case_13_dec(PACKET *pkt, ossl_ssize_t fail)
  463. {
  464. uint64_t max_data = 0;
  465. if (!TEST_int_eq(ossl_quic_wire_decode_frame_data_blocked(pkt,
  466. &max_data), fail < 0))
  467. return 0;
  468. if (fail >= 0)
  469. return 1;
  470. if (!TEST_uint64_t_eq(max_data, 0x1234))
  471. return 0;
  472. return 1;
  473. }
  474. static const unsigned char encode_case_13_expect[] = {
  475. 0x14, /* Type */
  476. 0x52, 0x34, /* Max Data */
  477. };
  478. /* 14. STREAM_DATA_BLOCKED */
  479. static int encode_case_14_enc(WPACKET *pkt)
  480. {
  481. if (!TEST_int_eq(ossl_quic_wire_encode_frame_stream_data_blocked(pkt,
  482. 0x1234,
  483. 0x9781), 1))
  484. return 0;
  485. return 1;
  486. }
  487. static int encode_case_14_dec(PACKET *pkt, ossl_ssize_t fail)
  488. {
  489. uint64_t stream_id = 0, max_data = 0;
  490. if (!TEST_int_eq(ossl_quic_wire_decode_frame_stream_data_blocked(pkt,
  491. &stream_id,
  492. &max_data), fail < 0))
  493. return 0;
  494. if (fail >= 0)
  495. return 1;
  496. if (!TEST_uint64_t_eq(stream_id, 0x1234))
  497. return 0;
  498. if (!TEST_uint64_t_eq(max_data, 0x9781))
  499. return 0;
  500. return 1;
  501. }
  502. static const unsigned char encode_case_14_expect[] = {
  503. 0x15, /* Type */
  504. 0x52, 0x34, /* Stream ID */
  505. 0x80, 0x00, 0x97, 0x81, /* Max Data */
  506. };
  507. /* 15. STREAMS_BLOCKED */
  508. static int encode_case_15_enc(WPACKET *pkt)
  509. {
  510. if (!TEST_int_eq(ossl_quic_wire_encode_frame_streams_blocked(pkt, 0, 0x1234), 1))
  511. return 0;
  512. if (!TEST_int_eq(ossl_quic_wire_encode_frame_streams_blocked(pkt, 1, 0x9781), 1))
  513. return 0;
  514. return 1;
  515. }
  516. static int encode_case_15_dec(PACKET *pkt, ossl_ssize_t fail)
  517. {
  518. uint64_t max_streams_1 = 0, max_streams_2 = 0,
  519. frame_type_1 = 0, frame_type_2 = 0;
  520. int is_minimal = 1, success_if;
  521. success_if = (fail < 0 || fail >= 1);
  522. if (!TEST_int_eq(ossl_quic_wire_peek_frame_header(pkt, &frame_type_1,
  523. &is_minimal),
  524. success_if))
  525. return 0;
  526. if (!TEST_true(!success_if || is_minimal))
  527. return 0;
  528. success_if = (fail < 0 || fail >= 3);
  529. if (!TEST_int_eq(ossl_quic_wire_decode_frame_streams_blocked(pkt,
  530. &max_streams_1),
  531. success_if))
  532. return 0;
  533. success_if = (fail < 0 || fail >= 4);
  534. if (!TEST_int_eq(ossl_quic_wire_peek_frame_header(pkt, &frame_type_2,
  535. &is_minimal),
  536. success_if))
  537. return 0;
  538. if (!TEST_true(!success_if || is_minimal))
  539. return 0;
  540. if (!TEST_int_eq(ossl_quic_wire_decode_frame_streams_blocked(pkt,
  541. &max_streams_2),
  542. fail < 0 || fail >= 8))
  543. return 0;
  544. if ((fail < 0 || fail >= 1)
  545. && !TEST_uint64_t_eq(frame_type_1, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_BIDI))
  546. return 0;
  547. if ((fail < 0 || fail >= 3)
  548. && !TEST_uint64_t_eq(max_streams_1, 0x1234))
  549. return 0;
  550. if ((fail < 0 || fail >= 4)
  551. && !TEST_uint64_t_eq(frame_type_2, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_UNI))
  552. return 0;
  553. if ((fail < 0 || fail >= 8)
  554. && !TEST_uint64_t_eq(max_streams_2, 0x9781))
  555. return 0;
  556. return 1;
  557. }
  558. static const unsigned char encode_case_15_expect[] = {
  559. 0x16, /* Type (STREAMS_BLOCKED Bidirectional) */
  560. 0x52, 0x34, /* Max Streams */
  561. 0x17, /* Type (STREAMS_BLOCKED Unidirectional) */
  562. 0x80, 0x00, 0x97, 0x81, /* Max Streams */
  563. };
  564. /* 16. NEW_CONNECTION_ID */
  565. static const unsigned char encode_case_16_conn_id[] = {
  566. 0x33, 0x44, 0x55, 0x66
  567. };
  568. static const OSSL_QUIC_FRAME_NEW_CONN_ID encode_case_16_f = {
  569. 0x9781,
  570. 0x1234,
  571. {
  572. 0x4,
  573. {0x33, 0x44, 0x55, 0x66}
  574. },
  575. {
  576. {
  577. 0xde, 0x06, 0xcb, 0x76, 0x5d, 0xb1, 0xa7, 0x71,
  578. 0x78, 0x09, 0xbb, 0xe8, 0x50, 0x19, 0x12, 0x9a
  579. }
  580. }
  581. };
  582. static int encode_case_16_enc(WPACKET *pkt)
  583. {
  584. if (!TEST_int_eq(ossl_quic_wire_encode_frame_new_conn_id(pkt,
  585. &encode_case_16_f), 1))
  586. return 0;
  587. return 1;
  588. }
  589. static int encode_case_16_dec(PACKET *pkt, ossl_ssize_t fail)
  590. {
  591. OSSL_QUIC_FRAME_NEW_CONN_ID f = {0};
  592. if (!TEST_int_eq(ossl_quic_wire_decode_frame_new_conn_id(pkt, &f), fail < 0))
  593. return 0;
  594. if (fail >= 0)
  595. return 1;
  596. if (!TEST_uint64_t_eq(f.seq_num, 0x9781))
  597. return 0;
  598. if (!TEST_uint64_t_eq(f.retire_prior_to, 0x1234))
  599. return 0;
  600. if (!TEST_uint64_t_eq(f.conn_id.id_len, sizeof(encode_case_16_conn_id)))
  601. return 0;
  602. if (!TEST_mem_eq(f.conn_id.id, f.conn_id.id_len,
  603. encode_case_16_conn_id, sizeof(encode_case_16_conn_id)))
  604. return 0;
  605. if (!TEST_mem_eq(f.stateless_reset.token,
  606. sizeof(f.stateless_reset.token),
  607. encode_case_16_f.stateless_reset.token,
  608. sizeof(encode_case_16_f.stateless_reset.token)))
  609. return 0;
  610. return 1;
  611. }
  612. static const unsigned char encode_case_16_expect[] = {
  613. 0x18, /* Type */
  614. 0x80, 0x00, 0x97, 0x81, /* Sequence Number */
  615. 0x52, 0x34, /* Retire Prior To */
  616. 0x04, /* Connection ID Length */
  617. 0x33, 0x44, 0x55, 0x66, /* Connection ID */
  618. 0xde, 0x06, 0xcb, 0x76, 0x5d, 0xb1, 0xa7, 0x71, /* Stateless Reset Token */
  619. 0x78, 0x09, 0xbb, 0xe8, 0x50, 0x19, 0x12, 0x9a
  620. };
  621. /* 16b. NEW_CONNECTION_ID seq_num < retire_prior_to */
  622. static const OSSL_QUIC_FRAME_NEW_CONN_ID encode_case_16b_f = {
  623. 0x1234,
  624. 0x9781,
  625. {
  626. 0x4,
  627. {0x33, 0x44, 0x55, 0x66}
  628. },
  629. {
  630. {
  631. 0xde, 0x06, 0xcb, 0x76, 0x5d, 0xb1, 0xa7, 0x71,
  632. 0x78, 0x09, 0xbb, 0xe8, 0x50, 0x19, 0x12, 0x9a
  633. }
  634. }
  635. };
  636. static int encode_case_16b_enc(WPACKET *pkt)
  637. {
  638. if (!TEST_int_eq(ossl_quic_wire_encode_frame_new_conn_id(pkt,
  639. &encode_case_16b_f), 1))
  640. return 0;
  641. return 1;
  642. }
  643. static int encode_case_16b_dec(PACKET *pkt, ossl_ssize_t fail)
  644. {
  645. OSSL_QUIC_FRAME_NEW_CONN_ID f = {0};
  646. if (!TEST_int_eq(ossl_quic_wire_decode_frame_new_conn_id(pkt, &f), 0))
  647. return 0;
  648. if (!TEST_true(PACKET_forward(pkt, PACKET_remaining(pkt))))
  649. return 0;
  650. return 1;
  651. }
  652. static const unsigned char encode_case_16b_expect[] = {
  653. 0x18, /* Type */
  654. 0x52, 0x34, /* Sequence Number */
  655. 0x80, 0x00, 0x97, 0x81, /* Retire Prior To */
  656. 0x04, /* Connection ID Length */
  657. 0x33, 0x44, 0x55, 0x66, /* Connection ID */
  658. 0xde, 0x06, 0xcb, 0x76, 0x5d, 0xb1, 0xa7, 0x71, /* Stateless Reset Token */
  659. 0x78, 0x09, 0xbb, 0xe8, 0x50, 0x19, 0x12, 0x9a
  660. };
  661. /* 17. RETIRE_CONNECTION_ID */
  662. static int encode_case_17_enc(WPACKET *pkt)
  663. {
  664. if (!TEST_int_eq(ossl_quic_wire_encode_frame_retire_conn_id(pkt, 0x1234), 1))
  665. return 0;
  666. return 1;
  667. }
  668. static int encode_case_17_dec(PACKET *pkt, ossl_ssize_t fail)
  669. {
  670. uint64_t seq_num = 0;
  671. if (!TEST_int_eq(ossl_quic_wire_decode_frame_retire_conn_id(pkt, &seq_num), fail < 0))
  672. return 0;
  673. if (fail >= 0)
  674. return 1;
  675. if (!TEST_uint64_t_eq(seq_num, 0x1234))
  676. return 0;
  677. return 1;
  678. }
  679. static const unsigned char encode_case_17_expect[] = {
  680. 0x19, /* Type */
  681. 0x52, 0x34, /* Seq Num */
  682. };
  683. /* 18. PATH_CHALLENGE */
  684. static const uint64_t encode_case_18_data
  685. = (((uint64_t)0x5f4b12)<<40) | (uint64_t)0x731834UL;
  686. static int encode_case_18_enc(WPACKET *pkt)
  687. {
  688. if (!TEST_int_eq(ossl_quic_wire_encode_frame_path_challenge(pkt,
  689. encode_case_18_data), 1))
  690. return 0;
  691. return 1;
  692. }
  693. static int encode_case_18_dec(PACKET *pkt, ossl_ssize_t fail)
  694. {
  695. uint64_t challenge = 0;
  696. if (!TEST_int_eq(ossl_quic_wire_decode_frame_path_challenge(pkt, &challenge), fail < 0))
  697. return 0;
  698. if (fail >= 0)
  699. return 1;
  700. if (!TEST_uint64_t_eq(challenge, encode_case_18_data))
  701. return 0;
  702. return 1;
  703. }
  704. static const unsigned char encode_case_18_expect[] = {
  705. 0x1A, /* Type */
  706. 0x5f, 0x4b, 0x12, 0x00, 0x00, 0x73, 0x18, 0x34, /* Data */
  707. };
  708. /* 19. PATH_RESPONSE */
  709. static const uint64_t encode_case_19_data
  710. = (((uint64_t)0x5f4b12)<<40) | (uint64_t)0x731834UL;
  711. static int encode_case_19_enc(WPACKET *pkt)
  712. {
  713. if (!TEST_int_eq(ossl_quic_wire_encode_frame_path_response(pkt,
  714. encode_case_19_data), 1))
  715. return 0;
  716. return 1;
  717. }
  718. static int encode_case_19_dec(PACKET *pkt, ossl_ssize_t fail)
  719. {
  720. uint64_t challenge = 0;
  721. if (!TEST_int_eq(ossl_quic_wire_decode_frame_path_response(pkt, &challenge), fail < 0))
  722. return 0;
  723. if (fail >= 0)
  724. return 1;
  725. if (!TEST_uint64_t_eq(challenge, encode_case_19_data))
  726. return 0;
  727. return 1;
  728. }
  729. static const unsigned char encode_case_19_expect[] = {
  730. 0x1B, /* Type */
  731. 0x5f, 0x4b, 0x12, 0x00, 0x00, 0x73, 0x18, 0x34, /* Data */
  732. };
  733. /* 20. CONNECTION_CLOSE (transport) */
  734. static const char encode_case_20_reason[] = {
  735. /* "reason for closure" */
  736. 0x72, 0x65, 0x61, 0x73, 0x6f, 0x6e, 0x20, 0x66, 0x6f,
  737. 0x72, 0x20, 0x63, 0x6c, 0x6f, 0x73, 0x75, 0x72, 0x65
  738. };
  739. static const OSSL_QUIC_FRAME_CONN_CLOSE encode_case_20_f = {
  740. 0,
  741. 0x1234,
  742. 0x9781,
  743. (char *)encode_case_20_reason,
  744. sizeof(encode_case_20_reason)
  745. };
  746. static int encode_case_20_enc(WPACKET *pkt)
  747. {
  748. if (!TEST_int_eq(ossl_quic_wire_encode_frame_conn_close(pkt,
  749. &encode_case_20_f), 1))
  750. return 0;
  751. return 1;
  752. }
  753. static int encode_case_20_dec(PACKET *pkt, ossl_ssize_t fail)
  754. {
  755. OSSL_QUIC_FRAME_CONN_CLOSE f = {0};
  756. if (!TEST_int_eq(ossl_quic_wire_decode_frame_conn_close(pkt, &f), fail < 0))
  757. return 0;
  758. if (fail >= 0)
  759. return 1;
  760. if (!TEST_int_eq(f.is_app, 0))
  761. return 0;
  762. if (!TEST_uint64_t_eq(f.error_code, 0x1234))
  763. return 0;
  764. if (!TEST_uint64_t_eq(f.frame_type, 0x9781))
  765. return 0;
  766. if (!TEST_size_t_eq(f.reason_len, 18))
  767. return 0;
  768. if (!TEST_mem_eq(f.reason, f.reason_len,
  769. encode_case_20_f.reason, encode_case_20_f.reason_len))
  770. return 0;
  771. return 1;
  772. }
  773. static const unsigned char encode_case_20_expect[] = {
  774. 0x1C, /* Type */
  775. 0x52, 0x34, /* Sequence Number */
  776. 0x80, 0x00, 0x97, 0x81, /* Frame Type */
  777. 0x12, /* Reason Length */
  778. 0x72, 0x65, 0x61, 0x73, 0x6f, 0x6e, 0x20, 0x66, 0x6f, /* Reason */
  779. 0x72, 0x20, 0x63, 0x6c, 0x6f, 0x73, 0x75, 0x72, 0x65
  780. };
  781. /* 21. HANDSHAKE_DONE */
  782. static int encode_case_21_enc(WPACKET *pkt)
  783. {
  784. if (!TEST_int_eq(ossl_quic_wire_encode_frame_handshake_done(pkt), 1))
  785. return 0;
  786. return 1;
  787. }
  788. static int encode_case_21_dec(PACKET *pkt, ossl_ssize_t fail)
  789. {
  790. if (!TEST_int_eq(ossl_quic_wire_decode_frame_handshake_done(pkt), fail < 0))
  791. return 0;
  792. return 1;
  793. }
  794. static const unsigned char encode_case_21_expect[] = {
  795. 0x1E
  796. };
  797. /* 22. Buffer Transport Parameter */
  798. static const unsigned char encode_case_22_data[] = {0x55,0x77,0x32,0x46,0x99};
  799. static int encode_case_22_enc(WPACKET *pkt)
  800. {
  801. unsigned char *p;
  802. if (!TEST_ptr(ossl_quic_wire_encode_transport_param_bytes(pkt, 0x1234,
  803. encode_case_22_data,
  804. sizeof(encode_case_22_data))))
  805. return 0;
  806. if (!TEST_ptr(p = ossl_quic_wire_encode_transport_param_bytes(pkt, 0x9781,
  807. NULL, 2)))
  808. return 0;
  809. p[0] = 0x33;
  810. p[1] = 0x44;
  811. return 1;
  812. }
  813. static int encode_case_22_dec(PACKET *pkt, ossl_ssize_t fail)
  814. {
  815. uint64_t id = 0;
  816. size_t len = 0;
  817. const unsigned char *p;
  818. static const unsigned char data[] = {0x33, 0x44};
  819. if (!TEST_int_eq(ossl_quic_wire_peek_transport_param(pkt, &id),
  820. fail < 0 || fail >= 2))
  821. return 0;
  822. if ((fail < 0 || fail >= 2)
  823. && !TEST_uint64_t_eq(id, 0x1234))
  824. return 0;
  825. id = 0;
  826. p = ossl_quic_wire_decode_transport_param_bytes(pkt, &id, &len);
  827. if (fail < 0 || fail >= 8) {
  828. if (!TEST_ptr(p))
  829. return 0;
  830. } else {
  831. if (!TEST_ptr_null(p))
  832. return 0;
  833. }
  834. if ((fail < 0 || fail >= 8)
  835. && !TEST_uint64_t_eq(id, 0x1234))
  836. return 0;
  837. if ((fail < 0 || fail >= 8)
  838. && !TEST_mem_eq(p, len, encode_case_22_data, sizeof(encode_case_22_data)))
  839. return 0;
  840. if ((fail < 0 || fail >= 8)
  841. && !TEST_int_eq(ossl_quic_wire_peek_transport_param(pkt, &id),
  842. fail < 0 || fail >= 12))
  843. return 0;
  844. if ((fail < 0 || fail >= 12)
  845. && !TEST_uint64_t_eq(id, 0x9781))
  846. return 0;
  847. id = 0;
  848. p = ossl_quic_wire_decode_transport_param_bytes(pkt, &id, &len);
  849. if (fail < 0 || fail >= 15) {
  850. if (!TEST_ptr(p))
  851. return 0;
  852. } else {
  853. if (!TEST_ptr_null(p))
  854. return 0;
  855. }
  856. if ((fail < 0 || fail >= 15)
  857. && !TEST_uint64_t_eq(id, 0x9781))
  858. return 0;
  859. if ((fail < 0 || fail >= 15)
  860. && !TEST_mem_eq(p, len, data, sizeof(data)))
  861. return 0;
  862. return 1;
  863. }
  864. static const unsigned char encode_case_22_expect[] = {
  865. 0x52, 0x34, /* ID */
  866. 0x05, /* Length */
  867. 0x55, 0x77, 0x32, 0x46, 0x99, /* Data */
  868. 0x80, 0x00, 0x97, 0x81, /* ID */
  869. 0x02, /* Length */
  870. 0x33, 0x44 /* Data */
  871. };
  872. /* 23. Integer Transport Parameter */
  873. static int encode_case_23_enc(WPACKET *pkt)
  874. {
  875. if (!TEST_int_eq(ossl_quic_wire_encode_transport_param_int(pkt, 0x1234, 0x9781), 1))
  876. return 0;
  877. if (!TEST_int_eq(ossl_quic_wire_encode_transport_param_int(pkt, 0x2233, 0x4545), 1))
  878. return 0;
  879. return 1;
  880. }
  881. static int encode_case_23_dec(PACKET *pkt, ossl_ssize_t fail)
  882. {
  883. uint64_t id = 0, value = 0;
  884. if (!TEST_int_eq(ossl_quic_wire_decode_transport_param_int(pkt,
  885. &id, &value),
  886. fail < 0 || fail >= 7))
  887. return 0;
  888. if ((fail < 0 || fail >= 7)
  889. && !TEST_uint64_t_eq(id, 0x1234))
  890. return 0;
  891. if ((fail < 0 || fail >= 7)
  892. && !TEST_uint64_t_eq(value, 0x9781))
  893. return 0;
  894. if (!TEST_int_eq(ossl_quic_wire_decode_transport_param_int(pkt,
  895. &id, &value),
  896. fail < 0 || fail >= 14))
  897. return 0;
  898. if ((fail < 0 || fail >= 14)
  899. && !TEST_uint64_t_eq(id, 0x2233))
  900. return 0;
  901. if ((fail < 0 || fail >= 14)
  902. && !TEST_uint64_t_eq(value, 0x4545))
  903. return 0;
  904. return 1;
  905. }
  906. static const unsigned char encode_case_23_expect[] = {
  907. 0x52, 0x34,
  908. 0x04,
  909. 0x80, 0x00, 0x97, 0x81,
  910. 0x62, 0x33,
  911. 0x04,
  912. 0x80, 0x00, 0x45, 0x45,
  913. };
  914. #define ENCODE_CASE(n) \
  915. { \
  916. encode_case_##n##_enc, \
  917. encode_case_##n##_expect, \
  918. OSSL_NELEM(encode_case_##n##_expect), \
  919. encode_case_##n##_dec \
  920. },
  921. static const struct encode_test_case encode_cases[] = {
  922. ENCODE_CASE(1)
  923. ENCODE_CASE(2)
  924. ENCODE_CASE(3)
  925. ENCODE_CASE(4)
  926. ENCODE_CASE(5)
  927. ENCODE_CASE(6)
  928. ENCODE_CASE(7)
  929. ENCODE_CASE(8)
  930. ENCODE_CASE(9)
  931. ENCODE_CASE(10)
  932. ENCODE_CASE(11)
  933. ENCODE_CASE(12)
  934. ENCODE_CASE(13)
  935. ENCODE_CASE(14)
  936. ENCODE_CASE(15)
  937. ENCODE_CASE(16)
  938. ENCODE_CASE(16b)
  939. ENCODE_CASE(17)
  940. ENCODE_CASE(18)
  941. ENCODE_CASE(19)
  942. ENCODE_CASE(20)
  943. ENCODE_CASE(21)
  944. ENCODE_CASE(22)
  945. ENCODE_CASE(23)
  946. };
  947. static int test_wire_encode(int idx)
  948. {
  949. int testresult = 0;
  950. WPACKET wpkt;
  951. PACKET pkt;
  952. BUF_MEM *buf = NULL;
  953. size_t written;
  954. const struct encode_test_case *c = &encode_cases[idx];
  955. int have_wpkt = 0;
  956. size_t i;
  957. if (!TEST_ptr(buf = BUF_MEM_new()))
  958. goto err;
  959. if (!TEST_int_eq(WPACKET_init(&wpkt, buf), 1))
  960. goto err;
  961. have_wpkt = 1;
  962. if (!TEST_int_eq(c->serializer(&wpkt), 1))
  963. goto err;
  964. if (!TEST_int_eq(WPACKET_get_total_written(&wpkt, &written), 1))
  965. goto err;
  966. if (!TEST_mem_eq(buf->data, written, c->expect_buf, c->expect_buf_len))
  967. goto err;
  968. if (!TEST_int_eq(PACKET_buf_init(&pkt, (unsigned char *)buf->data, written), 1))
  969. goto err;
  970. if (!TEST_int_eq(c->deserializer(&pkt, -1), 1))
  971. goto err;
  972. if (!TEST_false(PACKET_remaining(&pkt)))
  973. goto err;
  974. for (i = 0; i < c->expect_buf_len; ++i) {
  975. PACKET pkt2;
  976. /*
  977. * Check parsing truncated (i.e., malformed) input is handled correctly.
  978. * Generate all possible truncations of our reference encoding and
  979. * verify that they are handled correctly. The number of bytes of the
  980. * truncated encoding is passed as an argument to the deserializer to
  981. * help it determine whether decoding should fail or not.
  982. */
  983. if (!TEST_int_eq(PACKET_buf_init(&pkt2, (unsigned char *)c->expect_buf, i), 1))
  984. goto err;
  985. if (!TEST_int_eq(c->deserializer(&pkt2, i), 1))
  986. goto err;
  987. }
  988. testresult = 1;
  989. err:
  990. if (have_wpkt)
  991. WPACKET_finish(&wpkt);
  992. BUF_MEM_free(buf);
  993. return testresult;
  994. }
  995. struct ack_test_case {
  996. const unsigned char *input_buf;
  997. size_t input_buf_len;
  998. int (*deserializer)(PACKET *pkt);
  999. int expect_fail;
  1000. };
  1001. /* ACK Frame with Excessive First ACK Range Field */
  1002. static const unsigned char ack_case_1_input[] = {
  1003. 0x02, /* ACK Without ECN */
  1004. 0x08, /* Largest Acknowledged */
  1005. 0x01, /* ACK Delay */
  1006. 0x00, /* ACK Range Count */
  1007. 0x09, /* First ACK Range */
  1008. };
  1009. /* ACK Frame with Valid ACK Range Field */
  1010. static const unsigned char ack_case_2_input[] = {
  1011. 0x02, /* ACK Without ECN */
  1012. 0x08, /* Largest Acknowledged */
  1013. 0x01, /* ACK Delay */
  1014. 0x00, /* ACK Range Count */
  1015. 0x08, /* First ACK Range */
  1016. };
  1017. /* ACK Frame with Excessive ACK Range Gap */
  1018. static const unsigned char ack_case_3_input[] = {
  1019. 0x02, /* ACK Without ECN */
  1020. 0x08, /* Largest Acknowledged */
  1021. 0x01, /* ACK Delay */
  1022. 0x01, /* ACK Range Count */
  1023. 0x01, /* First ACK Range */
  1024. 0x05, /* Gap */
  1025. 0x01, /* ACK Range Length */
  1026. };
  1027. /* ACK Frame with Valid ACK Range */
  1028. static const unsigned char ack_case_4_input[] = {
  1029. 0x02, /* ACK Without ECN */
  1030. 0x08, /* Largest Acknowledged */
  1031. 0x01, /* ACK Delay */
  1032. 0x01, /* ACK Range Count */
  1033. 0x01, /* First ACK Range */
  1034. 0x04, /* Gap */
  1035. 0x01, /* ACK Range Length */
  1036. };
  1037. /* ACK Frame with Excessive ACK Range Length */
  1038. static const unsigned char ack_case_5_input[] = {
  1039. 0x02, /* ACK Without ECN */
  1040. 0x08, /* Largest Acknowledged */
  1041. 0x01, /* ACK Delay */
  1042. 0x01, /* ACK Range Count */
  1043. 0x01, /* First ACK Range */
  1044. 0x04, /* Gap */
  1045. 0x02, /* ACK Range Length */
  1046. };
  1047. /* ACK Frame with Multiple ACK Ranges, Final Having Excessive Length */
  1048. static const unsigned char ack_case_6_input[] = {
  1049. 0x02, /* ACK Without ECN */
  1050. 0x08, /* Largest Acknowledged */
  1051. 0x01, /* ACK Delay */
  1052. 0x02, /* ACK Range Count */
  1053. 0x01, /* First ACK Range */
  1054. 0x01, /* Gap */
  1055. 0x02, /* ACK Range Length */
  1056. 0x00, /* Gap */
  1057. 0x01, /* ACK Range Length */
  1058. };
  1059. /* ACK Frame with Multiple ACK Ranges, Valid */
  1060. static const unsigned char ack_case_7_input[] = {
  1061. 0x02, /* ACK Without ECN */
  1062. 0x08, /* Largest Acknowledged */
  1063. 0x01, /* ACK Delay */
  1064. 0x02, /* ACK Range Count */
  1065. 0x01, /* First ACK Range */
  1066. 0x01, /* Gap */
  1067. 0x02, /* ACK Range Length */
  1068. 0x00, /* Gap */
  1069. 0x00, /* ACK Range Length */
  1070. };
  1071. static int ack_generic_decode(PACKET *pkt)
  1072. {
  1073. OSSL_QUIC_ACK_RANGE ranges[8] = {0};
  1074. OSSL_QUIC_FRAME_ACK f = {0};
  1075. uint64_t total_ranges = 0, peek_total_ranges = 0;
  1076. int r;
  1077. size_t i;
  1078. f.ack_ranges = ranges;
  1079. f.num_ack_ranges = OSSL_NELEM(ranges);
  1080. if (!TEST_int_eq(ossl_quic_wire_peek_frame_ack_num_ranges(pkt,
  1081. &peek_total_ranges), 1))
  1082. return 0;
  1083. r = ossl_quic_wire_decode_frame_ack(pkt, 3, &f, &total_ranges);
  1084. if (r == 0)
  1085. return 0;
  1086. if (!TEST_uint64_t_eq(total_ranges, peek_total_ranges))
  1087. return 0;
  1088. for (i = 0; i < f.num_ack_ranges; ++i) {
  1089. if (!TEST_uint64_t_le(f.ack_ranges[i].start, f.ack_ranges[i].end))
  1090. return 0;
  1091. if (!TEST_uint64_t_lt(f.ack_ranges[i].end, 1000))
  1092. return 0;
  1093. }
  1094. return 1;
  1095. }
  1096. #define ACK_CASE(n, expect_fail, dec) \
  1097. { \
  1098. ack_case_##n##_input, \
  1099. sizeof(ack_case_##n##_input), \
  1100. (dec), \
  1101. (expect_fail) \
  1102. },
  1103. static const struct ack_test_case ack_cases[] = {
  1104. ACK_CASE(1, 1, ack_generic_decode)
  1105. ACK_CASE(2, 0, ack_generic_decode)
  1106. ACK_CASE(3, 1, ack_generic_decode)
  1107. ACK_CASE(4, 0, ack_generic_decode)
  1108. ACK_CASE(5, 1, ack_generic_decode)
  1109. ACK_CASE(6, 1, ack_generic_decode)
  1110. ACK_CASE(7, 0, ack_generic_decode)
  1111. };
  1112. static int test_wire_ack(int idx)
  1113. {
  1114. int testresult = 0, r;
  1115. PACKET pkt;
  1116. const struct ack_test_case *c = &ack_cases[idx];
  1117. if (!TEST_int_eq(PACKET_buf_init(&pkt,
  1118. (unsigned char *)c->input_buf,
  1119. c->input_buf_len), 1))
  1120. goto err;
  1121. r = c->deserializer(&pkt);
  1122. if (c->expect_fail) {
  1123. if (!TEST_int_eq(r, 0))
  1124. goto err;
  1125. } else {
  1126. if (!TEST_int_eq(r, 1))
  1127. goto err;
  1128. if (!TEST_false(PACKET_remaining(&pkt)))
  1129. goto err;
  1130. }
  1131. testresult = 1;
  1132. err:
  1133. return testresult;
  1134. }
  1135. /* Packet Header PN Encoding Tests */
  1136. struct pn_test {
  1137. QUIC_PN pn, tx_largest_acked, rx_largest_pn;
  1138. char expected_len;
  1139. unsigned char expected_bytes[4];
  1140. };
  1141. static const struct pn_test pn_tests[] = {
  1142. /* RFC 9000 Section A.2 */
  1143. { 0xac5c02, 0xabe8b3, 0xabe8b3, 2, {0x5c,0x02} },
  1144. { 0xace8fe, 0xabe8b3, 0xabe8b3, 3, {0xac,0xe8,0xfe} },
  1145. /* RFC 9000 Section A.3 */
  1146. { 0xa82f9b32, 0xa82f30ea, 0xa82f30ea, 2, {0x9b,0x32} },
  1147. /* Boundary Cases */
  1148. { 1, 0, 0, 1, {0x01} },
  1149. { 256, 255, 255, 1, {0x00} },
  1150. { 257, 255, 255, 1, {0x01} },
  1151. { 256, 128, 128, 1, {0x00} },
  1152. { 256, 127, 127, 2, {0x01,0x00} },
  1153. { 65536, 32768, 32768, 2, {0x00,0x00} },
  1154. { 65537, 32769, 32769, 2, {0x00,0x01} },
  1155. { 65536, 32767, 32767, 3, {0x01,0x00,0x00} },
  1156. { 65537, 32768, 32768, 3, {0x01,0x00,0x01} },
  1157. { 16777216, 8388608, 8388608, 3, {0x00,0x00,0x00} },
  1158. { 16777217, 8388609, 8388609, 3, {0x00,0x00,0x01} },
  1159. { 16777216, 8388607, 8388607, 4, {0x01,0x00,0x00,0x00} },
  1160. { 16777217, 8388608, 8388608, 4, {0x01,0x00,0x00,0x01} },
  1161. { 4294967296, 2147483648, 2147483648, 4, {0x00,0x00,0x00,0x00} },
  1162. { 4294967297, 2147483648, 2147483648, 4, {0x00,0x00,0x00,0x01} },
  1163. };
  1164. static int test_wire_pkt_hdr_pn(int tidx)
  1165. {
  1166. int testresult = 0;
  1167. const struct pn_test *t = &pn_tests[tidx];
  1168. unsigned char buf[4];
  1169. int pn_len;
  1170. QUIC_PN res_pn;
  1171. pn_len = ossl_quic_wire_determine_pn_len(t->pn, t->tx_largest_acked);
  1172. if (!TEST_int_eq(pn_len, (int)t->expected_len))
  1173. goto err;
  1174. if (!TEST_true(ossl_quic_wire_encode_pkt_hdr_pn(t->pn, buf, pn_len)))
  1175. goto err;
  1176. if (!TEST_mem_eq(t->expected_bytes, t->expected_len, buf, pn_len))
  1177. goto err;
  1178. if (!TEST_true(ossl_quic_wire_decode_pkt_hdr_pn(buf, pn_len,
  1179. t->rx_largest_pn, &res_pn)))
  1180. goto err;
  1181. if (!TEST_uint64_t_eq(res_pn, t->pn))
  1182. goto err;
  1183. testresult = 1;
  1184. err:
  1185. return testresult;
  1186. }
  1187. /* RFC 9001 s. A.4 */
  1188. static const QUIC_CONN_ID retry_orig_dcid = {
  1189. 8, { 0x83, 0x94, 0xc8, 0xf0, 0x3e, 0x51, 0x57, 0x08 }
  1190. };
  1191. static const unsigned char retry_encoded[] = {
  1192. 0xff, /* Long Header, Retry */
  1193. 0x00, 0x00, 0x00, 0x01, /* Version 1 */
  1194. 0x00, /* DCID */
  1195. 0x08, 0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5, /* SCID */
  1196. /* Retry Token */
  1197. 0x74, 0x6f, 0x6b, 0x65, 0x6e,
  1198. /* Retry Integrity Tag */
  1199. 0x04, 0xa2, 0x65, 0xba, 0x2e, 0xff, 0x4d, 0x82, 0x90, 0x58, 0xfb, 0x3f, 0x0f,
  1200. 0x24, 0x96, 0xba
  1201. };
  1202. static int test_wire_retry_integrity_tag(void)
  1203. {
  1204. int testresult = 0;
  1205. PACKET pkt = {0};
  1206. QUIC_PKT_HDR hdr = {0};
  1207. unsigned char got_tag[QUIC_RETRY_INTEGRITY_TAG_LEN] = {0};
  1208. if (!TEST_true(PACKET_buf_init(&pkt, retry_encoded, sizeof(retry_encoded))))
  1209. goto err;
  1210. if (!TEST_true(ossl_quic_wire_decode_pkt_hdr(&pkt, 0, 0, 0, &hdr, NULL)))
  1211. goto err;
  1212. if (!TEST_int_eq(hdr.type, QUIC_PKT_TYPE_RETRY))
  1213. goto err;
  1214. if (!TEST_true(ossl_quic_calculate_retry_integrity_tag(NULL, NULL, &hdr,
  1215. &retry_orig_dcid,
  1216. got_tag)))
  1217. goto err;
  1218. if (!TEST_mem_eq(got_tag, sizeof(got_tag),
  1219. retry_encoded + sizeof(retry_encoded)
  1220. - QUIC_RETRY_INTEGRITY_TAG_LEN,
  1221. QUIC_RETRY_INTEGRITY_TAG_LEN))
  1222. goto err;
  1223. if (!TEST_true(ossl_quic_validate_retry_integrity_tag(NULL, NULL, &hdr,
  1224. &retry_orig_dcid)))
  1225. goto err;
  1226. testresult = 1;
  1227. err:
  1228. return testresult;
  1229. }
  1230. /* is_minimal=0 test */
  1231. static const unsigned char non_minimal_1[] = {
  1232. 0x40, 0x00,
  1233. };
  1234. static const unsigned char non_minimal_2[] = {
  1235. 0x40, 0x3F,
  1236. };
  1237. static const unsigned char non_minimal_3[] = {
  1238. 0x80, 0x00, 0x00, 0x00,
  1239. };
  1240. static const unsigned char non_minimal_4[] = {
  1241. 0x80, 0x00, 0x3F, 0xFF,
  1242. };
  1243. static const unsigned char non_minimal_5[] = {
  1244. 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  1245. };
  1246. static const unsigned char non_minimal_6[] = {
  1247. 0xC0, 0x00, 0x00, 0x00, 0x3F, 0xFF, 0xFF, 0xFF
  1248. };
  1249. static const unsigned char *const non_minimal[] = {
  1250. non_minimal_1,
  1251. non_minimal_2,
  1252. non_minimal_3,
  1253. non_minimal_4,
  1254. non_minimal_5,
  1255. non_minimal_6,
  1256. };
  1257. static const size_t non_minimal_len[] = {
  1258. OSSL_NELEM(non_minimal_1),
  1259. OSSL_NELEM(non_minimal_2),
  1260. OSSL_NELEM(non_minimal_3),
  1261. OSSL_NELEM(non_minimal_4),
  1262. OSSL_NELEM(non_minimal_5),
  1263. OSSL_NELEM(non_minimal_6),
  1264. };
  1265. static int test_wire_minimal(int idx)
  1266. {
  1267. int testresult = 0;
  1268. int is_minimal;
  1269. uint64_t frame_type;
  1270. PACKET pkt;
  1271. if (!TEST_true(PACKET_buf_init(&pkt, non_minimal[idx],
  1272. non_minimal_len[idx])))
  1273. goto err;
  1274. if (!TEST_true(ossl_quic_wire_peek_frame_header(&pkt, &frame_type,
  1275. &is_minimal)))
  1276. goto err;
  1277. if (!TEST_false(is_minimal))
  1278. goto err;
  1279. testresult = 1;
  1280. err:
  1281. return testresult;
  1282. }
  1283. int setup_tests(void)
  1284. {
  1285. ADD_ALL_TESTS(test_wire_encode, OSSL_NELEM(encode_cases));
  1286. ADD_ALL_TESTS(test_wire_ack, OSSL_NELEM(ack_cases));
  1287. ADD_ALL_TESTS(test_wire_pkt_hdr_pn, OSSL_NELEM(pn_tests));
  1288. ADD_TEST(test_wire_retry_integrity_tag);
  1289. ADD_ALL_TESTS(test_wire_minimal, OSSL_NELEM(non_minimal_len));
  1290. return 1;
  1291. }