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[] = " Not bleeding, sixteen spaces of padding"
281 const int payload_buf_len = honest_payload_size(payload_buf);
283 fixture.payload = &payload_buf[0];
284 fixture.sent_payload_len = payload_buf_len;
285 fixture.expected_return_value = 0;
286 fixture.expected_payload_len = payload_buf_len;
287 fixture.expected_return_payload = "Not bleeding, sixteen spaces of padding";
288 EXECUTE_HEARTBEAT_TEST();
291 static int test_dtls1_not_bleeding_empty_payload()
295 SETUP_HEARTBEAT_TEST_FIXTURE(dtls);
296 /* Three-byte pad at the beginning for type and payload length, plus a NUL
298 unsigned char payload_buf[4 + MIN_PADDING_SIZE];
299 memset(payload_buf, ' ', sizeof(payload_buf));
300 payload_buf[sizeof(payload_buf) - 1] = '\0';
301 payload_buf_len = honest_payload_size(payload_buf);
303 fixture.payload = &payload_buf[0];
304 fixture.sent_payload_len = payload_buf_len;
305 fixture.expected_return_value = 0;
306 fixture.expected_payload_len = payload_buf_len;
307 fixture.expected_return_payload = "";
308 EXECUTE_HEARTBEAT_TEST();
311 static int test_dtls1_heartbleed()
313 SETUP_HEARTBEAT_TEST_FIXTURE(dtls);
314 /* Three-byte pad at the beginning for type and payload length */
315 unsigned char payload_buf[] = " HEARTBLEED ";
317 fixture.payload = &payload_buf[0];
318 fixture.sent_payload_len = MAX_PRINTABLE_CHARACTERS;
319 fixture.expected_return_value = 0;
320 fixture.expected_payload_len = 0;
321 fixture.expected_return_payload = "";
322 EXECUTE_HEARTBEAT_TEST();
325 static int test_dtls1_heartbleed_empty_payload()
327 SETUP_HEARTBEAT_TEST_FIXTURE(dtls);
328 /* Excluding the NUL at the end, one byte short of type + payload length +
330 unsigned char payload_buf[MIN_PADDING_SIZE + 3];
331 memset(payload_buf, ' ', sizeof(payload_buf));
332 payload_buf[sizeof(payload_buf) - 1] = '\0';
334 fixture.payload = &payload_buf[0];
335 fixture.sent_payload_len = MAX_PRINTABLE_CHARACTERS;
336 fixture.expected_return_value = 0;
337 fixture.expected_payload_len = 0;
338 fixture.expected_return_payload = "";
339 EXECUTE_HEARTBEAT_TEST();
342 static int test_dtls1_heartbleed_excessive_plaintext_length()
344 SETUP_HEARTBEAT_TEST_FIXTURE(dtls);
345 /* Excluding the NUL at the end, one byte in excess of maximum allowed
346 * heartbeat message length */
347 unsigned char payload_buf[SSL3_RT_MAX_PLAIN_LENGTH + 2];
348 memset(payload_buf, ' ', sizeof(payload_buf));
349 payload_buf[sizeof(payload_buf) - 1] = '\0';
351 fixture.payload = &payload_buf[0];
352 fixture.sent_payload_len = honest_payload_size(payload_buf);
353 fixture.expected_return_value = 0;
354 fixture.expected_payload_len = 0;
355 fixture.expected_return_payload = "";
356 EXECUTE_HEARTBEAT_TEST();
359 static int test_tls1_not_bleeding()
361 SETUP_HEARTBEAT_TEST_FIXTURE(tls);
362 /* Three-byte pad at the beginning for type and payload length */
363 unsigned char payload_buf[] = " Not bleeding, sixteen spaces of padding"
365 const int payload_buf_len = honest_payload_size(payload_buf);
367 fixture.payload = &payload_buf[0];
368 fixture.sent_payload_len = payload_buf_len;
369 fixture.expected_return_value = 0;
370 fixture.expected_payload_len = payload_buf_len;
371 fixture.expected_return_payload = "Not bleeding, sixteen spaces of padding";
372 EXECUTE_HEARTBEAT_TEST();
375 static int test_tls1_not_bleeding_empty_payload()
379 SETUP_HEARTBEAT_TEST_FIXTURE(tls);
380 /* Three-byte pad at the beginning for type and payload length, plus a NUL
382 unsigned char payload_buf[4 + MIN_PADDING_SIZE];
383 memset(payload_buf, ' ', sizeof(payload_buf));
384 payload_buf[sizeof(payload_buf) - 1] = '\0';
385 payload_buf_len = honest_payload_size(payload_buf);
387 fixture.payload = &payload_buf[0];
388 fixture.sent_payload_len = payload_buf_len;
389 fixture.expected_return_value = 0;
390 fixture.expected_payload_len = payload_buf_len;
391 fixture.expected_return_payload = "";
392 EXECUTE_HEARTBEAT_TEST();
395 static int test_tls1_heartbleed()
397 SETUP_HEARTBEAT_TEST_FIXTURE(tls);
398 /* Three-byte pad at the beginning for type and payload length */
399 unsigned char payload_buf[] = " HEARTBLEED ";
401 fixture.payload = &payload_buf[0];
402 fixture.sent_payload_len = MAX_PRINTABLE_CHARACTERS;
403 fixture.expected_return_value = 0;
404 fixture.expected_payload_len = 0;
405 fixture.expected_return_payload = "";
406 EXECUTE_HEARTBEAT_TEST();
409 static int test_tls1_heartbleed_empty_payload()
411 SETUP_HEARTBEAT_TEST_FIXTURE(tls);
412 /* Excluding the NUL at the end, one byte short of type + payload length +
414 unsigned char payload_buf[MIN_PADDING_SIZE + 3];
415 memset(payload_buf, ' ', sizeof(payload_buf));
416 payload_buf[sizeof(payload_buf) - 1] = '\0';
418 fixture.payload = &payload_buf[0];
419 fixture.sent_payload_len = MAX_PRINTABLE_CHARACTERS;
420 fixture.expected_return_value = 0;
421 fixture.expected_payload_len = 0;
422 fixture.expected_return_payload = "";
423 EXECUTE_HEARTBEAT_TEST();
426 #undef EXECUTE_HEARTBEAT_TEST
427 #undef SETUP_HEARTBEAT_TEST_FIXTURE
429 int main(int argc, char *argv[])
434 SSL_load_error_strings();
436 num_failed = test_dtls1_not_bleeding() +
437 test_dtls1_not_bleeding_empty_payload() +
438 test_dtls1_heartbleed() +
439 test_dtls1_heartbleed_empty_payload() +
440 /* The following test causes an assertion failure at
441 * ssl/d1_pkt.c:dtls1_write_bytes() in versions prior to 1.0.1g: */
442 (OPENSSL_VERSION_NUMBER >= 0x1000107fL ?
443 test_dtls1_heartbleed_excessive_plaintext_length() : 0) +
444 test_tls1_not_bleeding() +
445 test_tls1_not_bleeding_empty_payload() +
446 test_tls1_heartbleed() +
447 test_tls1_heartbleed_empty_payload() +
450 ERR_print_errors_fp(stderr);
454 printf("%d test%s failed\n", num_failed, num_failed != 1 ? "s" : "");
460 #else /* OPENSSL_NO_HEARTBEATS*/
462 int main(int argc, char *argv[])
466 #endif /* OPENSSL_NO_HEARTBEATS */