heartbeat_test.c 15 KB

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