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",
108 /* Clear the memory for the return buffer, since this isn't automatically
109 * zeroed in opt mode and will cause spurious test failures that will change
110 * with each execution.
112 memset(fixture
.s
->s3
->wbuf
.buf
, 0, fixture
.s
->s3
->wbuf
.len
);
117 ERR_print_errors_fp(stderr
);
123 static HEARTBEAT_TEST_FIXTURE
set_up_dtls(const char* const test_case_name
)
125 HEARTBEAT_TEST_FIXTURE fixture
= set_up(test_case_name
,
126 DTLSv1_server_method());
127 fixture
.process_heartbeat
= dtls1_process_heartbeat
;
129 /* As per dtls1_get_record(), skipping the following from the beginning of
130 * the returned heartbeat message:
131 * type-1 byte; version-2 bytes; sequence number-8 bytes; length-2 bytes
133 * And then skipping the 1-byte type encoded by process_heartbeat for
134 * a total of 14 bytes, at which point we can grab the length and the
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
;
154 /* As per do_ssl3_write(), skipping the following from the beginning of
155 * the returned heartbeat message:
156 * type-1 byte; version-2 bytes; length-2 bytes
158 * And then skipping the 1-byte type encoded by process_heartbeat for
159 * a total of 6 bytes, at which point we can grab the length and the payload
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");
181 if (end
!= n
) printf(" (first %d shown)", end
);
184 for (; i
!= end
; ++i
)
186 const unsigned char c
= payload
[i
];
187 if (isprint(c
)) fputc(c
, stdout
);
188 else printf("\\x%02x", c
);
193 static int execute_heartbeat(HEARTBEAT_TEST_FIXTURE fixture
)
197 unsigned char *payload
= fixture
.payload
;
198 unsigned char sent_buf
[MAX_PRINTABLE_CHARACTERS
+ 1];
200 unsigned const char *p
;
201 int actual_payload_len
;
203 s
->s3
->rrec
.data
= payload
;
204 s
->s3
->rrec
.length
= strlen((const char*)payload
);
205 *payload
++ = TLS1_HB_REQUEST
;
206 s2n(fixture
.sent_payload_len
, payload
);
208 /* Make a local copy of the request, since it gets overwritten at some
210 memcpy((char *)sent_buf
, (const char*)payload
, sizeof(sent_buf
));
212 return_value
= fixture
.process_heartbeat(s
);
214 if (return_value
!= fixture
.expected_return_value
)
216 printf("%s failed: expected return value %d, received %d\n",
217 fixture
.test_case_name
, fixture
.expected_return_value
,
222 /* If there is any byte alignment, it will be stored in wbuf.offset. */
223 p
= &(s
->s3
->wbuf
.buf
[
224 fixture
.return_payload_offset
+ s
->s3
->wbuf
.offset
]);
225 actual_payload_len
= 0;
226 n2s(p
, actual_payload_len
);
228 if (actual_payload_len
!= fixture
.expected_payload_len
)
230 printf("%s failed:\n expected payload len: %d\n received: %d\n",
231 fixture
.test_case_name
, fixture
.expected_payload_len
,
233 print_payload("sent", sent_buf
, strlen((const char*)sent_buf
));
234 print_payload("received", p
, actual_payload_len
);
239 char* actual_payload
= strndup((const char*)p
, actual_payload_len
);
240 if (strcmp(actual_payload
, fixture
.expected_return_payload
) != 0)
242 printf("%s failed:\n expected payload: \"%s\"\n received: \"%s\"\n",
243 fixture
.test_case_name
, fixture
.expected_return_payload
,
247 free(actual_payload
);
252 printf("** %s failed **\n--------\n", fixture
.test_case_name
);
257 static int honest_payload_size(unsigned char payload_buf
[])
259 /* Omit three-byte pad at the beginning for type and payload length */
260 return strlen((const char*)&payload_buf
[3]) - MIN_PADDING_SIZE
;
263 #define SETUP_HEARTBEAT_TEST_FIXTURE(type)\
264 HEARTBEAT_TEST_FIXTURE fixture = set_up_##type(__func__);\
267 #define EXECUTE_HEARTBEAT_TEST()\
268 if (execute_heartbeat(fixture) != 0) result = 1;\
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
[] = " 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
= "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
);
293 /* Three-byte pad at the beginning for type and payload length, plus a NUL
295 unsigned char payload_buf
[4 + MIN_PADDING_SIZE
];
296 memset(payload_buf
, ' ', sizeof(payload_buf
));
297 payload_buf
[sizeof(payload_buf
) - 1] = '\0';
298 payload_buf_len
= honest_payload_size(payload_buf
);
300 fixture
.payload
= &payload_buf
[0];
301 fixture
.sent_payload_len
= payload_buf_len
;
302 fixture
.expected_return_value
= 0;
303 fixture
.expected_payload_len
= payload_buf_len
;
304 fixture
.expected_return_payload
= "";
305 EXECUTE_HEARTBEAT_TEST();
308 static int test_dtls1_heartbleed()
310 SETUP_HEARTBEAT_TEST_FIXTURE(dtls
);
311 /* Three-byte pad at the beginning for type and payload length */
312 unsigned char payload_buf
[] = " HEARTBLEED ";
314 fixture
.payload
= &payload_buf
[0];
315 fixture
.sent_payload_len
= MAX_PRINTABLE_CHARACTERS
;
316 fixture
.expected_return_value
= 0;
317 fixture
.expected_payload_len
= 0;
318 fixture
.expected_return_payload
= "";
319 EXECUTE_HEARTBEAT_TEST();
322 static int test_dtls1_heartbleed_empty_payload()
324 SETUP_HEARTBEAT_TEST_FIXTURE(dtls
);
325 /* Excluding the NUL at the end, one byte short of type + payload length +
327 unsigned char payload_buf
[MIN_PADDING_SIZE
+ 3];
328 memset(payload_buf
, ' ', sizeof(payload_buf
));
329 payload_buf
[sizeof(payload_buf
) - 1] = '\0';
331 fixture
.payload
= &payload_buf
[0];
332 fixture
.sent_payload_len
= MAX_PRINTABLE_CHARACTERS
;
333 fixture
.expected_return_value
= 0;
334 fixture
.expected_payload_len
= 0;
335 fixture
.expected_return_payload
= "";
336 EXECUTE_HEARTBEAT_TEST();
339 static int test_dtls1_heartbleed_excessive_plaintext_length()
341 SETUP_HEARTBEAT_TEST_FIXTURE(dtls
);
342 /* Excluding the NUL at the end, one byte in excess of maximum allowed
343 * heartbeat message length */
344 unsigned char payload_buf
[SSL3_RT_MAX_PLAIN_LENGTH
+ 2];
345 memset(payload_buf
, ' ', sizeof(payload_buf
));
346 payload_buf
[sizeof(payload_buf
) - 1] = '\0';
348 fixture
.payload
= &payload_buf
[0];
349 fixture
.sent_payload_len
= honest_payload_size(payload_buf
);
350 fixture
.expected_return_value
= 0;
351 fixture
.expected_payload_len
= 0;
352 fixture
.expected_return_payload
= "";
353 EXECUTE_HEARTBEAT_TEST();
356 static int test_tls1_not_bleeding()
358 SETUP_HEARTBEAT_TEST_FIXTURE(tls
);
359 /* Three-byte pad at the beginning for type and payload length */
360 unsigned char payload_buf
[] = " Not bleeding, sixteen spaces of padding"
362 const int payload_buf_len
= honest_payload_size(payload_buf
);
364 fixture
.payload
= &payload_buf
[0];
365 fixture
.sent_payload_len
= payload_buf_len
;
366 fixture
.expected_return_value
= 0;
367 fixture
.expected_payload_len
= payload_buf_len
;
368 fixture
.expected_return_payload
= "Not bleeding, sixteen spaces of padding";
369 EXECUTE_HEARTBEAT_TEST();
372 static int test_tls1_not_bleeding_empty_payload()
376 SETUP_HEARTBEAT_TEST_FIXTURE(tls
);
377 /* Three-byte pad at the beginning for type and payload length, plus a NUL
379 unsigned char payload_buf
[4 + MIN_PADDING_SIZE
];
380 memset(payload_buf
, ' ', sizeof(payload_buf
));
381 payload_buf
[sizeof(payload_buf
) - 1] = '\0';
382 payload_buf_len
= honest_payload_size(payload_buf
);
384 fixture
.payload
= &payload_buf
[0];
385 fixture
.sent_payload_len
= payload_buf_len
;
386 fixture
.expected_return_value
= 0;
387 fixture
.expected_payload_len
= payload_buf_len
;
388 fixture
.expected_return_payload
= "";
389 EXECUTE_HEARTBEAT_TEST();
392 static int test_tls1_heartbleed()
394 SETUP_HEARTBEAT_TEST_FIXTURE(tls
);
395 /* Three-byte pad at the beginning for type and payload length */
396 unsigned char payload_buf
[] = " HEARTBLEED ";
398 fixture
.payload
= &payload_buf
[0];
399 fixture
.sent_payload_len
= MAX_PRINTABLE_CHARACTERS
;
400 fixture
.expected_return_value
= 0;
401 fixture
.expected_payload_len
= 0;
402 fixture
.expected_return_payload
= "";
403 EXECUTE_HEARTBEAT_TEST();
406 static int test_tls1_heartbleed_empty_payload()
408 SETUP_HEARTBEAT_TEST_FIXTURE(tls
);
409 /* Excluding the NUL at the end, one byte short of type + payload length +
411 unsigned char payload_buf
[MIN_PADDING_SIZE
+ 3];
412 memset(payload_buf
, ' ', sizeof(payload_buf
));
413 payload_buf
[sizeof(payload_buf
) - 1] = '\0';
415 fixture
.payload
= &payload_buf
[0];
416 fixture
.sent_payload_len
= MAX_PRINTABLE_CHARACTERS
;
417 fixture
.expected_return_value
= 0;
418 fixture
.expected_payload_len
= 0;
419 fixture
.expected_return_payload
= "";
420 EXECUTE_HEARTBEAT_TEST();
423 #undef EXECUTE_HEARTBEAT_TEST
424 #undef SETUP_HEARTBEAT_TEST_FIXTURE
426 int main(int argc
, char *argv
[])
431 SSL_load_error_strings();
433 num_failed
= test_dtls1_not_bleeding() +
434 test_dtls1_not_bleeding_empty_payload() +
435 test_dtls1_heartbleed() +
436 test_dtls1_heartbleed_empty_payload() +
437 /* The following test causes an assertion failure at
438 * ssl/d1_pkt.c:dtls1_write_bytes() in versions prior to 1.0.1g: */
439 (OPENSSL_VERSION_NUMBER
>= 0x1000107fL
?
440 test_dtls1_heartbleed_excessive_plaintext_length() : 0) +
441 test_tls1_not_bleeding() +
442 test_tls1_not_bleeding_empty_payload() +
443 test_tls1_heartbleed() +
444 test_tls1_heartbleed_empty_payload() +
447 ERR_print_errors_fp(stderr
);
451 printf("%d test%s failed\n", num_failed
, num_failed
!= 1 ? "s" : "");