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 "../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 {
62 const char *test_case_name;
63 int (*process_heartbeat) (SSL *s, unsigned char *p, unsigned int length);
64 unsigned char *payload;
66 int expected_return_value;
67 int return_payload_offset;
68 int expected_payload_len;
69 const char *expected_return_payload;
70 } HEARTBEAT_TEST_FIXTURE;
72 static HEARTBEAT_TEST_FIXTURE set_up(const char *const test_case_name,
73 const SSL_METHOD *meth)
75 HEARTBEAT_TEST_FIXTURE fixture;
77 memset(&fixture, 0, sizeof(fixture));
78 fixture.test_case_name = test_case_name;
80 fixture.ctx = SSL_CTX_new(meth);
82 fprintf(stderr, "Failed to allocate SSL_CTX for test: %s\n",
88 fixture.s = SSL_new(fixture.ctx);
90 fprintf(stderr, "Failed to allocate SSL for test: %s\n",
96 if (!ssl_init_wbio_buffer(fixture.s, 1)) {
97 fprintf(stderr, "Failed to set up wbio buffer for test: %s\n",
103 if (!ssl3_setup_buffers(fixture.s)) {
104 fprintf(stderr, "Failed to setup buffers for test: %s\n",
111 * Clear the memory for the return buffer, since this isn't automatically
112 * zeroed in opt mode and will cause spurious test failures that will
113 * change with each execution.
115 memset(fixture.s->rlayer.wbuf.buf, 0, fixture.s->rlayer.wbuf.len);
119 ERR_print_errors_fp(stderr);
125 static HEARTBEAT_TEST_FIXTURE set_up_dtls(const char *const test_case_name)
127 HEARTBEAT_TEST_FIXTURE fixture = set_up(test_case_name,
128 DTLSv1_server_method());
129 fixture.process_heartbeat = dtls1_process_heartbeat;
132 * As per dtls1_get_record(), skipping the following from the beginning
133 * of the returned heartbeat message: type-1 byte; version-2 bytes;
134 * sequence number-8 bytes; length-2 bytes And then skipping the 1-byte
135 * type encoded by process_heartbeat for a total of 14 bytes, at which
136 * point we can grab the length and the payload we seek.
138 fixture.return_payload_offset = 14;
142 /* Needed by ssl3_write_bytes() */
143 static int dummy_handshake(SSL *s)
148 static HEARTBEAT_TEST_FIXTURE set_up_tls(const char *const test_case_name)
150 HEARTBEAT_TEST_FIXTURE fixture = set_up(test_case_name,
151 TLSv1_server_method());
152 fixture.process_heartbeat = tls1_process_heartbeat;
153 fixture.s->handshake_func = dummy_handshake;
156 * As per do_ssl3_write(), skipping the following from the beginning of
157 * the returned heartbeat message: type-1 byte; version-2 bytes; length-2
158 * bytes And then skipping the 1-byte type encoded by process_heartbeat
159 * for a total of 6 bytes, at which point we can grab the length and the
162 fixture.return_payload_offset = 6;
166 static void tear_down(HEARTBEAT_TEST_FIXTURE fixture)
168 ERR_print_errors_fp(stderr);
170 SSL_CTX_free(fixture.ctx);
173 static void print_payload(const char *const prefix,
174 const unsigned char *payload, const int n)
176 const int end = n < MAX_PRINTABLE_CHARACTERS ? n
177 : MAX_PRINTABLE_CHARACTERS;
180 printf("%s %d character%s", prefix, n, n == 1 ? "" : "s");
182 printf(" (first %d shown)", end);
185 for (; i != end; ++i) {
186 const unsigned char c = payload[i];
190 printf("\\x%02x", c);
195 static int execute_heartbeat(HEARTBEAT_TEST_FIXTURE fixture)
199 unsigned char *payload = fixture.payload;
200 unsigned char sent_buf[MAX_PRINTABLE_CHARACTERS + 1];
202 unsigned const char *p;
203 int actual_payload_len;
205 s->rlayer.rrec.data = payload;
206 s->rlayer.rrec.length = strlen((const char *)payload);
207 *payload++ = TLS1_HB_REQUEST;
208 s2n(fixture.sent_payload_len, payload);
211 * Make a local copy of the request, since it gets overwritten at some
214 memcpy(sent_buf, payload, sizeof(sent_buf));
216 return_value = fixture.process_heartbeat(s, s->rlayer.rrec.data,
217 s->rlayer.rrec.length);
219 if (return_value != fixture.expected_return_value) {
220 printf("%s failed: expected return value %d, received %d\n",
221 fixture.test_case_name, fixture.expected_return_value,
227 * If there is any byte alignment, it will be stored in wbuf.offset.
230 wbuf.buf[fixture.return_payload_offset + s->rlayer.wbuf.offset]);
231 actual_payload_len = 0;
232 n2s(p, actual_payload_len);
234 if (actual_payload_len != fixture.expected_payload_len) {
235 printf("%s failed:\n expected payload len: %d\n received: %d\n",
236 fixture.test_case_name, fixture.expected_payload_len,
238 print_payload("sent", sent_buf, strlen((const char *)sent_buf));
239 print_payload("received", p, actual_payload_len);
242 char *actual_payload =
243 BUF_strndup((const char *)p, actual_payload_len);
244 if (strcmp(actual_payload, fixture.expected_return_payload) != 0) {
246 ("%s failed:\n expected payload: \"%s\"\n received: \"%s\"\n",
247 fixture.test_case_name, fixture.expected_return_payload,
251 OPENSSL_free(actual_payload);
255 printf("** %s failed **\n--------\n", fixture.test_case_name);
260 static int honest_payload_size(unsigned char payload_buf[])
262 /* Omit three-byte pad at the beginning for type and payload length */
263 return strlen((const char *)&payload_buf[3]) - MIN_PADDING_SIZE;
266 # define SETUP_HEARTBEAT_TEST_FIXTURE(type)\
267 SETUP_TEST_FIXTURE(HEARTBEAT_TEST_FIXTURE, set_up_##type)
269 # define EXECUTE_HEARTBEAT_TEST()\
270 EXECUTE_TEST(execute_heartbeat, tear_down)
272 static int test_dtls1_not_bleeding()
274 SETUP_HEARTBEAT_TEST_FIXTURE(dtls);
275 /* Three-byte pad at the beginning for type and payload length */
276 unsigned char payload_buf[MAX_PRINTABLE_CHARACTERS + 4] =
277 " Not bleeding, sixteen spaces of padding" " ";
278 const int payload_buf_len = honest_payload_size(payload_buf);
280 fixture.payload = &payload_buf[0];
281 fixture.sent_payload_len = payload_buf_len;
282 fixture.expected_return_value = 0;
283 fixture.expected_payload_len = payload_buf_len;
284 fixture.expected_return_payload =
285 "Not bleeding, sixteen spaces of padding";
286 EXECUTE_HEARTBEAT_TEST();
289 static int test_dtls1_not_bleeding_empty_payload()
293 SETUP_HEARTBEAT_TEST_FIXTURE(dtls);
295 * Three-byte pad at the beginning for type and payload length, plus a
298 unsigned char payload_buf[4 + MAX_PRINTABLE_CHARACTERS];
299 memset(payload_buf, ' ', MIN_PADDING_SIZE + 3);
300 payload_buf[MIN_PADDING_SIZE + 3] = '\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[4 + MAX_PRINTABLE_CHARACTERS] =
318 fixture.payload = &payload_buf[0];
319 fixture.sent_payload_len = MAX_PRINTABLE_CHARACTERS;
320 fixture.expected_return_value = 0;
321 fixture.expected_payload_len = 0;
322 fixture.expected_return_payload = "";
323 EXECUTE_HEARTBEAT_TEST();
326 static int test_dtls1_heartbleed_empty_payload()
328 SETUP_HEARTBEAT_TEST_FIXTURE(dtls);
330 * Excluding the NUL at the end, one byte short of type + payload length
333 unsigned char payload_buf[MAX_PRINTABLE_CHARACTERS + 4];
334 memset(payload_buf, ' ', MIN_PADDING_SIZE + 2);
335 payload_buf[MIN_PADDING_SIZE + 2] = '\0';
337 fixture.payload = &payload_buf[0];
338 fixture.sent_payload_len = MAX_PRINTABLE_CHARACTERS;
339 fixture.expected_return_value = 0;
340 fixture.expected_payload_len = 0;
341 fixture.expected_return_payload = "";
342 EXECUTE_HEARTBEAT_TEST();
345 static int test_dtls1_heartbleed_excessive_plaintext_length()
347 SETUP_HEARTBEAT_TEST_FIXTURE(dtls);
349 * Excluding the NUL at the end, one byte in excess of maximum allowed
350 * heartbeat message length
352 unsigned char payload_buf[SSL3_RT_MAX_PLAIN_LENGTH + 2];
353 memset(payload_buf, ' ', sizeof(payload_buf));
354 payload_buf[sizeof(payload_buf) - 1] = '\0';
356 fixture.payload = &payload_buf[0];
357 fixture.sent_payload_len = honest_payload_size(payload_buf);
358 fixture.expected_return_value = 0;
359 fixture.expected_payload_len = 0;
360 fixture.expected_return_payload = "";
361 EXECUTE_HEARTBEAT_TEST();
364 static int test_tls1_not_bleeding()
366 SETUP_HEARTBEAT_TEST_FIXTURE(tls);
367 /* Three-byte pad at the beginning for type and payload length */
368 unsigned char payload_buf[MAX_PRINTABLE_CHARACTERS + 4] =
369 " Not bleeding, sixteen spaces of padding" " ";
370 const int payload_buf_len = honest_payload_size(payload_buf);
372 fixture.payload = &payload_buf[0];
373 fixture.sent_payload_len = payload_buf_len;
374 fixture.expected_return_value = 0;
375 fixture.expected_payload_len = payload_buf_len;
376 fixture.expected_return_payload =
377 "Not bleeding, sixteen spaces of padding";
378 EXECUTE_HEARTBEAT_TEST();
381 static int test_tls1_not_bleeding_empty_payload()
385 SETUP_HEARTBEAT_TEST_FIXTURE(tls);
387 * Three-byte pad at the beginning for type and payload length, plus a
390 unsigned char payload_buf[4 + MAX_PRINTABLE_CHARACTERS];
391 memset(payload_buf, ' ', MIN_PADDING_SIZE + 3);
392 payload_buf[MIN_PADDING_SIZE + 3] = '\0';
393 payload_buf_len = honest_payload_size(payload_buf);
395 fixture.payload = &payload_buf[0];
396 fixture.sent_payload_len = payload_buf_len;
397 fixture.expected_return_value = 0;
398 fixture.expected_payload_len = payload_buf_len;
399 fixture.expected_return_payload = "";
400 EXECUTE_HEARTBEAT_TEST();
403 static int test_tls1_heartbleed()
405 SETUP_HEARTBEAT_TEST_FIXTURE(tls);
406 /* Three-byte pad at the beginning for type and payload length */
407 unsigned char payload_buf[MAX_PRINTABLE_CHARACTERS + 4] =
410 fixture.payload = &payload_buf[0];
411 fixture.sent_payload_len = MAX_PRINTABLE_CHARACTERS;
412 fixture.expected_return_value = 0;
413 fixture.expected_payload_len = 0;
414 fixture.expected_return_payload = "";
415 EXECUTE_HEARTBEAT_TEST();
418 static int test_tls1_heartbleed_empty_payload()
420 SETUP_HEARTBEAT_TEST_FIXTURE(tls);
422 * Excluding the NUL at the end, one byte short of type + payload length
425 unsigned char payload_buf[MAX_PRINTABLE_CHARACTERS + 4];
426 memset(payload_buf, ' ', MIN_PADDING_SIZE + 2);
427 payload_buf[MIN_PADDING_SIZE + 2] = '\0';
429 fixture.payload = &payload_buf[0];
430 fixture.sent_payload_len = MAX_PRINTABLE_CHARACTERS;
431 fixture.expected_return_value = 0;
432 fixture.expected_payload_len = 0;
433 fixture.expected_return_payload = "";
434 EXECUTE_HEARTBEAT_TEST();
437 # undef EXECUTE_HEARTBEAT_TEST
438 # undef SETUP_HEARTBEAT_TEST_FIXTURE
440 int main(int argc, char *argv[])
445 SSL_load_error_strings();
447 ADD_TEST(test_dtls1_not_bleeding);
448 ADD_TEST(test_dtls1_not_bleeding_empty_payload);
449 ADD_TEST(test_dtls1_heartbleed);
450 ADD_TEST(test_dtls1_heartbleed_empty_payload);
451 ADD_TEST(test_dtls1_heartbleed_excessive_plaintext_length);
452 ADD_TEST(test_tls1_not_bleeding);
453 ADD_TEST(test_tls1_not_bleeding_empty_payload);
454 ADD_TEST(test_tls1_heartbleed);
455 ADD_TEST(test_tls1_heartbleed_empty_payload);
457 result = run_tests(argv[0]);
458 ERR_print_errors_fp(stderr);
462 #else /* OPENSSL_NO_HEARTBEATS */
464 int main(int argc, char *argv[])
468 #endif /* OPENSSL_NO_HEARTBEATS */