2
0

wpackettest.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*
  2. * Copyright 2016-2017 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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 "../ssl/packet_locl.h"
  12. #include "testutil.h"
  13. static const unsigned char simple1[] = { 0xff };
  14. static const unsigned char simple2[] = { 0x01, 0xff };
  15. static const unsigned char simple3[] = { 0x00, 0x00, 0x00, 0x01, 0xff };
  16. static const unsigned char nestedsub[] = { 0x03, 0xff, 0x01, 0xff };
  17. static const unsigned char seqsub[] = { 0x01, 0xff, 0x01, 0xff };
  18. static const unsigned char empty[] = { 0x00 };
  19. static const unsigned char alloc[] = { 0x02, 0xfe, 0xff };
  20. static const unsigned char submem[] = { 0x03, 0x02, 0xfe, 0xff };
  21. static const unsigned char fixed[] = { 0xff, 0xff, 0xff };
  22. static BUF_MEM *buf;
  23. static int cleanup(WPACKET *pkt)
  24. {
  25. WPACKET_cleanup(pkt);
  26. return 0;
  27. }
  28. static int test_WPACKET_init(void)
  29. {
  30. WPACKET pkt;
  31. int i;
  32. size_t written;
  33. unsigned char sbuf[3];
  34. if (!TEST_true(WPACKET_init(&pkt, buf))
  35. || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
  36. /* Closing a top level WPACKET should fail */
  37. || !TEST_false(WPACKET_close(&pkt))
  38. /* Finishing a top level WPACKET should succeed */
  39. || !TEST_true(WPACKET_finish(&pkt))
  40. /*
  41. * Can't call close or finish on a WPACKET that's already
  42. * finished.
  43. */
  44. || !TEST_false(WPACKET_close(&pkt))
  45. || !TEST_false(WPACKET_finish(&pkt))
  46. || !TEST_true(WPACKET_get_total_written(&pkt, &written))
  47. || !TEST_mem_eq(buf->data, written, simple1, sizeof(simple1)))
  48. return cleanup(&pkt);
  49. /* Now try with a one byte length prefix */
  50. if (!TEST_true(WPACKET_init_len(&pkt, buf, 1))
  51. || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
  52. || !TEST_true(WPACKET_finish(&pkt))
  53. || !TEST_true(WPACKET_get_total_written(&pkt, &written))
  54. || !TEST_mem_eq(buf->data, written, simple2, sizeof(simple2)))
  55. return cleanup(&pkt);
  56. /* And a longer length prefix */
  57. if (!TEST_true(WPACKET_init_len(&pkt, buf, 4))
  58. || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
  59. || !TEST_true(WPACKET_finish(&pkt))
  60. || !TEST_true(WPACKET_get_total_written(&pkt, &written))
  61. || !TEST_mem_eq(buf->data, written, simple3, sizeof(simple3)))
  62. return cleanup(&pkt);
  63. if (!TEST_true(WPACKET_init_len(&pkt, buf, 1)))
  64. return cleanup(&pkt);
  65. for (i = 1; i < 257; i++) {
  66. /*
  67. * Putting more bytes in than fit for the size of the length prefix
  68. * should fail
  69. */
  70. if (!TEST_int_eq(WPACKET_put_bytes_u8(&pkt, 0xff), i < 256))
  71. return cleanup(&pkt);
  72. }
  73. if (!TEST_true(WPACKET_finish(&pkt)))
  74. return cleanup(&pkt);
  75. /* Test initialising from a fixed size buffer */
  76. if (!TEST_true(WPACKET_init_static_len(&pkt, sbuf, sizeof(sbuf), 0))
  77. /* Adding 3 bytes should succeed */
  78. || !TEST_true(WPACKET_put_bytes_u24(&pkt, 0xffffff))
  79. /* Adding 1 more byte should fail */
  80. || !TEST_false(WPACKET_put_bytes_u8(&pkt, 0xff))
  81. /* Finishing the top level WPACKET should succeed */
  82. || !TEST_true(WPACKET_finish(&pkt))
  83. || !TEST_true(WPACKET_get_total_written(&pkt, &written))
  84. || !TEST_mem_eq(sbuf, written, fixed, sizeof(sbuf))
  85. /* Initialise with 1 len byte */
  86. || !TEST_true(WPACKET_init_static_len(&pkt, sbuf, sizeof(sbuf), 1))
  87. /* Adding 2 bytes should succeed */
  88. || !TEST_true(WPACKET_put_bytes_u16(&pkt, 0xfeff))
  89. /* Adding 1 more byte should fail */
  90. || !TEST_false(WPACKET_put_bytes_u8(&pkt, 0xff))
  91. || !TEST_true(WPACKET_finish(&pkt))
  92. || !TEST_true(WPACKET_get_total_written(&pkt, &written))
  93. || !TEST_mem_eq(sbuf, written, alloc, sizeof(alloc)))
  94. return cleanup(&pkt);
  95. return 1;
  96. }
  97. static int test_WPACKET_set_max_size(void)
  98. {
  99. WPACKET pkt;
  100. size_t written;
  101. if (!TEST_true(WPACKET_init(&pkt, buf))
  102. /*
  103. * No previous lenbytes set so we should be ok to set the max
  104. * possible max size
  105. */
  106. || !TEST_true(WPACKET_set_max_size(&pkt, SIZE_MAX))
  107. /* We should be able to set it smaller too */
  108. || !TEST_true(WPACKET_set_max_size(&pkt, SIZE_MAX -1))
  109. /* And setting it bigger again should be ok */
  110. || !TEST_true(WPACKET_set_max_size(&pkt, SIZE_MAX))
  111. || !TEST_true(WPACKET_finish(&pkt)))
  112. return cleanup(&pkt);
  113. if (!TEST_true(WPACKET_init_len(&pkt, buf, 1))
  114. /*
  115. * Should fail because we already consumed 1 byte with the
  116. * length
  117. */
  118. || !TEST_false(WPACKET_set_max_size(&pkt, 0))
  119. /*
  120. * Max size can't be bigger than biggest that will fit in
  121. * lenbytes
  122. */
  123. || !TEST_false(WPACKET_set_max_size(&pkt, 0x0101))
  124. /* It can be the same as the maximum possible size */
  125. || !TEST_true(WPACKET_set_max_size(&pkt, 0x0100))
  126. /* Or it can be less */
  127. || !TEST_true(WPACKET_set_max_size(&pkt, 0x01))
  128. /* Should fail because packet is already filled */
  129. || !TEST_false(WPACKET_put_bytes_u8(&pkt, 0xff))
  130. /* You can't put in more bytes than max size */
  131. || !TEST_true(WPACKET_set_max_size(&pkt, 0x02))
  132. || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
  133. || !TEST_false(WPACKET_put_bytes_u8(&pkt, 0xff))
  134. || !TEST_true(WPACKET_finish(&pkt))
  135. || !TEST_true(WPACKET_get_total_written(&pkt, &written))
  136. || !TEST_mem_eq(buf->data, written, simple2, sizeof(simple2)))
  137. return cleanup(&pkt);
  138. return 1;
  139. }
  140. static int test_WPACKET_start_sub_packet(void)
  141. {
  142. WPACKET pkt;
  143. size_t written;
  144. size_t len;
  145. if (!TEST_true(WPACKET_init(&pkt, buf))
  146. || !TEST_true(WPACKET_start_sub_packet(&pkt))
  147. || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
  148. /* Can't finish because we have a sub packet */
  149. || !TEST_false(WPACKET_finish(&pkt))
  150. || !TEST_true(WPACKET_close(&pkt))
  151. /* Sub packet is closed so can't close again */
  152. || !TEST_false(WPACKET_close(&pkt))
  153. /* Now a top level so finish should succeed */
  154. || !TEST_true(WPACKET_finish(&pkt))
  155. || !TEST_true(WPACKET_get_total_written(&pkt, &written))
  156. || !TEST_mem_eq(buf->data, written, simple1, sizeof(simple1)))
  157. return cleanup(&pkt);
  158. /* Single sub-packet with length prefix */
  159. if (!TEST_true(WPACKET_init(&pkt, buf))
  160. || !TEST_true(WPACKET_start_sub_packet_u8(&pkt))
  161. || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
  162. || !TEST_true(WPACKET_close(&pkt))
  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. /* Nested sub-packets with length prefixes */
  168. if (!TEST_true(WPACKET_init(&pkt, buf))
  169. || !TEST_true(WPACKET_start_sub_packet_u8(&pkt))
  170. || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
  171. || !TEST_true(WPACKET_start_sub_packet_u8(&pkt))
  172. || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
  173. || !TEST_true(WPACKET_get_length(&pkt, &len))
  174. || !TEST_size_t_eq(len, 1)
  175. || !TEST_true(WPACKET_close(&pkt))
  176. || !TEST_true(WPACKET_get_length(&pkt, &len))
  177. || !TEST_size_t_eq(len, 3)
  178. || !TEST_true(WPACKET_close(&pkt))
  179. || !TEST_true(WPACKET_finish(&pkt))
  180. || !TEST_true(WPACKET_get_total_written(&pkt, &written))
  181. || !TEST_mem_eq(buf->data, written, nestedsub, sizeof(nestedsub)))
  182. return cleanup(&pkt);
  183. /* Sequential sub-packets with length prefixes */
  184. if (!TEST_true(WPACKET_init(&pkt, buf))
  185. || !TEST_true(WPACKET_start_sub_packet_u8(&pkt))
  186. || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
  187. || !TEST_true(WPACKET_close(&pkt))
  188. || !TEST_true(WPACKET_start_sub_packet_u8(&pkt))
  189. || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
  190. || !TEST_true(WPACKET_close(&pkt))
  191. || !TEST_true(WPACKET_finish(&pkt))
  192. || !TEST_true(WPACKET_get_total_written(&pkt, &written))
  193. || !TEST_mem_eq(buf->data, written, seqsub, sizeof(seqsub)))
  194. return cleanup(&pkt);
  195. /* Nested sub-packets with lengths filled before finish */
  196. if (!TEST_true(WPACKET_init(&pkt, buf))
  197. || !TEST_true(WPACKET_start_sub_packet_u8(&pkt))
  198. || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
  199. || !TEST_true(WPACKET_start_sub_packet_u8(&pkt))
  200. || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
  201. || !TEST_true(WPACKET_get_length(&pkt, &len))
  202. || !TEST_size_t_eq(len, 1)
  203. || !TEST_true(WPACKET_close(&pkt))
  204. || !TEST_true(WPACKET_get_length(&pkt, &len))
  205. || !TEST_size_t_eq(len, 3)
  206. || !TEST_true(WPACKET_close(&pkt))
  207. || !TEST_true(WPACKET_fill_lengths(&pkt))
  208. || !TEST_true(WPACKET_get_total_written(&pkt, &written))
  209. || !TEST_mem_eq(buf->data, written, nestedsub, sizeof(nestedsub))
  210. || !TEST_true(WPACKET_finish(&pkt)))
  211. return cleanup(&pkt);
  212. return 1;
  213. }
  214. static int test_WPACKET_set_flags(void)
  215. {
  216. WPACKET pkt;
  217. size_t written;
  218. /* Set packet to be non-zero length */
  219. if (!TEST_true(WPACKET_init(&pkt, buf))
  220. || !TEST_true(WPACKET_set_flags(&pkt, WPACKET_FLAGS_NON_ZERO_LENGTH))
  221. /* Should fail because of zero length */
  222. || !TEST_false(WPACKET_finish(&pkt))
  223. || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
  224. || !TEST_true(WPACKET_finish(&pkt))
  225. || !TEST_true(WPACKET_get_total_written(&pkt, &written))
  226. || !TEST_mem_eq(buf->data, written, simple1, sizeof(simple1)))
  227. return cleanup(&pkt);
  228. /* Repeat above test in a sub-packet */
  229. if (!TEST_true(WPACKET_init(&pkt, buf))
  230. || !TEST_true(WPACKET_start_sub_packet(&pkt))
  231. || !TEST_true(WPACKET_set_flags(&pkt, WPACKET_FLAGS_NON_ZERO_LENGTH))
  232. /* Should fail because of zero length */
  233. || !TEST_false(WPACKET_close(&pkt))
  234. || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
  235. || !TEST_true(WPACKET_close(&pkt))
  236. || !TEST_true(WPACKET_finish(&pkt))
  237. || !TEST_true(WPACKET_get_total_written(&pkt, &written))
  238. || !TEST_mem_eq(buf->data, written, simple1, sizeof(simple1)))
  239. return cleanup(&pkt);
  240. /* Set packet to abandon non-zero length */
  241. if (!TEST_true(WPACKET_init_len(&pkt, buf, 1))
  242. || !TEST_true(WPACKET_set_flags(&pkt, WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH))
  243. || !TEST_true(WPACKET_finish(&pkt))
  244. || !TEST_true(WPACKET_get_total_written(&pkt, &written))
  245. || !TEST_size_t_eq(written, 0))
  246. return cleanup(&pkt);
  247. /* Repeat above test but only abandon a sub-packet */
  248. if (!TEST_true(WPACKET_init_len(&pkt, buf, 1))
  249. || !TEST_true(WPACKET_start_sub_packet_u8(&pkt))
  250. || !TEST_true(WPACKET_set_flags(&pkt, WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH))
  251. || !TEST_true(WPACKET_close(&pkt))
  252. || !TEST_true(WPACKET_finish(&pkt))
  253. || !TEST_true(WPACKET_get_total_written(&pkt, &written))
  254. || !TEST_mem_eq(buf->data, written, empty, sizeof(empty)))
  255. return cleanup(&pkt);
  256. /* And repeat with a non empty sub-packet */
  257. if (!TEST_true(WPACKET_init(&pkt, buf))
  258. || !TEST_true(WPACKET_start_sub_packet_u8(&pkt))
  259. || !TEST_true(WPACKET_set_flags(&pkt, WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH))
  260. || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
  261. || !TEST_true(WPACKET_close(&pkt))
  262. || !TEST_true(WPACKET_finish(&pkt))
  263. || !TEST_true(WPACKET_get_total_written(&pkt, &written))
  264. || !TEST_mem_eq(buf->data, written, simple2, sizeof(simple2)))
  265. return cleanup(&pkt);
  266. return 1;
  267. }
  268. static int test_WPACKET_allocate_bytes(void)
  269. {
  270. WPACKET pkt;
  271. size_t written;
  272. unsigned char *bytes;
  273. if (!TEST_true(WPACKET_init_len(&pkt, buf, 1))
  274. || !TEST_true(WPACKET_allocate_bytes(&pkt, 2, &bytes)))
  275. return cleanup(&pkt);
  276. bytes[0] = 0xfe;
  277. bytes[1] = 0xff;
  278. if (!TEST_true(WPACKET_finish(&pkt))
  279. || !TEST_true(WPACKET_get_total_written(&pkt, &written))
  280. || !TEST_mem_eq(buf->data, written, alloc, sizeof(alloc)))
  281. return cleanup(&pkt);
  282. /* Repeat with WPACKET_sub_allocate_bytes */
  283. if (!TEST_true(WPACKET_init_len(&pkt, buf, 1))
  284. || !TEST_true(WPACKET_sub_allocate_bytes_u8(&pkt, 2, &bytes)))
  285. return cleanup(&pkt);
  286. bytes[0] = 0xfe;
  287. bytes[1] = 0xff;
  288. if (!TEST_true(WPACKET_finish(&pkt))
  289. || !TEST_true(WPACKET_get_total_written(&pkt, &written))
  290. || !TEST_mem_eq(buf->data, written, submem, sizeof(submem)))
  291. return cleanup(&pkt);
  292. return 1;
  293. }
  294. static int test_WPACKET_memcpy(void)
  295. {
  296. WPACKET pkt;
  297. size_t written;
  298. const unsigned char bytes[] = { 0xfe, 0xff };
  299. if (!TEST_true(WPACKET_init_len(&pkt, buf, 1))
  300. || !TEST_true(WPACKET_memcpy(&pkt, bytes, sizeof(bytes)))
  301. || !TEST_true(WPACKET_finish(&pkt))
  302. || !TEST_true(WPACKET_get_total_written(&pkt, &written))
  303. || !TEST_mem_eq(buf->data, written, alloc, sizeof(alloc)))
  304. return cleanup(&pkt);
  305. /* Repeat with WPACKET_sub_memcpy() */
  306. if (!TEST_true(WPACKET_init_len(&pkt, buf, 1))
  307. || !TEST_true(WPACKET_sub_memcpy_u8(&pkt, bytes, sizeof(bytes)))
  308. || !TEST_true(WPACKET_finish(&pkt))
  309. || !TEST_true(WPACKET_get_total_written(&pkt, &written))
  310. || !TEST_mem_eq(buf->data, written, submem, sizeof(submem)))
  311. return cleanup(&pkt);
  312. return 1;
  313. }
  314. int setup_tests(void)
  315. {
  316. if (!TEST_ptr(buf = BUF_MEM_new()))
  317. return 0;
  318. ADD_TEST(test_WPACKET_init);
  319. ADD_TEST(test_WPACKET_set_max_size);
  320. ADD_TEST(test_WPACKET_start_sub_packet);
  321. ADD_TEST(test_WPACKET_set_flags);
  322. ADD_TEST(test_WPACKET_allocate_bytes);
  323. ADD_TEST(test_WPACKET_memcpy);
  324. return 1;
  325. }
  326. void cleanup_tests(void)
  327. {
  328. BUF_MEM_free(buf);
  329. }