heartbeat_test.c revision 279264
1/* test/heartbeat_test.c */ 2/* 3 * Unit test for TLS heartbeats. 4 * 5 * Acts as a regression test against the Heartbleed bug (CVE-2014-0160). 6 * 7 * Author: Mike Bland (mbland@acm.org, http://mike-bland.com/) 8 * Date: 2014-04-12 9 * License: Creative Commons Attribution 4.0 International (CC By 4.0) 10 * http://creativecommons.org/licenses/by/4.0/deed.en_US 11 * 12 * OUTPUT 13 * ------ 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. 16 * 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. 20 * 21 * In a "bleeding" version, you'll see: 22 * 23 * test_dtls1_heartbleed failed: 24 * expected payload len: 0 25 * received: 1024 26 * sent 26 characters 27 * "HEARTBLEED " 28 * received 1024 characters 29 * "HEARTBLEED \xde\xad\xbe\xef..." 30 * ** test_dtls1_heartbleed failed ** 31 * 32 * The contents of the returned buffer in the failing test will depend on the 33 * contents of memory on your machine. 34 * 35 * MORE INFORMATION 36 * ---------------- 37 * http://mike-bland.com/2014/04/12/heartbleed.html 38 * http://mike-bland.com/tags/heartbleed.html 39 */ 40 41#define OPENSSL_UNIT_TEST 42 43#include "../test/testutil.h" 44 45#include "../ssl/ssl_locl.h" 46#include <ctype.h> 47#include <stdio.h> 48#include <stdlib.h> 49#include <string.h> 50 51#if !defined(OPENSSL_NO_HEARTBEATS) && !defined(OPENSSL_NO_UNIT_TEST) 52 53/* As per https://tools.ietf.org/html/rfc6520#section-4 */ 54#define MIN_PADDING_SIZE 16 55 56/* Maximum number of payload characters to print as test output */ 57#define MAX_PRINTABLE_CHARACTERS 1024 58 59typedef struct heartbeat_test_fixture 60 { 61 SSL_CTX *ctx; 62 SSL *s; 63 const char* test_case_name; 64 int (*process_heartbeat)(SSL* s); 65 unsigned char* payload; 66 int sent_payload_len; 67 int expected_return_value; 68 int return_payload_offset; 69 int expected_payload_len; 70 const char* expected_return_payload; 71 } HEARTBEAT_TEST_FIXTURE; 72 73static HEARTBEAT_TEST_FIXTURE set_up(const char* const test_case_name, 74 const SSL_METHOD* meth) 75 { 76 HEARTBEAT_TEST_FIXTURE fixture; 77 int setup_ok = 1; 78 memset(&fixture, 0, sizeof(fixture)); 79 fixture.test_case_name = test_case_name; 80 81 fixture.ctx = SSL_CTX_new(meth); 82 if (!fixture.ctx) 83 { 84 fprintf(stderr, "Failed to allocate SSL_CTX for test: %s\n", 85 test_case_name); 86 setup_ok = 0; 87 goto fail; 88 } 89 90 fixture.s = SSL_new(fixture.ctx); 91 if (!fixture.s) 92 { 93 fprintf(stderr, "Failed to allocate SSL for test: %s\n", test_case_name); 94 setup_ok = 0; 95 goto fail; 96 } 97 98 if (!ssl_init_wbio_buffer(fixture.s, 1)) 99 { 100 fprintf(stderr, "Failed to set up wbio buffer for test: %s\n", 101 test_case_name); 102 setup_ok = 0; 103 goto fail; 104 } 105 106 if (!ssl3_setup_buffers(fixture.s)) 107 { 108 fprintf(stderr, "Failed to setup buffers for test: %s\n", 109 test_case_name); 110 setup_ok = 0; 111 goto fail; 112 } 113 114 /* Clear the memory for the return buffer, since this isn't automatically 115 * zeroed in opt mode and will cause spurious test failures that will change 116 * with each execution. 117 */ 118 memset(fixture.s->s3->wbuf.buf, 0, fixture.s->s3->wbuf.len); 119 120 fail: 121 if (!setup_ok) 122 { 123 ERR_print_errors_fp(stderr); 124 exit(EXIT_FAILURE); 125 } 126 return fixture; 127 } 128 129static HEARTBEAT_TEST_FIXTURE set_up_dtls(const char* const test_case_name) 130 { 131 HEARTBEAT_TEST_FIXTURE fixture = set_up(test_case_name, 132 DTLSv1_server_method()); 133 fixture.process_heartbeat = dtls1_process_heartbeat; 134 135 /* As per dtls1_get_record(), skipping the following from the beginning of 136 * the returned heartbeat message: 137 * type-1 byte; version-2 bytes; sequence number-8 bytes; length-2 bytes 138 * 139 * And then skipping the 1-byte type encoded by process_heartbeat for 140 * a total of 14 bytes, at which point we can grab the length and the 141 * payload we seek. 142 */ 143 fixture.return_payload_offset = 14; 144 return fixture; 145 } 146 147/* Needed by ssl3_write_bytes() */ 148static int dummy_handshake(SSL* s) 149 { 150 return 1; 151 } 152 153static HEARTBEAT_TEST_FIXTURE set_up_tls(const char* const test_case_name) 154 { 155 HEARTBEAT_TEST_FIXTURE fixture = set_up(test_case_name, 156 TLSv1_server_method()); 157 fixture.process_heartbeat = tls1_process_heartbeat; 158 fixture.s->handshake_func = dummy_handshake; 159 160 /* As per do_ssl3_write(), skipping the following from the beginning of 161 * the returned heartbeat message: 162 * type-1 byte; version-2 bytes; length-2 bytes 163 * 164 * And then skipping the 1-byte type encoded by process_heartbeat for 165 * a total of 6 bytes, at which point we can grab the length and the payload 166 * we seek. 167 */ 168 fixture.return_payload_offset = 6; 169 return fixture; 170 } 171 172static void tear_down(HEARTBEAT_TEST_FIXTURE fixture) 173 { 174 ERR_print_errors_fp(stderr); 175 SSL_free(fixture.s); 176 SSL_CTX_free(fixture.ctx); 177 } 178 179static void print_payload(const char* const prefix, 180 const unsigned char *payload, const int n) 181 { 182 const int end = n < MAX_PRINTABLE_CHARACTERS ? n 183 : MAX_PRINTABLE_CHARACTERS; 184 int i = 0; 185 186 printf("%s %d character%s", prefix, n, n == 1 ? "" : "s"); 187 if (end != n) printf(" (first %d shown)", end); 188 printf("\n \""); 189 190 for (; i != end; ++i) 191 { 192 const unsigned char c = payload[i]; 193 if (isprint(c)) fputc(c, stdout); 194 else printf("\\x%02x", c); 195 } 196 printf("\"\n"); 197 } 198 199static int execute_heartbeat(HEARTBEAT_TEST_FIXTURE fixture) 200 { 201 int result = 0; 202 SSL* s = fixture.s; 203 unsigned char *payload = fixture.payload; 204 unsigned char sent_buf[MAX_PRINTABLE_CHARACTERS + 1]; 205 int return_value; 206 unsigned const char *p; 207 int actual_payload_len; 208 209 s->s3->rrec.data = payload; 210 s->s3->rrec.length = strlen((const char*)payload); 211 *payload++ = TLS1_HB_REQUEST; 212 s2n(fixture.sent_payload_len, payload); 213 214 /* Make a local copy of the request, since it gets overwritten at some 215 * point */ 216 memcpy((char *)sent_buf, (const char*)payload, sizeof(sent_buf)); 217 218 return_value = fixture.process_heartbeat(s); 219 220 if (return_value != fixture.expected_return_value) 221 { 222 printf("%s failed: expected return value %d, received %d\n", 223 fixture.test_case_name, fixture.expected_return_value, 224 return_value); 225 result = 1; 226 } 227 228 /* If there is any byte alignment, it will be stored in wbuf.offset. */ 229 p = &(s->s3->wbuf.buf[ 230 fixture.return_payload_offset + s->s3->wbuf.offset]); 231 actual_payload_len = 0; 232 n2s(p, actual_payload_len); 233 234 if (actual_payload_len != fixture.expected_payload_len) 235 { 236 printf("%s failed:\n expected payload len: %d\n received: %d\n", 237 fixture.test_case_name, fixture.expected_payload_len, 238 actual_payload_len); 239 print_payload("sent", sent_buf, strlen((const char*)sent_buf)); 240 print_payload("received", p, actual_payload_len); 241 result = 1; 242 } 243 else 244 { 245 char* actual_payload = BUF_strndup((const char*)p, actual_payload_len); 246 if (strcmp(actual_payload, fixture.expected_return_payload) != 0) 247 { 248 printf("%s failed:\n expected payload: \"%s\"\n received: \"%s\"\n", 249 fixture.test_case_name, fixture.expected_return_payload, 250 actual_payload); 251 result = 1; 252 } 253 OPENSSL_free(actual_payload); 254 } 255 256 if (result != 0) 257 { 258 printf("** %s failed **\n--------\n", fixture.test_case_name); 259 } 260 return result; 261 } 262 263static int honest_payload_size(unsigned char payload_buf[]) 264 { 265 /* Omit three-byte pad at the beginning for type and payload length */ 266 return strlen((const char*)&payload_buf[3]) - MIN_PADDING_SIZE; 267 } 268 269#define SETUP_HEARTBEAT_TEST_FIXTURE(type)\ 270 SETUP_TEST_FIXTURE(HEARTBEAT_TEST_FIXTURE, set_up_##type) 271 272#define EXECUTE_HEARTBEAT_TEST()\ 273 EXECUTE_TEST(execute_heartbeat, tear_down) 274 275static int test_dtls1_not_bleeding() 276 { 277 SETUP_HEARTBEAT_TEST_FIXTURE(dtls); 278 /* Three-byte pad at the beginning for type and payload length */ 279 unsigned char payload_buf[] = " Not bleeding, sixteen spaces of padding" 280 " "; 281 const int payload_buf_len = honest_payload_size(payload_buf); 282 283 fixture.payload = &payload_buf[0]; 284 fixture.sent_payload_len = payload_buf_len; 285 fixture.expected_return_value = 0; 286 fixture.expected_payload_len = payload_buf_len; 287 fixture.expected_return_payload = "Not bleeding, sixteen spaces of padding"; 288 EXECUTE_HEARTBEAT_TEST(); 289 } 290 291static int test_dtls1_not_bleeding_empty_payload() 292 { 293 int payload_buf_len; 294 295 SETUP_HEARTBEAT_TEST_FIXTURE(dtls); 296 /* Three-byte pad at the beginning for type and payload length, plus a NUL 297 * at the end */ 298 unsigned char payload_buf[4 + MIN_PADDING_SIZE]; 299 memset(payload_buf, ' ', sizeof(payload_buf)); 300 payload_buf[sizeof(payload_buf) - 1] = '\0'; 301 payload_buf_len = honest_payload_size(payload_buf); 302 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(); 309 } 310 311static int test_dtls1_heartbleed() 312 { 313 SETUP_HEARTBEAT_TEST_FIXTURE(dtls); 314 /* Three-byte pad at the beginning for type and payload length */ 315 unsigned char payload_buf[] = " HEARTBLEED "; 316 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(); 323 } 324 325static int test_dtls1_heartbleed_empty_payload() 326 { 327 SETUP_HEARTBEAT_TEST_FIXTURE(dtls); 328 /* Excluding the NUL at the end, one byte short of type + payload length + 329 * minimum padding */ 330 unsigned char payload_buf[MIN_PADDING_SIZE + 3]; 331 memset(payload_buf, ' ', sizeof(payload_buf)); 332 payload_buf[sizeof(payload_buf) - 1] = '\0'; 333 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(); 340 } 341 342static int test_dtls1_heartbleed_excessive_plaintext_length() 343 { 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'; 350 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(); 357 } 358 359static int test_tls1_not_bleeding() 360 { 361 SETUP_HEARTBEAT_TEST_FIXTURE(tls); 362 /* Three-byte pad at the beginning for type and payload length */ 363 unsigned char payload_buf[] = " Not bleeding, sixteen spaces of padding" 364 " "; 365 const int payload_buf_len = honest_payload_size(payload_buf); 366 367 fixture.payload = &payload_buf[0]; 368 fixture.sent_payload_len = payload_buf_len; 369 fixture.expected_return_value = 0; 370 fixture.expected_payload_len = payload_buf_len; 371 fixture.expected_return_payload = "Not bleeding, sixteen spaces of padding"; 372 EXECUTE_HEARTBEAT_TEST(); 373 } 374 375static int test_tls1_not_bleeding_empty_payload() 376 { 377 int payload_buf_len; 378 379 SETUP_HEARTBEAT_TEST_FIXTURE(tls); 380 /* Three-byte pad at the beginning for type and payload length, plus a NUL 381 * at the end */ 382 unsigned char payload_buf[4 + MIN_PADDING_SIZE]; 383 memset(payload_buf, ' ', sizeof(payload_buf)); 384 payload_buf[sizeof(payload_buf) - 1] = '\0'; 385 payload_buf_len = honest_payload_size(payload_buf); 386 387 fixture.payload = &payload_buf[0]; 388 fixture.sent_payload_len = payload_buf_len; 389 fixture.expected_return_value = 0; 390 fixture.expected_payload_len = payload_buf_len; 391 fixture.expected_return_payload = ""; 392 EXECUTE_HEARTBEAT_TEST(); 393 } 394 395static int test_tls1_heartbleed() 396 { 397 SETUP_HEARTBEAT_TEST_FIXTURE(tls); 398 /* Three-byte pad at the beginning for type and payload length */ 399 unsigned char payload_buf[] = " HEARTBLEED "; 400 401 fixture.payload = &payload_buf[0]; 402 fixture.sent_payload_len = MAX_PRINTABLE_CHARACTERS; 403 fixture.expected_return_value = 0; 404 fixture.expected_payload_len = 0; 405 fixture.expected_return_payload = ""; 406 EXECUTE_HEARTBEAT_TEST(); 407 } 408 409static int test_tls1_heartbleed_empty_payload() 410 { 411 SETUP_HEARTBEAT_TEST_FIXTURE(tls); 412 /* Excluding the NUL at the end, one byte short of type + payload length + 413 * minimum padding */ 414 unsigned char payload_buf[MIN_PADDING_SIZE + 3]; 415 memset(payload_buf, ' ', sizeof(payload_buf)); 416 payload_buf[sizeof(payload_buf) - 1] = '\0'; 417 418 fixture.payload = &payload_buf[0]; 419 fixture.sent_payload_len = MAX_PRINTABLE_CHARACTERS; 420 fixture.expected_return_value = 0; 421 fixture.expected_payload_len = 0; 422 fixture.expected_return_payload = ""; 423 EXECUTE_HEARTBEAT_TEST(); 424 } 425 426#undef EXECUTE_HEARTBEAT_TEST 427#undef SETUP_HEARTBEAT_TEST_FIXTURE 428 429int main(int argc, char *argv[]) 430 { 431 int num_failed; 432 433 SSL_library_init(); 434 SSL_load_error_strings(); 435 436 num_failed = test_dtls1_not_bleeding() + 437 test_dtls1_not_bleeding_empty_payload() + 438 test_dtls1_heartbleed() + 439 test_dtls1_heartbleed_empty_payload() + 440 /* The following test causes an assertion failure at 441 * ssl/d1_pkt.c:dtls1_write_bytes() in versions prior to 1.0.1g: */ 442 (OPENSSL_VERSION_NUMBER >= 0x1000107fL ? 443 test_dtls1_heartbleed_excessive_plaintext_length() : 0) + 444 test_tls1_not_bleeding() + 445 test_tls1_not_bleeding_empty_payload() + 446 test_tls1_heartbleed() + 447 test_tls1_heartbleed_empty_payload() + 448 0; 449 450 ERR_print_errors_fp(stderr); 451 452 if (num_failed != 0) 453 { 454 printf("%d test%s failed\n", num_failed, num_failed != 1 ? "s" : ""); 455 return EXIT_FAILURE; 456 } 457 return EXIT_SUCCESS; 458 } 459 460#else /* OPENSSL_NO_HEARTBEATS*/ 461 462int main(int argc, char *argv[]) 463 { 464 return EXIT_SUCCESS; 465 } 466#endif /* OPENSSL_NO_HEARTBEATS */ 467