heartbeat_test.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*-
  2. * Unit test for TLS heartbeats.
  3. *
  4. * Acts as a regression test against the Heartbleed bug (CVE-2014-0160).
  5. *
  6. * Author: Mike Bland (mbland@acm.org, http://mike-bland.com/)
  7. * Date: 2014-04-12
  8. * License: Creative Commons Attribution 4.0 International (CC By 4.0)
  9. * http://creativecommons.org/licenses/by/4.0/deed.en_US
  10. *
  11. * OUTPUT
  12. * ------
  13. * The program returns zero on success. It will print a message with a count
  14. * of the number of failed tests and return nonzero if any tests fail.
  15. *
  16. * It will print the contents of the request and response buffers for each
  17. * failing test. In a "fixed" version, all the tests should pass and there
  18. * should be no output.
  19. *
  20. * In a "bleeding" version, you'll see:
  21. *
  22. * test_dtls1_heartbleed failed:
  23. * expected payload len: 0
  24. * received: 1024
  25. * sent 26 characters
  26. * "HEARTBLEED "
  27. * received 1024 characters
  28. * "HEARTBLEED \xde\xad\xbe\xef..."
  29. * ** test_dtls1_heartbleed failed **
  30. *
  31. * The contents of the returned buffer in the failing test will depend on the
  32. * contents of memory on your machine.
  33. *
  34. * MORE INFORMATION
  35. * ----------------
  36. * http://mike-bland.com/2014/04/12/heartbleed.html
  37. * http://mike-bland.com/tags/heartbleed.html
  38. */
  39. #define OPENSSL_UNIT_TEST
  40. #include "../ssl/ssl_locl.h"
  41. #include "testutil.h"
  42. #include <ctype.h>
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #include <string.h>
  46. #if !defined(OPENSSL_NO_HEARTBEATS) && !defined(OPENSSL_NO_UNIT_TEST)
  47. /* As per https://tools.ietf.org/html/rfc6520#section-4 */
  48. # define MIN_PADDING_SIZE 16
  49. /* Maximum number of payload characters to print as test output */
  50. # define MAX_PRINTABLE_CHARACTERS 1024
  51. typedef struct heartbeat_test_fixture {
  52. SSL_CTX *ctx;
  53. SSL *s;
  54. const char *test_case_name;
  55. int (*process_heartbeat) (SSL *s, unsigned char *p, unsigned int length);
  56. unsigned char *payload;
  57. int sent_payload_len;
  58. int expected_return_value;
  59. int return_payload_offset;
  60. int expected_payload_len;
  61. const char *expected_return_payload;
  62. } HEARTBEAT_TEST_FIXTURE;
  63. static HEARTBEAT_TEST_FIXTURE set_up(const char *const test_case_name,
  64. const SSL_METHOD *meth)
  65. {
  66. HEARTBEAT_TEST_FIXTURE fixture;
  67. int setup_ok = 1;
  68. memset(&fixture, 0, sizeof(fixture));
  69. fixture.test_case_name = test_case_name;
  70. fixture.ctx = SSL_CTX_new(meth);
  71. if (!fixture.ctx) {
  72. fprintf(stderr, "Failed to allocate SSL_CTX for test: %s\n",
  73. test_case_name);
  74. setup_ok = 0;
  75. goto fail;
  76. }
  77. fixture.s = SSL_new(fixture.ctx);
  78. if (!fixture.s) {
  79. fprintf(stderr, "Failed to allocate SSL for test: %s\n",
  80. test_case_name);
  81. setup_ok = 0;
  82. goto fail;
  83. }
  84. if (!ssl_init_wbio_buffer(fixture.s, 1)) {
  85. fprintf(stderr, "Failed to set up wbio buffer for test: %s\n",
  86. test_case_name);
  87. setup_ok = 0;
  88. goto fail;
  89. }
  90. if (!ssl3_setup_buffers(fixture.s)) {
  91. fprintf(stderr, "Failed to setup buffers for test: %s\n",
  92. test_case_name);
  93. setup_ok = 0;
  94. goto fail;
  95. }
  96. /*
  97. * Clear the memory for the return buffer, since this isn't automatically
  98. * zeroed in opt mode and will cause spurious test failures that will
  99. * change with each execution.
  100. */
  101. memset(fixture.s->rlayer.wbuf.buf, 0, fixture.s->rlayer.wbuf.len);
  102. fail:
  103. if (!setup_ok) {
  104. ERR_print_errors_fp(stderr);
  105. exit(EXIT_FAILURE);
  106. }
  107. return fixture;
  108. }
  109. static HEARTBEAT_TEST_FIXTURE set_up_dtls(const char *const test_case_name)
  110. {
  111. HEARTBEAT_TEST_FIXTURE fixture = set_up(test_case_name,
  112. DTLSv1_server_method());
  113. fixture.process_heartbeat = dtls1_process_heartbeat;
  114. /*
  115. * As per dtls1_get_record(), skipping the following from the beginning
  116. * of the returned heartbeat message: type-1 byte; version-2 bytes;
  117. * sequence number-8 bytes; length-2 bytes And then skipping the 1-byte
  118. * type encoded by process_heartbeat for a total of 14 bytes, at which
  119. * point we can grab the length and the payload we seek.
  120. */
  121. fixture.return_payload_offset = 14;
  122. return fixture;
  123. }
  124. /* Needed by ssl3_write_bytes() */
  125. static int dummy_handshake(SSL *s)
  126. {
  127. return 1;
  128. }
  129. static void tear_down(HEARTBEAT_TEST_FIXTURE fixture)
  130. {
  131. ERR_print_errors_fp(stderr);
  132. SSL_free(fixture.s);
  133. SSL_CTX_free(fixture.ctx);
  134. }
  135. static void print_payload(const char *const prefix,
  136. const unsigned char *payload, const int n)
  137. {
  138. const int end = n < MAX_PRINTABLE_CHARACTERS ? n
  139. : MAX_PRINTABLE_CHARACTERS;
  140. int i = 0;
  141. printf("%s %d character%s", prefix, n, n == 1 ? "" : "s");
  142. if (end != n)
  143. printf(" (first %d shown)", end);
  144. printf("\n \"");
  145. for (; i != end; ++i) {
  146. const unsigned char c = payload[i];
  147. if (isprint(c))
  148. fputc(c, stdout);
  149. else
  150. printf("\\x%02x", c);
  151. }
  152. printf("\"\n");
  153. }
  154. static int execute_heartbeat(HEARTBEAT_TEST_FIXTURE fixture)
  155. {
  156. int result = 0;
  157. SSL *s = fixture.s;
  158. unsigned char *payload = fixture.payload;
  159. unsigned char sent_buf[MAX_PRINTABLE_CHARACTERS + 1];
  160. int return_value;
  161. unsigned const char *p;
  162. int actual_payload_len;
  163. s->rlayer.rrec.data = payload;
  164. s->rlayer.rrec.length = strlen((const char *)payload);
  165. *payload++ = TLS1_HB_REQUEST;
  166. s2n(fixture.sent_payload_len, payload);
  167. /*
  168. * Make a local copy of the request, since it gets overwritten at some
  169. * point
  170. */
  171. memcpy(sent_buf, payload, sizeof(sent_buf));
  172. return_value = fixture.process_heartbeat(s, s->rlayer.rrec.data,
  173. s->rlayer.rrec.length);
  174. if (return_value != fixture.expected_return_value) {
  175. printf("%s failed: expected return value %d, received %d\n",
  176. fixture.test_case_name, fixture.expected_return_value,
  177. return_value);
  178. result = 1;
  179. }
  180. /*
  181. * If there is any byte alignment, it will be stored in wbuf.offset.
  182. */
  183. p = &(s->rlayer.
  184. wbuf.buf[fixture.return_payload_offset + s->rlayer.wbuf.offset]);
  185. actual_payload_len = 0;
  186. n2s(p, actual_payload_len);
  187. if (actual_payload_len != fixture.expected_payload_len) {
  188. printf("%s failed:\n expected payload len: %d\n received: %d\n",
  189. fixture.test_case_name, fixture.expected_payload_len,
  190. actual_payload_len);
  191. print_payload("sent", sent_buf, strlen((const char *)sent_buf));
  192. print_payload("received", p, actual_payload_len);
  193. result = 1;
  194. } else {
  195. char *actual_payload =
  196. OPENSSL_strndup((const char *)p, actual_payload_len);
  197. if (strcmp(actual_payload, fixture.expected_return_payload) != 0) {
  198. printf
  199. ("%s failed:\n expected payload: \"%s\"\n received: \"%s\"\n",
  200. fixture.test_case_name, fixture.expected_return_payload,
  201. actual_payload);
  202. result = 1;
  203. }
  204. OPENSSL_free(actual_payload);
  205. }
  206. if (result != 0) {
  207. printf("** %s failed **\n--------\n", fixture.test_case_name);
  208. }
  209. return result;
  210. }
  211. static int honest_payload_size(unsigned char payload_buf[])
  212. {
  213. /* Omit three-byte pad at the beginning for type and payload length */
  214. return strlen((const char *)&payload_buf[3]) - MIN_PADDING_SIZE;
  215. }
  216. # define SETUP_HEARTBEAT_TEST_FIXTURE(type)\
  217. SETUP_TEST_FIXTURE(HEARTBEAT_TEST_FIXTURE, set_up_##type)
  218. # define EXECUTE_HEARTBEAT_TEST()\
  219. EXECUTE_TEST(execute_heartbeat, tear_down)
  220. static int test_dtls1_not_bleeding()
  221. {
  222. SETUP_HEARTBEAT_TEST_FIXTURE(dtls);
  223. /* Three-byte pad at the beginning for type and payload length */
  224. unsigned char payload_buf[MAX_PRINTABLE_CHARACTERS + 4] =
  225. " Not bleeding, sixteen spaces of padding" " ";
  226. const int payload_buf_len = honest_payload_size(payload_buf);
  227. fixture.payload = &payload_buf[0];
  228. fixture.sent_payload_len = payload_buf_len;
  229. fixture.expected_return_value = 0;
  230. fixture.expected_payload_len = payload_buf_len;
  231. fixture.expected_return_payload =
  232. "Not bleeding, sixteen spaces of padding";
  233. EXECUTE_HEARTBEAT_TEST();
  234. }
  235. static int test_dtls1_not_bleeding_empty_payload()
  236. {
  237. int payload_buf_len;
  238. SETUP_HEARTBEAT_TEST_FIXTURE(dtls);
  239. /*
  240. * Three-byte pad at the beginning for type and payload length, plus a
  241. * NUL at the end
  242. */
  243. unsigned char payload_buf[4 + MAX_PRINTABLE_CHARACTERS];
  244. memset(payload_buf, ' ', MIN_PADDING_SIZE + 3);
  245. payload_buf[MIN_PADDING_SIZE + 3] = '\0';
  246. payload_buf_len = honest_payload_size(payload_buf);
  247. fixture.payload = &payload_buf[0];
  248. fixture.sent_payload_len = payload_buf_len;
  249. fixture.expected_return_value = 0;
  250. fixture.expected_payload_len = payload_buf_len;
  251. fixture.expected_return_payload = "";
  252. EXECUTE_HEARTBEAT_TEST();
  253. }
  254. static int test_dtls1_heartbleed()
  255. {
  256. SETUP_HEARTBEAT_TEST_FIXTURE(dtls);
  257. /* Three-byte pad at the beginning for type and payload length */
  258. unsigned char payload_buf[4 + MAX_PRINTABLE_CHARACTERS] =
  259. " HEARTBLEED ";
  260. fixture.payload = &payload_buf[0];
  261. fixture.sent_payload_len = MAX_PRINTABLE_CHARACTERS;
  262. fixture.expected_return_value = 0;
  263. fixture.expected_payload_len = 0;
  264. fixture.expected_return_payload = "";
  265. EXECUTE_HEARTBEAT_TEST();
  266. }
  267. static int test_dtls1_heartbleed_empty_payload()
  268. {
  269. SETUP_HEARTBEAT_TEST_FIXTURE(dtls);
  270. /*
  271. * Excluding the NUL at the end, one byte short of type + payload length
  272. * + minimum padding
  273. */
  274. unsigned char payload_buf[MAX_PRINTABLE_CHARACTERS + 4];
  275. memset(payload_buf, ' ', MIN_PADDING_SIZE + 2);
  276. payload_buf[MIN_PADDING_SIZE + 2] = '\0';
  277. fixture.payload = &payload_buf[0];
  278. fixture.sent_payload_len = MAX_PRINTABLE_CHARACTERS;
  279. fixture.expected_return_value = 0;
  280. fixture.expected_payload_len = 0;
  281. fixture.expected_return_payload = "";
  282. EXECUTE_HEARTBEAT_TEST();
  283. }
  284. static int test_dtls1_heartbleed_excessive_plaintext_length()
  285. {
  286. SETUP_HEARTBEAT_TEST_FIXTURE(dtls);
  287. /*
  288. * Excluding the NUL at the end, one byte in excess of maximum allowed
  289. * heartbeat message length
  290. */
  291. unsigned char payload_buf[SSL3_RT_MAX_PLAIN_LENGTH + 2];
  292. memset(payload_buf, ' ', sizeof(payload_buf));
  293. payload_buf[sizeof(payload_buf) - 1] = '\0';
  294. fixture.payload = &payload_buf[0];
  295. fixture.sent_payload_len = honest_payload_size(payload_buf);
  296. fixture.expected_return_value = 0;
  297. fixture.expected_payload_len = 0;
  298. fixture.expected_return_payload = "";
  299. EXECUTE_HEARTBEAT_TEST();
  300. }
  301. # undef EXECUTE_HEARTBEAT_TEST
  302. # undef SETUP_HEARTBEAT_TEST_FIXTURE
  303. int main(int argc, char *argv[])
  304. {
  305. int result = 0;
  306. ADD_TEST(test_dtls1_not_bleeding);
  307. ADD_TEST(test_dtls1_not_bleeding_empty_payload);
  308. ADD_TEST(test_dtls1_heartbleed);
  309. ADD_TEST(test_dtls1_heartbleed_empty_payload);
  310. ADD_TEST(test_dtls1_heartbleed_excessive_plaintext_length);
  311. result = run_tests(argv[0]);
  312. ERR_print_errors_fp(stderr);
  313. return result;
  314. }
  315. #else /* OPENSSL_NO_HEARTBEATS */
  316. int main(int argc, char *argv[])
  317. {
  318. return EXIT_SUCCESS;
  319. }
  320. #endif /* OPENSSL_NO_HEARTBEATS */