2 * Unit test for TLS heartbeats.
4 * Acts as a regression test against the Heartbleed bug (CVE-2014-0160).
6 * Author: Mike Bland (mbland@acm.org, http://mike-bland.com/)
8 * License: Creative Commons Attribution 4.0 International (CC By 4.0)
9 * http://creativecommons.org/licenses/by/4.0/deed.en_US
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.
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.
20 * In a "bleeding" version, you'll see:
22 * test_dtls1_heartbleed failed:
23 * expected payload len: 0
27 * received 1024 characters
28 * "HEARTBLEED \xde\xad\xbe\xef..."
29 * ** test_dtls1_heartbleed failed **
31 * The contents of the returned buffer in the failing test will depend on the
32 * contents of memory on your machine.
36 * http://mike-bland.com/2014/04/12/heartbleed.html
37 * http://mike-bland.com/tags/heartbleed.html
40 #define OPENSSL_UNIT_TEST
42 #include "../ssl/ssl_locl.h"
50 #if !defined(OPENSSL_NO_HEARTBEATS) && !defined(OPENSSL_NO_UNIT_TEST)
52 /* As per https://tools.ietf.org/html/rfc6520#section-4 */
53 # define MIN_PADDING_SIZE 16
55 /* Maximum number of payload characters to print as test output */
56 # define MAX_PRINTABLE_CHARACTERS 1024
58 typedef struct heartbeat_test_fixture
{
61 const char *test_case_name
;
62 int (*process_heartbeat
) (SSL
*s
, unsigned char *p
, unsigned int length
);
63 unsigned char *payload
;
65 int expected_return_value
;
66 int return_payload_offset
;
67 int expected_payload_len
;
68 const char *expected_return_payload
;
69 } HEARTBEAT_TEST_FIXTURE
;
71 static HEARTBEAT_TEST_FIXTURE
set_up(const char *const test_case_name
,
72 const SSL_METHOD
*meth
)
74 HEARTBEAT_TEST_FIXTURE fixture
;
76 memset(&fixture
, 0, sizeof(fixture
));
77 fixture
.test_case_name
= test_case_name
;
79 fixture
.ctx
= SSL_CTX_new(meth
);
81 fprintf(stderr
, "Failed to allocate SSL_CTX for test: %s\n",
87 fixture
.s
= SSL_new(fixture
.ctx
);
89 fprintf(stderr
, "Failed to allocate SSL for test: %s\n",
95 if (!ssl_init_wbio_buffer(fixture
.s
, 1)) {
96 fprintf(stderr
, "Failed to set up wbio buffer for test: %s\n",
102 if (!ssl3_setup_buffers(fixture
.s
)) {
103 fprintf(stderr
, "Failed to setup buffers for test: %s\n",
110 * Clear the memory for the return buffer, since this isn't automatically
111 * zeroed in opt mode and will cause spurious test failures that will
112 * change with each execution.
114 memset(fixture
.s
->rlayer
.wbuf
.buf
, 0, fixture
.s
->rlayer
.wbuf
.len
);
118 ERR_print_errors_fp(stderr
);
124 static HEARTBEAT_TEST_FIXTURE
set_up_dtls(const char *const test_case_name
)
126 HEARTBEAT_TEST_FIXTURE fixture
= set_up(test_case_name
,
127 DTLSv1_server_method());
128 fixture
.process_heartbeat
= dtls1_process_heartbeat
;
131 * As per dtls1_get_record(), skipping the following from the beginning
132 * of the returned heartbeat message: type-1 byte; version-2 bytes;
133 * sequence number-8 bytes; length-2 bytes And then skipping the 1-byte
134 * type encoded by process_heartbeat for a total of 14 bytes, at which
135 * point we can grab the length and the payload we seek.
137 fixture
.return_payload_offset
= 14;
141 /* Needed by ssl3_write_bytes() */
142 static int dummy_handshake(SSL
*s
)
147 static HEARTBEAT_TEST_FIXTURE
set_up_tls(const char *const test_case_name
)
149 HEARTBEAT_TEST_FIXTURE fixture
= set_up(test_case_name
,
150 TLSv1_server_method());
151 fixture
.process_heartbeat
= tls1_process_heartbeat
;
152 fixture
.s
->handshake_func
= dummy_handshake
;
155 * As per do_ssl3_write(), skipping the following from the beginning of
156 * the returned heartbeat message: type-1 byte; version-2 bytes; length-2
157 * bytes And then skipping the 1-byte type encoded by process_heartbeat
158 * for a total of 6 bytes, at which point we can grab the length and the
161 fixture
.return_payload_offset
= 6;
165 static void tear_down(HEARTBEAT_TEST_FIXTURE fixture
)
167 ERR_print_errors_fp(stderr
);
169 SSL_CTX_free(fixture
.ctx
);
172 static void print_payload(const char *const prefix
,
173 const unsigned char *payload
, const int n
)
175 const int end
= n
< MAX_PRINTABLE_CHARACTERS
? n
176 : MAX_PRINTABLE_CHARACTERS
;
179 printf("%s %d character%s", prefix
, n
, n
== 1 ? "" : "s");
181 printf(" (first %d shown)", end
);
184 for (; i
!= end
; ++i
) {
185 const unsigned char c
= payload
[i
];
189 printf("\\x%02x", c
);
194 static int execute_heartbeat(HEARTBEAT_TEST_FIXTURE fixture
)
198 unsigned char *payload
= fixture
.payload
;
199 unsigned char sent_buf
[MAX_PRINTABLE_CHARACTERS
+ 1];
201 unsigned const char *p
;
202 int actual_payload_len
;
204 s
->rlayer
.rrec
.data
= payload
;
205 s
->rlayer
.rrec
.length
= strlen((const char *)payload
);
206 *payload
++ = TLS1_HB_REQUEST
;
207 s2n(fixture
.sent_payload_len
, payload
);
210 * Make a local copy of the request, since it gets overwritten at some
213 memcpy(sent_buf
, payload
, sizeof(sent_buf
));
215 return_value
= fixture
.process_heartbeat(s
, s
->rlayer
.rrec
.data
,
216 s
->rlayer
.rrec
.length
);
218 if (return_value
!= fixture
.expected_return_value
) {
219 printf("%s failed: expected return value %d, received %d\n",
220 fixture
.test_case_name
, fixture
.expected_return_value
,
226 * If there is any byte alignment, it will be stored in wbuf.offset.
229 wbuf
.buf
[fixture
.return_payload_offset
+ s
->rlayer
.wbuf
.offset
]);
230 actual_payload_len
= 0;
231 n2s(p
, actual_payload_len
);
233 if (actual_payload_len
!= fixture
.expected_payload_len
) {
234 printf("%s failed:\n expected payload len: %d\n received: %d\n",
235 fixture
.test_case_name
, fixture
.expected_payload_len
,
237 print_payload("sent", sent_buf
, strlen((const char *)sent_buf
));
238 print_payload("received", p
, actual_payload_len
);
241 char *actual_payload
=
242 OPENSSL_strndup((const char *)p
, actual_payload_len
);
243 if (strcmp(actual_payload
, fixture
.expected_return_payload
) != 0) {
245 ("%s failed:\n expected payload: \"%s\"\n received: \"%s\"\n",
246 fixture
.test_case_name
, fixture
.expected_return_payload
,
250 OPENSSL_free(actual_payload
);
254 printf("** %s failed **\n--------\n", fixture
.test_case_name
);
259 static int honest_payload_size(unsigned char payload_buf
[])
261 /* Omit three-byte pad at the beginning for type and payload length */
262 return strlen((const char *)&payload_buf
[3]) - MIN_PADDING_SIZE
;
265 # define SETUP_HEARTBEAT_TEST_FIXTURE(type)\
266 SETUP_TEST_FIXTURE(HEARTBEAT_TEST_FIXTURE, set_up_##type)
268 # define EXECUTE_HEARTBEAT_TEST()\
269 EXECUTE_TEST(execute_heartbeat, tear_down)
271 static int test_dtls1_not_bleeding()
273 SETUP_HEARTBEAT_TEST_FIXTURE(dtls
);
274 /* Three-byte pad at the beginning for type and payload length */
275 unsigned char payload_buf
[MAX_PRINTABLE_CHARACTERS
+ 4] =
276 " Not bleeding, sixteen spaces of padding" " ";
277 const int payload_buf_len
= honest_payload_size(payload_buf
);
279 fixture
.payload
= &payload_buf
[0];
280 fixture
.sent_payload_len
= payload_buf_len
;
281 fixture
.expected_return_value
= 0;
282 fixture
.expected_payload_len
= payload_buf_len
;
283 fixture
.expected_return_payload
=
284 "Not bleeding, sixteen spaces of padding";
285 EXECUTE_HEARTBEAT_TEST();
288 static int test_dtls1_not_bleeding_empty_payload()
292 SETUP_HEARTBEAT_TEST_FIXTURE(dtls
);
294 * Three-byte pad at the beginning for type and payload length, plus a
297 unsigned char payload_buf
[4 + MAX_PRINTABLE_CHARACTERS
];
298 memset(payload_buf
, ' ', MIN_PADDING_SIZE
+ 3);
299 payload_buf
[MIN_PADDING_SIZE
+ 3] = '\0';
300 payload_buf_len
= honest_payload_size(payload_buf
);
302 fixture
.payload
= &payload_buf
[0];
303 fixture
.sent_payload_len
= payload_buf_len
;
304 fixture
.expected_return_value
= 0;
305 fixture
.expected_payload_len
= payload_buf_len
;
306 fixture
.expected_return_payload
= "";
307 EXECUTE_HEARTBEAT_TEST();
310 static int test_dtls1_heartbleed()
312 SETUP_HEARTBEAT_TEST_FIXTURE(dtls
);
313 /* Three-byte pad at the beginning for type and payload length */
314 unsigned char payload_buf
[4 + MAX_PRINTABLE_CHARACTERS
] =
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
);
329 * 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
);
348 * Excluding the NUL at the end, one byte in excess of maximum allowed
349 * heartbeat message length
351 unsigned char payload_buf
[SSL3_RT_MAX_PLAIN_LENGTH
+ 2];
352 memset(payload_buf
, ' ', sizeof(payload_buf
));
353 payload_buf
[sizeof(payload_buf
) - 1] = '\0';
355 fixture
.payload
= &payload_buf
[0];
356 fixture
.sent_payload_len
= honest_payload_size(payload_buf
);
357 fixture
.expected_return_value
= 0;
358 fixture
.expected_payload_len
= 0;
359 fixture
.expected_return_payload
= "";
360 EXECUTE_HEARTBEAT_TEST();
363 static int test_tls1_not_bleeding()
365 SETUP_HEARTBEAT_TEST_FIXTURE(tls
);
366 /* Three-byte pad at the beginning for type and payload length */
367 unsigned char payload_buf
[MAX_PRINTABLE_CHARACTERS
+ 4] =
368 " Not bleeding, sixteen spaces of padding" " ";
369 const int payload_buf_len
= honest_payload_size(payload_buf
);
371 fixture
.payload
= &payload_buf
[0];
372 fixture
.sent_payload_len
= payload_buf_len
;
373 fixture
.expected_return_value
= 0;
374 fixture
.expected_payload_len
= payload_buf_len
;
375 fixture
.expected_return_payload
=
376 "Not bleeding, sixteen spaces of padding";
377 EXECUTE_HEARTBEAT_TEST();
380 static int test_tls1_not_bleeding_empty_payload()
384 SETUP_HEARTBEAT_TEST_FIXTURE(tls
);
386 * Three-byte pad at the beginning for type and payload length, plus a
389 unsigned char payload_buf
[4 + MAX_PRINTABLE_CHARACTERS
];
390 memset(payload_buf
, ' ', MIN_PADDING_SIZE
+ 3);
391 payload_buf
[MIN_PADDING_SIZE
+ 3] = '\0';
392 payload_buf_len
= honest_payload_size(payload_buf
);
394 fixture
.payload
= &payload_buf
[0];
395 fixture
.sent_payload_len
= payload_buf_len
;
396 fixture
.expected_return_value
= 0;
397 fixture
.expected_payload_len
= payload_buf_len
;
398 fixture
.expected_return_payload
= "";
399 EXECUTE_HEARTBEAT_TEST();
402 static int test_tls1_heartbleed()
404 SETUP_HEARTBEAT_TEST_FIXTURE(tls
);
405 /* Three-byte pad at the beginning for type and payload length */
406 unsigned char payload_buf
[MAX_PRINTABLE_CHARACTERS
+ 4] =
409 fixture
.payload
= &payload_buf
[0];
410 fixture
.sent_payload_len
= MAX_PRINTABLE_CHARACTERS
;
411 fixture
.expected_return_value
= 0;
412 fixture
.expected_payload_len
= 0;
413 fixture
.expected_return_payload
= "";
414 EXECUTE_HEARTBEAT_TEST();
417 static int test_tls1_heartbleed_empty_payload()
419 SETUP_HEARTBEAT_TEST_FIXTURE(tls
);
421 * Excluding the NUL at the end, one byte short of type + payload length
424 unsigned char payload_buf
[MAX_PRINTABLE_CHARACTERS
+ 4];
425 memset(payload_buf
, ' ', MIN_PADDING_SIZE
+ 2);
426 payload_buf
[MIN_PADDING_SIZE
+ 2] = '\0';
428 fixture
.payload
= &payload_buf
[0];
429 fixture
.sent_payload_len
= MAX_PRINTABLE_CHARACTERS
;
430 fixture
.expected_return_value
= 0;
431 fixture
.expected_payload_len
= 0;
432 fixture
.expected_return_payload
= "";
433 EXECUTE_HEARTBEAT_TEST();
436 # undef EXECUTE_HEARTBEAT_TEST
437 # undef SETUP_HEARTBEAT_TEST_FIXTURE
439 int main(int argc
, char *argv
[])
444 SSL_load_error_strings();
446 ADD_TEST(test_dtls1_not_bleeding
);
447 ADD_TEST(test_dtls1_not_bleeding_empty_payload
);
448 ADD_TEST(test_dtls1_heartbleed
);
449 ADD_TEST(test_dtls1_heartbleed_empty_payload
);
450 ADD_TEST(test_dtls1_heartbleed_excessive_plaintext_length
);
451 ADD_TEST(test_tls1_not_bleeding
);
452 ADD_TEST(test_tls1_not_bleeding_empty_payload
);
453 ADD_TEST(test_tls1_heartbleed
);
454 ADD_TEST(test_tls1_heartbleed_empty_payload
);
456 result
= run_tests(argv
[0]);
457 ERR_print_errors_fp(stderr
);
461 #else /* OPENSSL_NO_HEARTBEATS */
463 int main(int argc
, char *argv
[])
467 #endif /* OPENSSL_NO_HEARTBEATS */