1 /* test/heartbeat_test.c */
3 * Unit test for TLS heartbeats.
5 * Acts as a regression test against the Heartbleed bug (CVE-2014-0160).
7 * Author: Mike Bland (mbland@acm.org, http://mike-bland.com/)
9 * License: Creative Commons Attribution 4.0 International (CC By 4.0)
10 * http://creativecommons.org/licenses/by/4.0/deed.en_US
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.
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.
21 * In a "bleeding" version, you'll see:
23 * test_dtls1_heartbleed failed:
24 * expected payload len: 0
28 * received 1024 characters
29 * "HEARTBLEED \xde\xad\xbe\xef..."
30 * ** test_dtls1_heartbleed failed **
32 * The contents of the returned buffer in the failing test will depend on the
33 * contents of memory on your machine.
37 * http://mike-bland.com/2014/04/12/heartbleed.html
38 * http://mike-bland.com/tags/heartbleed.html
41 #define OPENSSL_UNIT_TEST
43 #include "../test/testutil.h"
45 #include "../ssl/ssl_locl.h"
51 #if !defined(OPENSSL_NO_HEARTBEATS) && !defined(OPENSSL_NO_UNIT_TEST)
53 /* As per https://tools.ietf.org/html/rfc6520#section-4 */
54 #define MIN_PADDING_SIZE 16
56 /* Maximum number of payload characters to print as test output */
57 #define MAX_PRINTABLE_CHARACTERS 1024
59 typedef struct heartbeat_test_fixture
63 const char* test_case_name;
64 int (*process_heartbeat)(SSL* s);
65 unsigned char* payload;
67 int expected_return_value;
68 int return_payload_offset;
69 int expected_payload_len;
70 const char* expected_return_payload;
71 } HEARTBEAT_TEST_FIXTURE;
73 static HEARTBEAT_TEST_FIXTURE set_up(const char* const test_case_name,
74 const SSL_METHOD* meth)
76 HEARTBEAT_TEST_FIXTURE fixture;
78 memset(&fixture, 0, sizeof(fixture));
79 fixture.test_case_name = test_case_name;
81 fixture.ctx = SSL_CTX_new(meth);
84 fprintf(stderr, "Failed to allocate SSL_CTX for test: %s\n",
90 fixture.s = SSL_new(fixture.ctx);
93 fprintf(stderr, "Failed to allocate SSL for test: %s\n", test_case_name);
98 if (!ssl_init_wbio_buffer(fixture.s, 1))
100 fprintf(stderr, "Failed to set up wbio buffer for test: %s\n",
106 if (!ssl3_setup_buffers(fixture.s))
108 fprintf(stderr, "Failed to setup buffers for test: %s\n",
114 /* Clear the memory for the return buffer, since this isn't automatically
115 * zeroed in opt mode and will cause spurious test failures that will change
116 * with each execution.
118 memset(fixture.s->s3->wbuf.buf, 0, fixture.s->s3->wbuf.len);
123 ERR_print_errors_fp(stderr);
129 static HEARTBEAT_TEST_FIXTURE set_up_dtls(const char* const test_case_name)
131 HEARTBEAT_TEST_FIXTURE fixture = set_up(test_case_name,
132 DTLSv1_server_method());
133 fixture.process_heartbeat = dtls1_process_heartbeat;
135 /* As per dtls1_get_record(), skipping the following from the beginning of
136 * the returned heartbeat message:
137 * type-1 byte; version-2 bytes; sequence number-8 bytes; length-2 bytes
139 * And then skipping the 1-byte type encoded by process_heartbeat for
140 * a total of 14 bytes, at which point we can grab the length and the
143 fixture.return_payload_offset = 14;
147 /* Needed by ssl3_write_bytes() */
148 static int dummy_handshake(SSL* s)
153 static HEARTBEAT_TEST_FIXTURE set_up_tls(const char* const test_case_name)
155 HEARTBEAT_TEST_FIXTURE fixture = set_up(test_case_name,
156 TLSv1_server_method());
157 fixture.process_heartbeat = tls1_process_heartbeat;
158 fixture.s->handshake_func = dummy_handshake;
160 /* As per do_ssl3_write(), skipping the following from the beginning of
161 * the returned heartbeat message:
162 * type-1 byte; version-2 bytes; length-2 bytes
164 * And then skipping the 1-byte type encoded by process_heartbeat for
165 * a total of 6 bytes, at which point we can grab the length and the payload
168 fixture.return_payload_offset = 6;
172 static void tear_down(HEARTBEAT_TEST_FIXTURE fixture)
174 ERR_print_errors_fp(stderr);
176 SSL_CTX_free(fixture.ctx);
179 static void print_payload(const char* const prefix,
180 const unsigned char *payload, const int n)
182 const int end = n < MAX_PRINTABLE_CHARACTERS ? n
183 : MAX_PRINTABLE_CHARACTERS;
186 printf("%s %d character%s", prefix, n, n == 1 ? "" : "s");
187 if (end != n) printf(" (first %d shown)", end);
190 for (; i != end; ++i)
192 const unsigned char c = payload[i];
193 if (isprint(c)) fputc(c, stdout);
194 else printf("\\x%02x", c);
199 static int execute_heartbeat(HEARTBEAT_TEST_FIXTURE fixture)
203 unsigned char *payload = fixture.payload;
204 unsigned char sent_buf[MAX_PRINTABLE_CHARACTERS + 1];
206 unsigned const char *p;
207 int actual_payload_len;
209 s->s3->rrec.data = payload;
210 s->s3->rrec.length = strlen((const char*)payload);
211 *payload++ = TLS1_HB_REQUEST;
212 s2n(fixture.sent_payload_len, payload);
214 /* Make a local copy of the request, since it gets overwritten at some
216 memcpy((char *)sent_buf, (const char*)payload, sizeof(sent_buf));
218 return_value = fixture.process_heartbeat(s);
220 if (return_value != fixture.expected_return_value)
222 printf("%s failed: expected return value %d, received %d\n",
223 fixture.test_case_name, fixture.expected_return_value,
228 /* If there is any byte alignment, it will be stored in wbuf.offset. */
229 p = &(s->s3->wbuf.buf[
230 fixture.return_payload_offset + s->s3->wbuf.offset]);
231 actual_payload_len = 0;
232 n2s(p, actual_payload_len);
234 if (actual_payload_len != fixture.expected_payload_len)
236 printf("%s failed:\n expected payload len: %d\n received: %d\n",
237 fixture.test_case_name, fixture.expected_payload_len,
239 print_payload("sent", sent_buf, strlen((const char*)sent_buf));
240 print_payload("received", p, actual_payload_len);
245 char* actual_payload = BUF_strndup((const char*)p, actual_payload_len);
246 if (strcmp(actual_payload, fixture.expected_return_payload) != 0)
248 printf("%s failed:\n expected payload: \"%s\"\n received: \"%s\"\n",
249 fixture.test_case_name, fixture.expected_return_payload,
253 OPENSSL_free(actual_payload);
258 printf("** %s failed **\n--------\n", fixture.test_case_name);
263 static int honest_payload_size(unsigned char payload_buf[])
265 /* Omit three-byte pad at the beginning for type and payload length */
266 return strlen((const char*)&payload_buf[3]) - MIN_PADDING_SIZE;
269 #define SETUP_HEARTBEAT_TEST_FIXTURE(type)\
270 SETUP_TEST_FIXTURE(HEARTBEAT_TEST_FIXTURE, set_up_##type)
272 #define EXECUTE_HEARTBEAT_TEST()\
273 EXECUTE_TEST(execute_heartbeat, tear_down)
275 static int test_dtls1_not_bleeding()
277 SETUP_HEARTBEAT_TEST_FIXTURE(dtls);
278 /* Three-byte pad at the beginning for type and payload length */
279 unsigned char payload_buf[MAX_PRINTABLE_CHARACTERS+4] =
280 " Not bleeding, sixteen spaces of padding"
282 const int payload_buf_len = honest_payload_size(payload_buf);
284 fixture.payload = &payload_buf[0];
285 fixture.sent_payload_len = payload_buf_len;
286 fixture.expected_return_value = 0;
287 fixture.expected_payload_len = payload_buf_len;
288 fixture.expected_return_payload = "Not bleeding, sixteen spaces of padding";
289 EXECUTE_HEARTBEAT_TEST();
292 static int test_dtls1_not_bleeding_empty_payload()
296 SETUP_HEARTBEAT_TEST_FIXTURE(dtls);
297 /* Three-byte pad at the beginning for type and payload length, plus a NUL
299 unsigned char payload_buf[4 + MAX_PRINTABLE_CHARACTERS];
300 memset(payload_buf, ' ', MIN_PADDING_SIZE+3);
301 payload_buf[MIN_PADDING_SIZE+3] = '\0';
302 payload_buf_len = honest_payload_size(payload_buf);
304 fixture.payload = &payload_buf[0];
305 fixture.sent_payload_len = payload_buf_len;
306 fixture.expected_return_value = 0;
307 fixture.expected_payload_len = payload_buf_len;
308 fixture.expected_return_payload = "";
309 EXECUTE_HEARTBEAT_TEST();
312 static int test_dtls1_heartbleed()
314 SETUP_HEARTBEAT_TEST_FIXTURE(dtls);
315 /* Three-byte pad at the beginning for type and payload length */
316 unsigned char payload_buf[4+MAX_PRINTABLE_CHARACTERS] =
319 fixture.payload = &payload_buf[0];
320 fixture.sent_payload_len = MAX_PRINTABLE_CHARACTERS;
321 fixture.expected_return_value = 0;
322 fixture.expected_payload_len = 0;
323 fixture.expected_return_payload = "";
324 EXECUTE_HEARTBEAT_TEST();
327 static int test_dtls1_heartbleed_empty_payload()
329 SETUP_HEARTBEAT_TEST_FIXTURE(dtls);
330 /* Excluding the NUL at the end, one byte short of type + payload length +
332 unsigned char payload_buf[MAX_PRINTABLE_CHARACTERS + 4];
333 memset(payload_buf, ' ', MIN_PADDING_SIZE+2);
334 payload_buf[MIN_PADDING_SIZE+2] = '\0';
336 fixture.payload = &payload_buf[0];
337 fixture.sent_payload_len = MAX_PRINTABLE_CHARACTERS;
338 fixture.expected_return_value = 0;
339 fixture.expected_payload_len = 0;
340 fixture.expected_return_payload = "";
341 EXECUTE_HEARTBEAT_TEST();
344 static int test_dtls1_heartbleed_excessive_plaintext_length()
346 SETUP_HEARTBEAT_TEST_FIXTURE(dtls);
347 /* Excluding the NUL at the end, one byte in excess of maximum allowed
348 * heartbeat message length */
349 unsigned char payload_buf[SSL3_RT_MAX_PLAIN_LENGTH + 2];
350 memset(payload_buf, ' ', sizeof(payload_buf));
351 payload_buf[sizeof(payload_buf) - 1] = '\0';
353 fixture.payload = &payload_buf[0];
354 fixture.sent_payload_len = honest_payload_size(payload_buf);
355 fixture.expected_return_value = 0;
356 fixture.expected_payload_len = 0;
357 fixture.expected_return_payload = "";
358 EXECUTE_HEARTBEAT_TEST();
361 static int test_tls1_not_bleeding()
363 SETUP_HEARTBEAT_TEST_FIXTURE(tls);
364 /* Three-byte pad at the beginning for type and payload length */
365 unsigned char payload_buf[MAX_PRINTABLE_CHARACTERS+4] =
366 " Not bleeding, sixteen spaces of padding"
368 const int payload_buf_len = honest_payload_size(payload_buf);
370 fixture.payload = &payload_buf[0];
371 fixture.sent_payload_len = payload_buf_len;
372 fixture.expected_return_value = 0;
373 fixture.expected_payload_len = payload_buf_len;
374 fixture.expected_return_payload = "Not bleeding, sixteen spaces of padding";
375 EXECUTE_HEARTBEAT_TEST();
378 static int test_tls1_not_bleeding_empty_payload()
382 SETUP_HEARTBEAT_TEST_FIXTURE(tls);
383 /* Three-byte pad at the beginning for type and payload length, plus a NUL
385 unsigned char payload_buf[4 + MAX_PRINTABLE_CHARACTERS];
386 memset(payload_buf, ' ', MIN_PADDING_SIZE+3);
387 payload_buf[MIN_PADDING_SIZE+3] = '\0';
388 payload_buf_len = honest_payload_size(payload_buf);
390 fixture.payload = &payload_buf[0];
391 fixture.sent_payload_len = payload_buf_len;
392 fixture.expected_return_value = 0;
393 fixture.expected_payload_len = payload_buf_len;
394 fixture.expected_return_payload = "";
395 EXECUTE_HEARTBEAT_TEST();
398 static int test_tls1_heartbleed()
400 SETUP_HEARTBEAT_TEST_FIXTURE(tls);
401 /* Three-byte pad at the beginning for type and payload length */
402 unsigned char payload_buf[MAX_PRINTABLE_CHARACTERS+4] =
405 fixture.payload = &payload_buf[0];
406 fixture.sent_payload_len = MAX_PRINTABLE_CHARACTERS;
407 fixture.expected_return_value = 0;
408 fixture.expected_payload_len = 0;
409 fixture.expected_return_payload = "";
410 EXECUTE_HEARTBEAT_TEST();
413 static int test_tls1_heartbleed_empty_payload()
415 SETUP_HEARTBEAT_TEST_FIXTURE(tls);
416 /* Excluding the NUL at the end, one byte short of type + payload length +
418 unsigned char payload_buf[MAX_PRINTABLE_CHARACTERS + 4];
419 memset(payload_buf, ' ', MIN_PADDING_SIZE+2);
420 payload_buf[MIN_PADDING_SIZE+2] = '\0';
422 fixture.payload = &payload_buf[0];
423 fixture.sent_payload_len = MAX_PRINTABLE_CHARACTERS;
424 fixture.expected_return_value = 0;
425 fixture.expected_payload_len = 0;
426 fixture.expected_return_payload = "";
427 EXECUTE_HEARTBEAT_TEST();
430 #undef EXECUTE_HEARTBEAT_TEST
431 #undef SETUP_HEARTBEAT_TEST_FIXTURE
433 int main(int argc, char *argv[])
438 SSL_load_error_strings();
440 num_failed = test_dtls1_not_bleeding() +
441 test_dtls1_not_bleeding_empty_payload() +
442 test_dtls1_heartbleed() +
443 test_dtls1_heartbleed_empty_payload() +
444 /* The following test causes an assertion failure at
445 * ssl/d1_pkt.c:dtls1_write_bytes() in versions prior to 1.0.1g: */
446 (OPENSSL_VERSION_NUMBER >= 0x1000107fL ?
447 test_dtls1_heartbleed_excessive_plaintext_length() : 0) +
448 test_tls1_not_bleeding() +
449 test_tls1_not_bleeding_empty_payload() +
450 test_tls1_heartbleed() +
451 test_tls1_heartbleed_empty_payload() +
454 ERR_print_errors_fp(stderr);
458 printf("%d test%s failed\n", num_failed, num_failed != 1 ? "s" : "");
464 #else /* OPENSSL_NO_HEARTBEATS*/
466 int main(int argc, char *argv[])
470 #endif /* OPENSSL_NO_HEARTBEATS */