wpackettest.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. /*
  2. * Copyright 2016-2020 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 <string.h>
  10. #include <openssl/buffer.h>
  11. #include <openssl/rand.h>
  12. #include "internal/packet.h"
  13. #include "testutil.h"
  14. static const unsigned char simple1[] = { 0xff };
  15. static const unsigned char simple2[] = { 0x01, 0xff };
  16. static const unsigned char simple3[] = { 0x00, 0x00, 0x00, 0x01, 0xff };
  17. static const unsigned char nestedsub[] = { 0x03, 0xff, 0x01, 0xff };
  18. static const unsigned char seqsub[] = { 0x01, 0xff, 0x01, 0xff };
  19. static const unsigned char empty[] = { 0x00 };
  20. static const unsigned char alloc[] = { 0x02, 0xfe, 0xff };
  21. static const unsigned char submem[] = { 0x03, 0x02, 0xfe, 0xff };
  22. static const unsigned char fixed[] = { 0xff, 0xff, 0xff };
  23. static const unsigned char simpleder[] = {
  24. 0xfc, 0x04, 0x00, 0x01, 0x02, 0x03, 0xff, 0xfe, 0xfd
  25. };
  26. /* QUIC sub-packet with 4-byte length prefix, containing a 1-byte vlint */
  27. static const unsigned char quic1[] = { 0x80, 0x00, 0x00, 0x01, 0x09 };
  28. /* QUIC sub-packet with 1-byte length prefix, containing a 1-byte vlint */
  29. static const unsigned char quic2[] = { 0x01, 0x09 };
  30. /* QUIC sub-packet with 2-byte length prefix, containing a 2-byte vlint */
  31. static const unsigned char quic3[] = { 0x40, 0x02, 0x40, 0x41 };
  32. /* QUIC sub-packet with 8-byte length prefix, containing a 4-byte vlint */
  33. static const unsigned char quic4[] = {
  34. 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
  35. 0x80, 0x01, 0x3c, 0x6a
  36. };
  37. /* QUIC sub-packet with 8-byte length prefix, containing a 8-byte vlint */
  38. static const unsigned char quic5[] = {
  39. 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
  40. 0xef, 0x77, 0x21, 0x3f, 0x3f, 0x50, 0x5b, 0xa5
  41. };
  42. /* QUIC sub-packet, length known up-front */
  43. static const unsigned char quic6[] = { 0x03, 0x55, 0x66, 0x77 };
  44. /* Nested and sequential sub-packets with length prefixes */
  45. static const unsigned char quic7[] = {
  46. 0x07, 0x80, 0x00, 0x00, 0x08, 0x65, 0x14, 0x40, 0x01, 0x05,
  47. 0x40, 0x01, 0x11, 0x40, 0x01, 0x12, 0x40, 0x01, 0x13
  48. };
  49. static BUF_MEM *buf;
  50. static int cleanup(WPACKET *pkt)
  51. {
  52. WPACKET_cleanup(pkt);
  53. return 0;
  54. }
  55. static int test_WPACKET_init(void)
  56. {
  57. WPACKET pkt;
  58. int i;
  59. size_t written;
  60. unsigned char sbuf[3];
  61. if (!TEST_true(WPACKET_init(&pkt, buf))
  62. || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
  63. /* Closing a top level WPACKET should fail */
  64. || !TEST_false(WPACKET_close(&pkt))
  65. /* Finishing a top level WPACKET should succeed */
  66. || !TEST_true(WPACKET_finish(&pkt))
  67. /*
  68. * Can't call close or finish on a WPACKET that's already
  69. * finished.
  70. */
  71. || !TEST_false(WPACKET_close(&pkt))
  72. || !TEST_false(WPACKET_finish(&pkt))
  73. || !TEST_true(WPACKET_get_total_written(&pkt, &written))
  74. || !TEST_mem_eq(buf->data, written, simple1, sizeof(simple1)))
  75. return cleanup(&pkt);
  76. /* Now try with a one byte length prefix */
  77. if (!TEST_true(WPACKET_init_len(&pkt, buf, 1))
  78. || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
  79. || !TEST_true(WPACKET_finish(&pkt))
  80. || !TEST_true(WPACKET_get_total_written(&pkt, &written))
  81. || !TEST_mem_eq(buf->data, written, simple2, sizeof(simple2)))
  82. return cleanup(&pkt);
  83. /* And a longer length prefix */
  84. if (!TEST_true(WPACKET_init_len(&pkt, buf, 4))
  85. || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
  86. || !TEST_true(WPACKET_finish(&pkt))
  87. || !TEST_true(WPACKET_get_total_written(&pkt, &written))
  88. || !TEST_mem_eq(buf->data, written, simple3, sizeof(simple3)))
  89. return cleanup(&pkt);
  90. if (!TEST_true(WPACKET_init_len(&pkt, buf, 1)))
  91. return cleanup(&pkt);
  92. for (i = 1; i < 257; i++) {
  93. /*
  94. * Putting more bytes in than fit for the size of the length prefix
  95. * should fail
  96. */
  97. if (!TEST_int_eq(WPACKET_put_bytes_u8(&pkt, 0xff), i < 256))
  98. return cleanup(&pkt);
  99. }
  100. if (!TEST_true(WPACKET_finish(&pkt)))
  101. return cleanup(&pkt);
  102. /* Test initialising from a fixed size buffer */
  103. if (!TEST_true(WPACKET_init_static_len(&pkt, sbuf, sizeof(sbuf), 0))
  104. /* Adding 3 bytes should succeed */
  105. || !TEST_true(WPACKET_put_bytes_u24(&pkt, 0xffffff))
  106. /* Adding 1 more byte should fail */
  107. || !TEST_false(WPACKET_put_bytes_u8(&pkt, 0xff))
  108. /* Finishing the top level WPACKET should succeed */
  109. || !TEST_true(WPACKET_finish(&pkt))
  110. || !TEST_true(WPACKET_get_total_written(&pkt, &written))
  111. || !TEST_mem_eq(sbuf, written, fixed, sizeof(sbuf))
  112. /* Initialise with 1 len byte */
  113. || !TEST_true(WPACKET_init_static_len(&pkt, sbuf, sizeof(sbuf), 1))
  114. /* Adding 2 bytes should succeed */
  115. || !TEST_true(WPACKET_put_bytes_u16(&pkt, 0xfeff))
  116. /* Adding 1 more byte should fail */
  117. || !TEST_false(WPACKET_put_bytes_u8(&pkt, 0xff))
  118. || !TEST_true(WPACKET_finish(&pkt))
  119. || !TEST_true(WPACKET_get_total_written(&pkt, &written))
  120. || !TEST_mem_eq(sbuf, written, alloc, sizeof(alloc)))
  121. return cleanup(&pkt);
  122. return 1;
  123. }
  124. static int test_WPACKET_set_max_size(void)
  125. {
  126. WPACKET pkt;
  127. size_t written;
  128. if (!TEST_true(WPACKET_init(&pkt, buf))
  129. /*
  130. * No previous lenbytes set so we should be ok to set the max
  131. * possible max size
  132. */
  133. || !TEST_true(WPACKET_set_max_size(&pkt, SIZE_MAX))
  134. /* We should be able to set it smaller too */
  135. || !TEST_true(WPACKET_set_max_size(&pkt, SIZE_MAX -1))
  136. /* And setting it bigger again should be ok */
  137. || !TEST_true(WPACKET_set_max_size(&pkt, SIZE_MAX))
  138. || !TEST_true(WPACKET_finish(&pkt)))
  139. return cleanup(&pkt);
  140. if (!TEST_true(WPACKET_init_len(&pkt, buf, 1))
  141. /*
  142. * Should fail because we already consumed 1 byte with the
  143. * length
  144. */
  145. || !TEST_false(WPACKET_set_max_size(&pkt, 0))
  146. /*
  147. * Max size can't be bigger than biggest that will fit in
  148. * lenbytes
  149. */
  150. || !TEST_false(WPACKET_set_max_size(&pkt, 0x0101))
  151. /* It can be the same as the maximum possible size */
  152. || !TEST_true(WPACKET_set_max_size(&pkt, 0x0100))
  153. /* Or it can be less */
  154. || !TEST_true(WPACKET_set_max_size(&pkt, 0x01))
  155. /* Should fail because packet is already filled */
  156. || !TEST_false(WPACKET_put_bytes_u8(&pkt, 0xff))
  157. /* You can't put in more bytes than max size */
  158. || !TEST_true(WPACKET_set_max_size(&pkt, 0x02))
  159. || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
  160. || !TEST_false(WPACKET_put_bytes_u8(&pkt, 0xff))
  161. || !TEST_true(WPACKET_finish(&pkt))
  162. || !TEST_true(WPACKET_get_total_written(&pkt, &written))
  163. || !TEST_mem_eq(buf->data, written, simple2, sizeof(simple2)))
  164. return cleanup(&pkt);
  165. return 1;
  166. }
  167. static int test_WPACKET_start_sub_packet(void)
  168. {
  169. WPACKET pkt;
  170. size_t written;
  171. size_t len;
  172. if (!TEST_true(WPACKET_init(&pkt, buf))
  173. || !TEST_true(WPACKET_start_sub_packet(&pkt))
  174. || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
  175. /* Can't finish because we have a sub packet */
  176. || !TEST_false(WPACKET_finish(&pkt))
  177. || !TEST_true(WPACKET_close(&pkt))
  178. /* Sub packet is closed so can't close again */
  179. || !TEST_false(WPACKET_close(&pkt))
  180. /* Now a top level so finish should succeed */
  181. || !TEST_true(WPACKET_finish(&pkt))
  182. || !TEST_true(WPACKET_get_total_written(&pkt, &written))
  183. || !TEST_mem_eq(buf->data, written, simple1, sizeof(simple1)))
  184. return cleanup(&pkt);
  185. /* Single sub-packet with length prefix */
  186. if (!TEST_true(WPACKET_init(&pkt, buf))
  187. || !TEST_true(WPACKET_start_sub_packet_u8(&pkt))
  188. || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
  189. || !TEST_true(WPACKET_close(&pkt))
  190. || !TEST_true(WPACKET_finish(&pkt))
  191. || !TEST_true(WPACKET_get_total_written(&pkt, &written))
  192. || !TEST_mem_eq(buf->data, written, simple2, sizeof(simple2)))
  193. return cleanup(&pkt);
  194. /* Nested sub-packets with length prefixes */
  195. if (!TEST_true(WPACKET_init(&pkt, buf))
  196. || !TEST_true(WPACKET_start_sub_packet_u8(&pkt))
  197. || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
  198. || !TEST_true(WPACKET_start_sub_packet_u8(&pkt))
  199. || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
  200. || !TEST_true(WPACKET_get_length(&pkt, &len))
  201. || !TEST_size_t_eq(len, 1)
  202. || !TEST_true(WPACKET_close(&pkt))
  203. || !TEST_true(WPACKET_get_length(&pkt, &len))
  204. || !TEST_size_t_eq(len, 3)
  205. || !TEST_true(WPACKET_close(&pkt))
  206. || !TEST_true(WPACKET_finish(&pkt))
  207. || !TEST_true(WPACKET_get_total_written(&pkt, &written))
  208. || !TEST_mem_eq(buf->data, written, nestedsub, sizeof(nestedsub)))
  209. return cleanup(&pkt);
  210. /* Sequential sub-packets with length prefixes */
  211. if (!TEST_true(WPACKET_init(&pkt, buf))
  212. || !TEST_true(WPACKET_start_sub_packet_u8(&pkt))
  213. || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
  214. || !TEST_true(WPACKET_close(&pkt))
  215. || !TEST_true(WPACKET_start_sub_packet_u8(&pkt))
  216. || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
  217. || !TEST_true(WPACKET_close(&pkt))
  218. || !TEST_true(WPACKET_finish(&pkt))
  219. || !TEST_true(WPACKET_get_total_written(&pkt, &written))
  220. || !TEST_mem_eq(buf->data, written, seqsub, sizeof(seqsub)))
  221. return cleanup(&pkt);
  222. /* Nested sub-packets with lengths filled before finish */
  223. if (!TEST_true(WPACKET_init(&pkt, buf))
  224. || !TEST_true(WPACKET_start_sub_packet_u8(&pkt))
  225. || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
  226. || !TEST_true(WPACKET_start_sub_packet_u8(&pkt))
  227. || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
  228. || !TEST_true(WPACKET_get_length(&pkt, &len))
  229. || !TEST_size_t_eq(len, 1)
  230. || !TEST_true(WPACKET_close(&pkt))
  231. || !TEST_true(WPACKET_get_length(&pkt, &len))
  232. || !TEST_size_t_eq(len, 3)
  233. || !TEST_true(WPACKET_close(&pkt))
  234. || !TEST_true(WPACKET_fill_lengths(&pkt))
  235. || !TEST_true(WPACKET_get_total_written(&pkt, &written))
  236. || !TEST_mem_eq(buf->data, written, nestedsub, sizeof(nestedsub))
  237. || !TEST_true(WPACKET_finish(&pkt)))
  238. return cleanup(&pkt);
  239. return 1;
  240. }
  241. static int test_WPACKET_set_flags(void)
  242. {
  243. WPACKET pkt;
  244. size_t written;
  245. /* Set packet to be non-zero length */
  246. if (!TEST_true(WPACKET_init(&pkt, buf))
  247. || !TEST_true(WPACKET_set_flags(&pkt, WPACKET_FLAGS_NON_ZERO_LENGTH))
  248. /* Should fail because of zero length */
  249. || !TEST_false(WPACKET_finish(&pkt))
  250. || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
  251. || !TEST_true(WPACKET_finish(&pkt))
  252. || !TEST_true(WPACKET_get_total_written(&pkt, &written))
  253. || !TEST_mem_eq(buf->data, written, simple1, sizeof(simple1)))
  254. return cleanup(&pkt);
  255. /* Repeat above test in a sub-packet */
  256. if (!TEST_true(WPACKET_init(&pkt, buf))
  257. || !TEST_true(WPACKET_start_sub_packet(&pkt))
  258. || !TEST_true(WPACKET_set_flags(&pkt, WPACKET_FLAGS_NON_ZERO_LENGTH))
  259. /* Should fail because of zero length */
  260. || !TEST_false(WPACKET_close(&pkt))
  261. || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
  262. || !TEST_true(WPACKET_close(&pkt))
  263. || !TEST_true(WPACKET_finish(&pkt))
  264. || !TEST_true(WPACKET_get_total_written(&pkt, &written))
  265. || !TEST_mem_eq(buf->data, written, simple1, sizeof(simple1)))
  266. return cleanup(&pkt);
  267. /* Set packet to abandon non-zero length */
  268. if (!TEST_true(WPACKET_init_len(&pkt, buf, 1))
  269. || !TEST_true(WPACKET_set_flags(&pkt, WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH))
  270. || !TEST_true(WPACKET_finish(&pkt))
  271. || !TEST_true(WPACKET_get_total_written(&pkt, &written))
  272. || !TEST_size_t_eq(written, 0))
  273. return cleanup(&pkt);
  274. /* Repeat above test but only abandon a sub-packet */
  275. if (!TEST_true(WPACKET_init_len(&pkt, buf, 1))
  276. || !TEST_true(WPACKET_start_sub_packet_u8(&pkt))
  277. || !TEST_true(WPACKET_set_flags(&pkt, WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH))
  278. || !TEST_true(WPACKET_close(&pkt))
  279. || !TEST_true(WPACKET_finish(&pkt))
  280. || !TEST_true(WPACKET_get_total_written(&pkt, &written))
  281. || !TEST_mem_eq(buf->data, written, empty, sizeof(empty)))
  282. return cleanup(&pkt);
  283. /* And repeat with a non empty sub-packet */
  284. if (!TEST_true(WPACKET_init(&pkt, buf))
  285. || !TEST_true(WPACKET_start_sub_packet_u8(&pkt))
  286. || !TEST_true(WPACKET_set_flags(&pkt, WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH))
  287. || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
  288. || !TEST_true(WPACKET_close(&pkt))
  289. || !TEST_true(WPACKET_finish(&pkt))
  290. || !TEST_true(WPACKET_get_total_written(&pkt, &written))
  291. || !TEST_mem_eq(buf->data, written, simple2, sizeof(simple2)))
  292. return cleanup(&pkt);
  293. return 1;
  294. }
  295. static int test_WPACKET_allocate_bytes(void)
  296. {
  297. WPACKET pkt;
  298. size_t written;
  299. unsigned char *bytes;
  300. if (!TEST_true(WPACKET_init_len(&pkt, buf, 1))
  301. || !TEST_true(WPACKET_allocate_bytes(&pkt, 2, &bytes)))
  302. return cleanup(&pkt);
  303. bytes[0] = 0xfe;
  304. bytes[1] = 0xff;
  305. if (!TEST_true(WPACKET_finish(&pkt))
  306. || !TEST_true(WPACKET_get_total_written(&pkt, &written))
  307. || !TEST_mem_eq(buf->data, written, alloc, sizeof(alloc)))
  308. return cleanup(&pkt);
  309. /* Repeat with WPACKET_sub_allocate_bytes */
  310. if (!TEST_true(WPACKET_init_len(&pkt, buf, 1))
  311. || !TEST_true(WPACKET_sub_allocate_bytes_u8(&pkt, 2, &bytes)))
  312. return cleanup(&pkt);
  313. bytes[0] = 0xfe;
  314. bytes[1] = 0xff;
  315. if (!TEST_true(WPACKET_finish(&pkt))
  316. || !TEST_true(WPACKET_get_total_written(&pkt, &written))
  317. || !TEST_mem_eq(buf->data, written, submem, sizeof(submem)))
  318. return cleanup(&pkt);
  319. return 1;
  320. }
  321. static int test_WPACKET_memcpy(void)
  322. {
  323. WPACKET pkt;
  324. size_t written;
  325. const unsigned char bytes[] = { 0xfe, 0xff };
  326. if (!TEST_true(WPACKET_init_len(&pkt, buf, 1))
  327. || !TEST_true(WPACKET_memcpy(&pkt, bytes, sizeof(bytes)))
  328. || !TEST_true(WPACKET_finish(&pkt))
  329. || !TEST_true(WPACKET_get_total_written(&pkt, &written))
  330. || !TEST_mem_eq(buf->data, written, alloc, sizeof(alloc)))
  331. return cleanup(&pkt);
  332. /* Repeat with WPACKET_sub_memcpy() */
  333. if (!TEST_true(WPACKET_init_len(&pkt, buf, 1))
  334. || !TEST_true(WPACKET_sub_memcpy_u8(&pkt, bytes, sizeof(bytes)))
  335. || !TEST_true(WPACKET_finish(&pkt))
  336. || !TEST_true(WPACKET_get_total_written(&pkt, &written))
  337. || !TEST_mem_eq(buf->data, written, submem, sizeof(submem)))
  338. return cleanup(&pkt);
  339. return 1;
  340. }
  341. static int test_WPACKET_init_der(void)
  342. {
  343. WPACKET pkt;
  344. unsigned char sbuf[1024];
  345. unsigned char testdata[] = { 0x00, 0x01, 0x02, 0x03 };
  346. unsigned char testdata2[259] = { 0x82, 0x01, 0x00 };
  347. size_t written[2];
  348. size_t size1, size2;
  349. int flags = WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH;
  350. int i;
  351. /* Test initialising for writing DER */
  352. if (!TEST_true(WPACKET_init_der(&pkt, sbuf, sizeof(sbuf)))
  353. || !TEST_true(WPACKET_put_bytes_u24(&pkt, 0xfffefd))
  354. /* Test writing data in a length prefixed sub-packet */
  355. || !TEST_true(WPACKET_start_sub_packet(&pkt))
  356. || !TEST_true(WPACKET_memcpy(&pkt, testdata, sizeof(testdata)))
  357. || !TEST_true(WPACKET_close(&pkt))
  358. || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xfc))
  359. /* this sub-packet is empty, and should render zero bytes */
  360. || (!TEST_true(WPACKET_start_sub_packet(&pkt))
  361. || !TEST_true(WPACKET_set_flags(&pkt, flags))
  362. || !TEST_true(WPACKET_get_total_written(&pkt, &size1))
  363. || !TEST_true(WPACKET_close(&pkt))
  364. || !TEST_true(WPACKET_get_total_written(&pkt, &size2))
  365. || !TEST_size_t_eq(size1, size2))
  366. || !TEST_true(WPACKET_finish(&pkt))
  367. || !TEST_true(WPACKET_get_total_written(&pkt, &written[0]))
  368. || !TEST_mem_eq(WPACKET_get_curr(&pkt), written[0], simpleder,
  369. sizeof(simpleder)))
  370. return cleanup(&pkt);
  371. /* Generate random packet data for test */
  372. if (!TEST_int_gt(RAND_bytes(&testdata2[3], sizeof(testdata2) - 3), 0))
  373. return 0;
  374. /*
  375. * Test with a sub-packet that has 2 length bytes. We do 2 passes - first
  376. * with a NULL buffer, just to calculate lengths, and a second pass with a
  377. * real buffer to actually generate a packet
  378. */
  379. for (i = 0; i < 2; i++) {
  380. if (i == 0) {
  381. if (!TEST_true(WPACKET_init_null_der(&pkt)))
  382. return 0;
  383. } else {
  384. if (!TEST_true(WPACKET_init_der(&pkt, sbuf, sizeof(sbuf))))
  385. return 0;
  386. }
  387. if (!TEST_true(WPACKET_start_sub_packet(&pkt))
  388. || !TEST_true(WPACKET_memcpy(&pkt, &testdata2[3],
  389. sizeof(testdata2) - 3))
  390. || !TEST_true(WPACKET_close(&pkt))
  391. || !TEST_true(WPACKET_finish(&pkt))
  392. || !TEST_true(WPACKET_get_total_written(&pkt, &written[i])))
  393. return cleanup(&pkt);
  394. }
  395. /*
  396. * Check that the size calculated in the first pass equals the size of the
  397. * packet actually generated in the second pass. Also check the generated
  398. * packet looks as we expect it to.
  399. */
  400. if (!TEST_size_t_eq(written[0], written[1])
  401. || !TEST_mem_eq(WPACKET_get_curr(&pkt), written[1], testdata2,
  402. sizeof(testdata2)))
  403. return 0;
  404. return 1;
  405. }
  406. static int test_WPACKET_quic(void)
  407. {
  408. WPACKET pkt;
  409. size_t written, len;
  410. unsigned char *bytes;
  411. /* QUIC sub-packet with 4-byte length prefix, containing a 1-byte vlint */
  412. if (!TEST_true(WPACKET_init(&pkt, buf))
  413. || !TEST_true(WPACKET_start_quic_sub_packet(&pkt))
  414. || !TEST_true(WPACKET_quic_write_vlint(&pkt, 0x09))
  415. /* Can't finish because we have a sub packet */
  416. || !TEST_false(WPACKET_finish(&pkt))
  417. || !TEST_true(WPACKET_close(&pkt))
  418. /* Sub packet is closed so can't close again */
  419. || !TEST_false(WPACKET_close(&pkt))
  420. /* Now a top level so finish should succeed */
  421. || !TEST_true(WPACKET_finish(&pkt))
  422. || !TEST_true(WPACKET_get_total_written(&pkt, &written))
  423. || !TEST_mem_eq(buf->data, written, quic1, sizeof(quic1)))
  424. return cleanup(&pkt);
  425. /* QUIC sub-packet with 1-byte length prefix, containing a 1-byte vlint */
  426. if (!TEST_true(WPACKET_init(&pkt, buf))
  427. || !TEST_true(WPACKET_start_quic_sub_packet_bound(&pkt, OSSL_QUIC_VLINT_1B_MAX))
  428. || !TEST_true(WPACKET_quic_write_vlint(&pkt, 0x09))
  429. || !TEST_false(WPACKET_finish(&pkt))
  430. || !TEST_true(WPACKET_close(&pkt))
  431. || !TEST_false(WPACKET_close(&pkt))
  432. || !TEST_true(WPACKET_finish(&pkt))
  433. || !TEST_true(WPACKET_get_total_written(&pkt, &written))
  434. || !TEST_mem_eq(buf->data, written, quic2, sizeof(quic2)))
  435. return cleanup(&pkt);
  436. /* QUIC sub-packet with 2-byte length prefix, containing a 2-byte vlint */
  437. if (!TEST_true(WPACKET_init(&pkt, buf))
  438. || !TEST_true(WPACKET_start_quic_sub_packet_bound(&pkt, OSSL_QUIC_VLINT_2B_MIN))
  439. || !TEST_true(WPACKET_quic_write_vlint(&pkt, 0x41))
  440. || !TEST_false(WPACKET_finish(&pkt))
  441. || !TEST_true(WPACKET_close(&pkt))
  442. || !TEST_false(WPACKET_close(&pkt))
  443. || !TEST_true(WPACKET_finish(&pkt))
  444. || !TEST_true(WPACKET_get_total_written(&pkt, &written))
  445. || !TEST_mem_eq(buf->data, written, quic3, sizeof(quic3)))
  446. return cleanup(&pkt);
  447. /* QUIC sub-packet with 8-byte length prefix, containing a 4-byte vlint */
  448. if (!TEST_true(WPACKET_init(&pkt, buf))
  449. || !TEST_true(WPACKET_start_quic_sub_packet_bound(&pkt, OSSL_QUIC_VLINT_8B_MIN))
  450. || !TEST_true(WPACKET_quic_write_vlint(&pkt, 0x13c6a))
  451. || !TEST_false(WPACKET_finish(&pkt))
  452. || !TEST_true(WPACKET_close(&pkt))
  453. || !TEST_false(WPACKET_close(&pkt))
  454. || !TEST_true(WPACKET_finish(&pkt))
  455. || !TEST_true(WPACKET_get_total_written(&pkt, &written))
  456. || !TEST_mem_eq(buf->data, written, quic4, sizeof(quic4)))
  457. return cleanup(&pkt);
  458. /* QUIC sub-packet with 8-byte length prefix, containing a 8-byte vlint */
  459. if (!TEST_true(WPACKET_init(&pkt, buf))
  460. || !TEST_true(WPACKET_start_quic_sub_packet_bound(&pkt, OSSL_QUIC_VLINT_8B_MIN))
  461. || !TEST_true(WPACKET_quic_write_vlint(&pkt, 0x2f77213f3f505ba5ULL))
  462. || !TEST_false(WPACKET_finish(&pkt))
  463. || !TEST_true(WPACKET_close(&pkt))
  464. || !TEST_false(WPACKET_close(&pkt))
  465. || !TEST_true(WPACKET_finish(&pkt))
  466. || !TEST_true(WPACKET_get_total_written(&pkt, &written))
  467. || !TEST_mem_eq(buf->data, written, quic5, sizeof(quic5)))
  468. return cleanup(&pkt);
  469. /* QUIC sub-packet, length known up-front */
  470. if (!TEST_true(WPACKET_init(&pkt, buf))
  471. || !TEST_true(WPACKET_quic_sub_allocate_bytes(&pkt, 3, &bytes)))
  472. return cleanup(&pkt);
  473. bytes[0] = 0x55;
  474. bytes[1] = 0x66;
  475. bytes[2] = 0x77;
  476. if (!TEST_true(WPACKET_finish(&pkt))
  477. || !TEST_true(WPACKET_get_total_written(&pkt, &written))
  478. || !TEST_mem_eq(buf->data, written, quic6, sizeof(quic6)))
  479. return cleanup(&pkt);
  480. /* Nested and sequential sub-packets with length prefixes */
  481. if (!TEST_true(WPACKET_init(&pkt, buf))
  482. || !TEST_true(WPACKET_quic_write_vlint(&pkt, 0x07))
  483. || !TEST_true(WPACKET_get_length(&pkt, &len))
  484. || !TEST_size_t_eq(len, 1)
  485. || !TEST_true(WPACKET_start_quic_sub_packet_bound(&pkt, OSSL_QUIC_VLINT_4B_MIN))
  486. || !TEST_true(WPACKET_quic_write_vlint(&pkt, 0x2514))
  487. || !TEST_true(WPACKET_get_length(&pkt, &len))
  488. || !TEST_size_t_eq(len, 2)
  489. || !TEST_true(WPACKET_start_quic_sub_packet_bound(&pkt, OSSL_QUIC_VLINT_2B_MIN))
  490. || !TEST_true(WPACKET_quic_write_vlint(&pkt, 0x05))
  491. || !TEST_true(WPACKET_get_length(&pkt, &len))
  492. || !TEST_size_t_eq(len, 1)
  493. || !TEST_true(WPACKET_close(&pkt))
  494. || !TEST_true(WPACKET_start_quic_sub_packet_bound(&pkt, OSSL_QUIC_VLINT_2B_MIN))
  495. || !TEST_true(WPACKET_quic_write_vlint(&pkt, 0x11))
  496. || !TEST_true(WPACKET_close(&pkt))
  497. || !TEST_true(WPACKET_get_length(&pkt, &len))
  498. || !TEST_size_t_eq(len, 8)
  499. || !TEST_true(WPACKET_close(&pkt))
  500. || !TEST_true(WPACKET_start_quic_sub_packet_bound(&pkt, OSSL_QUIC_VLINT_2B_MIN))
  501. || !TEST_true(WPACKET_quic_write_vlint(&pkt, 0x12))
  502. || !TEST_true(WPACKET_close(&pkt))
  503. || !TEST_true(WPACKET_start_quic_sub_packet_bound(&pkt, OSSL_QUIC_VLINT_2B_MIN))
  504. || !TEST_true(WPACKET_quic_write_vlint(&pkt, 0x13))
  505. || !TEST_true(WPACKET_close(&pkt))
  506. || !TEST_true(WPACKET_finish(&pkt))
  507. || !TEST_true(WPACKET_get_total_written(&pkt, &written))
  508. || !TEST_mem_eq(buf->data, written, quic7, sizeof(quic7)))
  509. return cleanup(&pkt);
  510. /* Trying to encode a value above OSSL_QUIC_VLINT_MAX should fail */
  511. if (!TEST_true(WPACKET_init(&pkt, buf))
  512. || !TEST_false(WPACKET_quic_write_vlint(&pkt, OSSL_QUIC_VLINT_MAX+1))
  513. || !TEST_true(WPACKET_quic_write_vlint(&pkt, OSSL_QUIC_VLINT_MAX)))
  514. return cleanup(&pkt);
  515. WPACKET_cleanup(&pkt);
  516. return 1;
  517. }
  518. static int test_WPACKET_quic_vlint_random(void)
  519. {
  520. size_t i, written;
  521. uint64_t expected, actual = 0;
  522. unsigned char rand_data[9];
  523. WPACKET pkt;
  524. PACKET read_pkt = {0};
  525. for (i = 0; i < 10000; ++i) {
  526. if (!TEST_int_gt(RAND_bytes(rand_data, sizeof(rand_data)), 0))
  527. return cleanup(&pkt);
  528. expected = *(uint64_t*)rand_data;
  529. /*
  530. * Ensure that all size classes get tested with equal probability.
  531. */
  532. switch (rand_data[8] & 3) {
  533. case 0:
  534. expected &= OSSL_QUIC_VLINT_1B_MAX;
  535. break;
  536. case 1:
  537. expected &= OSSL_QUIC_VLINT_2B_MAX;
  538. break;
  539. case 2:
  540. expected &= OSSL_QUIC_VLINT_4B_MAX;
  541. break;
  542. case 3:
  543. expected &= OSSL_QUIC_VLINT_8B_MAX;
  544. break;
  545. }
  546. if (!TEST_true(WPACKET_init(&pkt, buf))
  547. || !TEST_true(WPACKET_quic_write_vlint(&pkt, expected))
  548. || !TEST_true(WPACKET_get_total_written(&pkt, &written)))
  549. return cleanup(&pkt);
  550. if (!TEST_true(PACKET_buf_init(&read_pkt, (unsigned char *)buf->data, written))
  551. || !TEST_true(PACKET_get_quic_vlint(&read_pkt, &actual))
  552. || !TEST_uint64_t_eq(expected, actual))
  553. return cleanup(&pkt);
  554. WPACKET_cleanup(&pkt);
  555. }
  556. WPACKET_cleanup(&pkt);
  557. return 1;
  558. }
  559. int setup_tests(void)
  560. {
  561. if (!TEST_ptr(buf = BUF_MEM_new()))
  562. return 0;
  563. ADD_TEST(test_WPACKET_init);
  564. ADD_TEST(test_WPACKET_set_max_size);
  565. ADD_TEST(test_WPACKET_start_sub_packet);
  566. ADD_TEST(test_WPACKET_set_flags);
  567. ADD_TEST(test_WPACKET_allocate_bytes);
  568. ADD_TEST(test_WPACKET_memcpy);
  569. ADD_TEST(test_WPACKET_init_der);
  570. ADD_TEST(test_WPACKET_quic);
  571. ADD_TEST(test_WPACKET_quic_vlint_random);
  572. return 1;
  573. }
  574. void cleanup_tests(void)
  575. {
  576. BUF_MEM_free(buf);
  577. }