wpackettest.c 15 KB

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