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"
47 /* As per https://tools.ietf.org/html/rfc6520#section-4 */
48 #define MIN_PADDING_SIZE 16
50 /* Maximum number of payload characters to print as test output */
51 #define MAX_PRINTABLE_CHARACTERS 1024
53 typedef struct heartbeat_test_fixture
57 const char* test_case_name;
58 int (*process_heartbeat)(SSL* s);
59 unsigned char* payload;
61 int expected_return_value;
62 int return_payload_offset;
63 int expected_payload_len;
64 const char* expected_return_payload;
65 } HEARTBEAT_TEST_FIXTURE;
67 static HEARTBEAT_TEST_FIXTURE set_up(const char* const test_case_name,
68 const SSL_METHOD* meth)
70 HEARTBEAT_TEST_FIXTURE fixture;
72 memset(&fixture, 0, sizeof(fixture));
73 fixture.test_case_name = test_case_name;
75 fixture.ctx = SSL_CTX_new(meth);
78 fprintf(stderr, "Failed to allocate SSL_CTX for test: %s\n",
84 fixture.s = SSL_new(fixture.ctx);
87 fprintf(stderr, "Failed to allocate SSL for test: %s\n", test_case_name);
92 if (!ssl_init_wbio_buffer(fixture.s, 1))
94 fprintf(stderr, "Failed to set up wbio buffer for test: %s\n",
100 if (!ssl3_setup_buffers(fixture.s))
102 fprintf(stderr, "Failed to setup buffers for test: %s\n",
111 ERR_print_errors_fp(stderr);
117 static HEARTBEAT_TEST_FIXTURE set_up_dtls(const char* const test_case_name)
119 HEARTBEAT_TEST_FIXTURE fixture = set_up(test_case_name,
120 DTLSv1_server_method());
121 fixture.process_heartbeat = dtls1_process_heartbeat;
123 /* As per dtls1_get_record(), skipping the following from the beginning of
124 * the returned heartbeat message:
125 * type-1 byte; version-2 bytes; sequence number-8 bytes; length-2 bytes
127 * And then skipping the 1-byte type encoded by process_heartbeat for
128 * a total of 14 bytes, at which point we can grab the length and the
131 fixture.return_payload_offset = 14;
135 /* Needed by ssl3_write_bytes() */
136 static int dummy_handshake(SSL* s)
141 static HEARTBEAT_TEST_FIXTURE set_up_tls(const char* const test_case_name)
143 HEARTBEAT_TEST_FIXTURE fixture = set_up(test_case_name,
144 TLSv1_server_method());
145 fixture.process_heartbeat = tls1_process_heartbeat;
146 fixture.s->handshake_func = dummy_handshake;
148 /* As per do_ssl3_write(), skipping the following from the beginning of
149 * the returned heartbeat message:
150 * type-1 byte; version-2 bytes; length-2 bytes
152 * And then skipping the 1-byte type encoded by process_heartbeat for
153 * a total of 6 bytes, at which point we can grab the length and the payload
156 fixture.return_payload_offset = 6;
160 static void tear_down(HEARTBEAT_TEST_FIXTURE fixture)
162 ERR_print_errors_fp(stderr);
163 memset(fixture.s, 0, sizeof(*fixture.s));
165 memset(fixture.ctx, 0, sizeof(*fixture.ctx));
166 SSL_CTX_free(fixture.ctx);
169 static void print_payload(const char* const prefix,
170 const unsigned char *payload, const int n)
172 const int end = n < MAX_PRINTABLE_CHARACTERS ? n : MAX_PRINTABLE_CHARACTERS;
173 printf("%s %d character%s", prefix, n, n == 1 ? "" : "s");
174 if (end != n) printf(" (first %d shown)", end);
178 for (; i != end; ++i)
180 const unsigned char c = payload[i];
181 if (isprint(c)) fputc(c, stdout);
182 else printf("\\x%02x", c);
187 static int execute_heartbeat(HEARTBEAT_TEST_FIXTURE fixture)
191 unsigned char *payload = fixture.payload;
192 unsigned char sent_buf[MAX_PRINTABLE_CHARACTERS + 1];
194 s->s3->rrec.data = payload;
195 s->s3->rrec.length = strlen((const char*)payload);
196 *payload++ = TLS1_HB_REQUEST;
197 s2n(fixture.sent_payload_len, payload);
199 /* Make a local copy of the request, since it gets overwritten at some
201 memcpy((char *)sent_buf, (const char*)payload, sizeof(sent_buf));
203 int return_value = fixture.process_heartbeat(s);
205 if (return_value != fixture.expected_return_value)
207 printf("%s failed: expected return value %d, received %d\n",
208 fixture.test_case_name, fixture.expected_return_value,
213 /* If there is any byte alignment, it will be stored in wbuf.offset. */
214 unsigned const char *p = &(s->s3->wbuf.buf[
215 fixture.return_payload_offset + s->s3->wbuf.offset]);
216 int actual_payload_len = 0;
217 n2s(p, actual_payload_len);
219 if (actual_payload_len != fixture.expected_payload_len)
221 printf("%s failed:\n expected payload len: %d\n received: %d\n",
222 fixture.test_case_name, fixture.expected_payload_len,
224 print_payload("sent", sent_buf, strlen((const char*)sent_buf));
225 print_payload("received", p, actual_payload_len);
230 char* actual_payload = strndup((const char*)p, actual_payload_len);
231 if (strcmp(actual_payload, fixture.expected_return_payload) != 0)
233 printf("%s failed:\n expected payload: \"%s\"\n received: \"%s\"\n",
234 fixture.test_case_name, fixture.expected_return_payload,
238 free(actual_payload);
243 printf("** %s failed **\n--------\n", fixture.test_case_name);
248 static int honest_payload_size(unsigned char payload_buf[])
250 /* Omit three-byte pad at the beginning for type and payload length */
251 return strlen((const char*)&payload_buf[3]) - MIN_PADDING_SIZE;
254 #define SETUP_HEARTBEAT_TEST_FIXTURE(type)\
255 HEARTBEAT_TEST_FIXTURE fixture = set_up_##type(__func__);\
258 #define EXECUTE_HEARTBEAT_TEST()\
259 if (execute_heartbeat(fixture) != 0) result = 1;\
263 static int test_dtls1_not_bleeding()
265 SETUP_HEARTBEAT_TEST_FIXTURE(dtls);
266 /* Three-byte pad at the beginning for type and payload length */
267 unsigned char payload_buf[] = " Not bleeding, sixteen spaces of padding"
269 const int payload_buf_len = honest_payload_size(payload_buf);
271 fixture.payload = &payload_buf[0];
272 fixture.sent_payload_len = payload_buf_len;
273 fixture.expected_return_value = 0;
274 fixture.expected_payload_len = payload_buf_len;
275 fixture.expected_return_payload = "Not bleeding, sixteen spaces of padding";
276 EXECUTE_HEARTBEAT_TEST();
279 static int test_dtls1_not_bleeding_empty_payload()
281 SETUP_HEARTBEAT_TEST_FIXTURE(dtls);
282 /* Three-byte pad at the beginning for type and payload length, plus a NUL
284 unsigned char payload_buf[4 + MIN_PADDING_SIZE];
285 memset(payload_buf, ' ', sizeof(payload_buf));
286 payload_buf[sizeof(payload_buf) - 1] = '\0';
287 const int payload_buf_len = honest_payload_size(payload_buf);
289 fixture.payload = &payload_buf[0];
290 fixture.sent_payload_len = payload_buf_len;
291 fixture.expected_return_value = 0;
292 fixture.expected_payload_len = payload_buf_len;
293 fixture.expected_return_payload = "";
294 EXECUTE_HEARTBEAT_TEST();
297 static int test_dtls1_heartbleed()
299 SETUP_HEARTBEAT_TEST_FIXTURE(dtls);
300 /* Three-byte pad at the beginning for type and payload length */
301 unsigned char payload_buf[] = " HEARTBLEED ";
303 fixture.payload = &payload_buf[0];
304 fixture.sent_payload_len = MAX_PRINTABLE_CHARACTERS;
305 fixture.expected_return_value = 0;
306 fixture.expected_payload_len = 0;
307 fixture.expected_return_payload = "";
308 EXECUTE_HEARTBEAT_TEST();
311 static int test_dtls1_heartbleed_empty_payload()
313 SETUP_HEARTBEAT_TEST_FIXTURE(dtls);
314 /* Excluding the NUL at the end, one byte short of type + payload length +
316 unsigned char payload_buf[MIN_PADDING_SIZE + 3];
317 memset(payload_buf, ' ', sizeof(payload_buf));
318 payload_buf[sizeof(payload_buf) - 1] = '\0';
320 fixture.payload = &payload_buf[0];
321 fixture.sent_payload_len = MAX_PRINTABLE_CHARACTERS;
322 fixture.expected_return_value = 0;
323 fixture.expected_payload_len = 0;
324 fixture.expected_return_payload = "";
325 EXECUTE_HEARTBEAT_TEST();
328 static int test_dtls1_heartbleed_excessive_plaintext_length()
330 SETUP_HEARTBEAT_TEST_FIXTURE(dtls);
331 /* Excluding the NUL at the end, one byte in excess of maximum allowed
332 * heartbeat message length */
333 unsigned char payload_buf[SSL3_RT_MAX_PLAIN_LENGTH + 2];
334 memset(payload_buf, ' ', sizeof(payload_buf));
335 payload_buf[sizeof(payload_buf) - 1] = '\0';
337 fixture.payload = &payload_buf[0];
338 fixture.sent_payload_len = honest_payload_size(payload_buf);
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_tls1_not_bleeding()
347 SETUP_HEARTBEAT_TEST_FIXTURE(tls);
348 /* Three-byte pad at the beginning for type and payload length */
349 unsigned char payload_buf[] = " Not bleeding, sixteen spaces of padding"
351 const int payload_buf_len = honest_payload_size(payload_buf);
353 fixture.payload = &payload_buf[0];
354 fixture.sent_payload_len = payload_buf_len;
355 fixture.expected_return_value = 0;
356 fixture.expected_payload_len = payload_buf_len;
357 fixture.expected_return_payload = "Not bleeding, sixteen spaces of padding";
358 EXECUTE_HEARTBEAT_TEST();
361 static int test_tls1_not_bleeding_empty_payload()
363 SETUP_HEARTBEAT_TEST_FIXTURE(tls);
364 /* Three-byte pad at the beginning for type and payload length, plus a NUL
366 unsigned char payload_buf[4 + MIN_PADDING_SIZE];
367 memset(payload_buf, ' ', sizeof(payload_buf));
368 payload_buf[sizeof(payload_buf) - 1] = '\0';
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 EXECUTE_HEARTBEAT_TEST();
379 static int test_tls1_heartbleed()
381 SETUP_HEARTBEAT_TEST_FIXTURE(tls);
382 /* Three-byte pad at the beginning for type and payload length */
383 unsigned char payload_buf[] = " HEARTBLEED ";
385 fixture.payload = &payload_buf[0];
386 fixture.sent_payload_len = MAX_PRINTABLE_CHARACTERS;
387 fixture.expected_return_value = 0;
388 fixture.expected_payload_len = 0;
389 fixture.expected_return_payload = "";
390 EXECUTE_HEARTBEAT_TEST();
393 static int test_tls1_heartbleed_empty_payload()
395 SETUP_HEARTBEAT_TEST_FIXTURE(tls);
396 /* Excluding the NUL at the end, one byte short of type + payload length +
398 unsigned char payload_buf[MIN_PADDING_SIZE + 3];
399 memset(payload_buf, ' ', sizeof(payload_buf));
400 payload_buf[sizeof(payload_buf) - 1] = '\0';
402 fixture.payload = &payload_buf[0];
403 fixture.sent_payload_len = MAX_PRINTABLE_CHARACTERS;
404 fixture.expected_return_value = 0;
405 fixture.expected_payload_len = 0;
406 fixture.expected_return_payload = "";
407 EXECUTE_HEARTBEAT_TEST();
410 #undef EXECUTE_HEARTBEAT_TEST
411 #undef SETUP_HEARTBEAT_TEST_FIXTURE
413 int main(int argc, char *argv[])
416 SSL_load_error_strings();
418 const int num_failed = test_dtls1_not_bleeding() +
419 test_dtls1_not_bleeding_empty_payload() +
420 test_dtls1_heartbleed() +
421 test_dtls1_heartbleed_empty_payload() +
422 /* The following test causes an assertion failure at
423 * ssl/d1_pkt.c:dtls1_write_bytes() in versions prior to 1.0.1g: */
424 (OPENSSL_VERSION_NUMBER >= 0x1000107fL ?
425 test_dtls1_heartbleed_excessive_plaintext_length() : 0) +
426 test_tls1_not_bleeding() +
427 test_tls1_not_bleeding_empty_payload() +
428 test_tls1_heartbleed() +
429 test_tls1_heartbleed_empty_payload() +
432 ERR_print_errors_fp(stderr);
436 printf("%d test%s failed\n", num_failed, num_failed != 1 ? "s" : "");