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