wpackettest.c 26 KB

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