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 #include "../ssl/ssl_locl.h"
49 #if !defined(OPENSSL_NO_HEARTBEATS) && !defined(OPENSSL_SYS_WINDOWS)
51 /* As per https://tools.ietf.org/html/rfc6520#section-4 */
52 #define MIN_PADDING_SIZE 16
54 /* Maximum number of payload characters to print as test output */
55 #define MAX_PRINTABLE_CHARACTERS 1024
57 typedef struct heartbeat_test_fixture
61 const char* test_case_name;
62 int (*process_heartbeat)(SSL* s);
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);
82 fprintf(stderr, "Failed to allocate SSL_CTX for test: %s\n",
88 fixture.s = SSL_new(fixture.ctx);
91 fprintf(stderr, "Failed to allocate SSL for test: %s\n", test_case_name);
96 if (!ssl_init_wbio_buffer(fixture.s, 1))
98 fprintf(stderr, "Failed to set up wbio buffer for test: %s\n",
104 if (!ssl3_setup_buffers(fixture.s))
106 fprintf(stderr, "Failed to setup buffers for test: %s\n",
112 /* Clear the memory for the return buffer, since this isn't automatically
113 * zeroed in opt mode and will cause spurious test failures that will change
114 * with each execution.
116 memset(fixture.s->s3->wbuf.buf, 0, fixture.s->s3->wbuf.len);
121 ERR_print_errors_fp(stderr);
127 static HEARTBEAT_TEST_FIXTURE set_up_dtls(const char* const test_case_name)
129 HEARTBEAT_TEST_FIXTURE fixture = set_up(test_case_name,
130 DTLSv1_server_method());
131 fixture.process_heartbeat = dtls1_process_heartbeat;
133 /* As per dtls1_get_record(), skipping the following from the beginning of
134 * the returned heartbeat message:
135 * type-1 byte; version-2 bytes; sequence number-8 bytes; length-2 bytes
137 * And then skipping the 1-byte type encoded by process_heartbeat for
138 * a total of 14 bytes, at which point we can grab the length and the
141 fixture.return_payload_offset = 14;
145 /* Needed by ssl3_write_bytes() */
146 static int dummy_handshake(SSL* s)
151 static HEARTBEAT_TEST_FIXTURE set_up_tls(const char* const test_case_name)
153 HEARTBEAT_TEST_FIXTURE fixture = set_up(test_case_name,
154 TLSv1_server_method());
155 fixture.process_heartbeat = tls1_process_heartbeat;
156 fixture.s->handshake_func = dummy_handshake;
158 /* As per do_ssl3_write(), skipping the following from the beginning of
159 * the returned heartbeat message:
160 * type-1 byte; version-2 bytes; length-2 bytes
162 * And then skipping the 1-byte type encoded by process_heartbeat for
163 * a total of 6 bytes, at which point we can grab the length and the payload
166 fixture.return_payload_offset = 6;
170 static void tear_down(HEARTBEAT_TEST_FIXTURE fixture)
172 ERR_print_errors_fp(stderr);
174 SSL_CTX_free(fixture.ctx);
177 static void print_payload(const char* const prefix,
178 const unsigned char *payload, const int n)
180 const int end = n < MAX_PRINTABLE_CHARACTERS ? n
181 : MAX_PRINTABLE_CHARACTERS;
184 printf("%s %d character%s", prefix, n, n == 1 ? "" : "s");
185 if (end != n) printf(" (first %d shown)", end);
188 for (; i != end; ++i)
190 const unsigned char c = payload[i];
191 if (isprint(c)) fputc(c, stdout);
192 else printf("\\x%02x", c);
197 static int execute_heartbeat(HEARTBEAT_TEST_FIXTURE fixture)
201 unsigned char *payload = fixture.payload;
202 unsigned char sent_buf[MAX_PRINTABLE_CHARACTERS + 1];
204 unsigned const char *p;
205 int actual_payload_len;
207 s->s3->rrec.data = payload;
208 s->s3->rrec.length = strlen((const char*)payload);
209 *payload++ = TLS1_HB_REQUEST;
210 s2n(fixture.sent_payload_len, payload);
212 /* Make a local copy of the request, since it gets overwritten at some
214 memcpy((char *)sent_buf, (const char*)payload, sizeof(sent_buf));
216 return_value = fixture.process_heartbeat(s);
218 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,
226 /* If there is any byte alignment, it will be stored in wbuf.offset. */
227 p = &(s->s3->wbuf.buf[
228 fixture.return_payload_offset + s->s3->wbuf.offset]);
229 actual_payload_len = 0;
230 n2s(p, actual_payload_len);
232 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);
243 char* actual_payload = BUF_strndup((const char*)p, actual_payload_len);
244 if (strcmp(actual_payload, fixture.expected_return_payload) != 0)
246 printf("%s failed:\n expected payload: \"%s\"\n received: \"%s\"\n",
247 fixture.test_case_name, fixture.expected_return_payload,
251 OPENSSL_free(actual_payload);
256 printf("** %s failed **\n--------\n", fixture.test_case_name);
261 static int honest_payload_size(unsigned char payload_buf[])
263 /* Omit three-byte pad at the beginning for type and payload length */
264 return strlen((const char*)&payload_buf[3]) - MIN_PADDING_SIZE;
267 #define SETUP_HEARTBEAT_TEST_FIXTURE(type)\
268 SETUP_TEST_FIXTURE(HEARTBEAT_TEST_FIXTURE, set_up_##type)
270 #define EXECUTE_HEARTBEAT_TEST()\
271 EXECUTE_TEST(execute_heartbeat, tear_down)
273 static int test_dtls1_not_bleeding()
275 SETUP_HEARTBEAT_TEST_FIXTURE(dtls);
276 /* Three-byte pad at the beginning for type and payload length */
277 unsigned char payload_buf[MAX_PRINTABLE_CHARACTERS+4] =
278 " Not bleeding, sixteen spaces of padding"
280 const int payload_buf_len = honest_payload_size(payload_buf);
282 fixture.payload = &payload_buf[0];
283 fixture.sent_payload_len = payload_buf_len;
284 fixture.expected_return_value = 0;
285 fixture.expected_payload_len = payload_buf_len;
286 fixture.expected_return_payload = "Not bleeding, sixteen spaces of padding";
287 EXECUTE_HEARTBEAT_TEST();
290 static int test_dtls1_not_bleeding_empty_payload()
294 SETUP_HEARTBEAT_TEST_FIXTURE(dtls);
295 /* Three-byte pad at the beginning for type and payload length, plus a NUL
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);
328 /* Excluding the NUL at the end, one byte short of type + payload length +
330 unsigned char payload_buf[MAX_PRINTABLE_CHARACTERS + 4];
331 memset(payload_buf, ' ', MIN_PADDING_SIZE+2);
332 payload_buf[MIN_PADDING_SIZE+2] = '\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[MAX_PRINTABLE_CHARACTERS+4] =
364 " Not bleeding, sixteen spaces of padding"
366 const int payload_buf_len = honest_payload_size(payload_buf);
368 fixture.payload = &payload_buf[0];
369 fixture.sent_payload_len = payload_buf_len;
370 fixture.expected_return_value = 0;
371 fixture.expected_payload_len = payload_buf_len;
372 fixture.expected_return_payload = "Not bleeding, sixteen spaces of padding";
373 EXECUTE_HEARTBEAT_TEST();
376 static int test_tls1_not_bleeding_empty_payload()
380 SETUP_HEARTBEAT_TEST_FIXTURE(tls);
381 /* Three-byte pad at the beginning for type and payload length, plus a NUL
383 unsigned char payload_buf[4 + MAX_PRINTABLE_CHARACTERS];
384 memset(payload_buf, ' ', MIN_PADDING_SIZE+3);
385 payload_buf[MIN_PADDING_SIZE+3] = '\0';
386 payload_buf_len = honest_payload_size(payload_buf);
388 fixture.payload = &payload_buf[0];
389 fixture.sent_payload_len = payload_buf_len;
390 fixture.expected_return_value = 0;
391 fixture.expected_payload_len = payload_buf_len;
392 fixture.expected_return_payload = "";
393 EXECUTE_HEARTBEAT_TEST();
396 static int test_tls1_heartbleed()
398 SETUP_HEARTBEAT_TEST_FIXTURE(tls);
399 /* Three-byte pad at the beginning for type and payload length */
400 unsigned char payload_buf[MAX_PRINTABLE_CHARACTERS+4] =
403 fixture.payload = &payload_buf[0];
404 fixture.sent_payload_len = MAX_PRINTABLE_CHARACTERS;
405 fixture.expected_return_value = 0;
406 fixture.expected_payload_len = 0;
407 fixture.expected_return_payload = "";
408 EXECUTE_HEARTBEAT_TEST();
411 static int test_tls1_heartbleed_empty_payload()
413 SETUP_HEARTBEAT_TEST_FIXTURE(tls);
414 /* Excluding the NUL at the end, one byte short of type + payload length +
416 unsigned char payload_buf[MAX_PRINTABLE_CHARACTERS + 4];
417 memset(payload_buf, ' ', MIN_PADDING_SIZE+2);
418 payload_buf[MIN_PADDING_SIZE+2] = '\0';
420 fixture.payload = &payload_buf[0];
421 fixture.sent_payload_len = MAX_PRINTABLE_CHARACTERS;
422 fixture.expected_return_value = 0;
423 fixture.expected_payload_len = 0;
424 fixture.expected_return_payload = "";
425 EXECUTE_HEARTBEAT_TEST();
428 #undef EXECUTE_HEARTBEAT_TEST
429 #undef SETUP_HEARTBEAT_TEST_FIXTURE
431 int main(int argc, char *argv[])
436 SSL_load_error_strings();
438 ADD_TEST(test_dtls1_not_bleeding);
439 ADD_TEST(test_dtls1_not_bleeding_empty_payload);
440 ADD_TEST(test_dtls1_heartbleed);
441 ADD_TEST(test_dtls1_heartbleed_empty_payload);
442 ADD_TEST(test_dtls1_heartbleed_excessive_plaintext_length);
443 ADD_TEST(test_tls1_not_bleeding);
444 ADD_TEST(test_tls1_not_bleeding_empty_payload);
445 ADD_TEST(test_tls1_heartbleed);
446 ADD_TEST(test_tls1_heartbleed_empty_payload);
448 result = run_tests(argv[0]);
449 ERR_print_errors_fp(stderr);
453 #else /* OPENSSL_NO_HEARTBEATS*/
455 int main(int argc, char *argv[])
459 #endif /* OPENSSL_NO_HEARTBEATS */