Update heartbeat_test #includes
[openssl.git] / ssl / heartbeat_test.c
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
43 #include "testutil.h"
44 #include <ctype.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48
49 #if !defined(OPENSSL_NO_HEARTBEATS) && !defined(OPENSSL_SYS_WINDOWS)
50
51 /* As per https://tools.ietf.org/html/rfc6520#section-4 */
52 #define MIN_PADDING_SIZE        16
53
54 /* Maximum number of payload characters to print as test output */
55 #define MAX_PRINTABLE_CHARACTERS        1024
56
57 typedef struct heartbeat_test_fixture
58         {
59         SSL_CTX *ctx;
60         SSL *s;
61         const char* test_case_name;
62         int (*process_heartbeat)(SSL* s);
63         unsigned char* payload;
64         int sent_payload_len;
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;
70
71 static HEARTBEAT_TEST_FIXTURE set_up(const char* const test_case_name,
72         const SSL_METHOD* meth)
73         {
74         HEARTBEAT_TEST_FIXTURE fixture;
75         int setup_ok = 1;
76         memset(&fixture, 0, sizeof(fixture));
77         fixture.test_case_name = test_case_name;
78
79         fixture.ctx = SSL_CTX_new(meth);
80         if (!fixture.ctx)
81                 {
82                 fprintf(stderr, "Failed to allocate SSL_CTX for test: %s\n",
83                         test_case_name);
84                 setup_ok = 0;
85                 goto fail;
86                 }
87
88         fixture.s = SSL_new(fixture.ctx);
89         if (!fixture.s)
90                 {
91                 fprintf(stderr, "Failed to allocate SSL for test: %s\n", test_case_name);
92                 setup_ok = 0;
93                 goto fail;
94                 }
95
96         if (!ssl_init_wbio_buffer(fixture.s, 1))
97                 {
98                 fprintf(stderr, "Failed to set up wbio buffer for test: %s\n",
99                         test_case_name);
100                 setup_ok = 0;
101                 goto fail;
102                 }
103
104         if (!ssl3_setup_buffers(fixture.s))
105                 {
106                 fprintf(stderr, "Failed to setup buffers for test: %s\n",
107                         test_case_name);
108                 setup_ok = 0;
109                 goto fail;
110                 }
111
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.
115          */
116         memset(fixture.s->s3->wbuf.buf, 0, fixture.s->s3->wbuf.len);
117
118         fail:
119         if (!setup_ok)
120                 {
121                 ERR_print_errors_fp(stderr);
122                 exit(EXIT_FAILURE);
123                 }
124         return fixture;
125         }
126
127 static HEARTBEAT_TEST_FIXTURE set_up_dtls(const char* const test_case_name)
128         {
129         HEARTBEAT_TEST_FIXTURE fixture = set_up(test_case_name,
130                 DTLSv1_server_method());
131         fixture.process_heartbeat = dtls1_process_heartbeat;
132
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
136          *
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
139          * payload we seek.
140          */
141         fixture.return_payload_offset = 14;
142         return fixture;
143         }
144
145 /* Needed by ssl3_write_bytes() */
146 static int dummy_handshake(SSL* s)
147         {
148         return 1;
149         }
150
151 static HEARTBEAT_TEST_FIXTURE set_up_tls(const char* const test_case_name)
152         {
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;
157
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
161          *
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
164          * we seek.
165          */
166         fixture.return_payload_offset = 6;
167         return fixture;
168         }
169
170 static void tear_down(HEARTBEAT_TEST_FIXTURE fixture)
171         {
172         ERR_print_errors_fp(stderr);
173         SSL_free(fixture.s);
174         SSL_CTX_free(fixture.ctx);
175         }
176
177 static void print_payload(const char* const prefix,
178                 const unsigned char *payload, const int n)
179         {
180         const int end = n < MAX_PRINTABLE_CHARACTERS ? n
181             : MAX_PRINTABLE_CHARACTERS;
182         int i = 0;
183
184         printf("%s %d character%s", prefix, n, n == 1 ? "" : "s");
185         if (end != n) printf(" (first %d shown)", end);
186         printf("\n  \"");
187
188         for (; i != end; ++i)
189                 {
190                 const unsigned char c = payload[i];
191                 if (isprint(c)) fputc(c, stdout);
192                 else printf("\\x%02x", c);
193                 }
194         printf("\"\n");
195         }
196
197 static int execute_heartbeat(HEARTBEAT_TEST_FIXTURE fixture)
198         {
199         int result = 0;
200         SSL* s = fixture.s;
201         unsigned char *payload = fixture.payload;
202         unsigned char sent_buf[MAX_PRINTABLE_CHARACTERS + 1];
203         int return_value;
204         unsigned const char *p;
205         int actual_payload_len;
206
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);
211
212         /* Make a local copy of the request, since it gets overwritten at some
213          * point */
214         memcpy((char *)sent_buf, (const char*)payload, sizeof(sent_buf));
215
216         return_value = fixture.process_heartbeat(s);
217
218         if (return_value != fixture.expected_return_value)
219                 {
220                 printf("%s failed: expected return value %d, received %d\n",
221                                          fixture.test_case_name, fixture.expected_return_value,
222                                          return_value);
223                 result = 1;
224                 }
225
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);
231
232         if (actual_payload_len != fixture.expected_payload_len)
233                 {
234                 printf("%s failed:\n  expected payload len: %d\n  received: %d\n",
235                                          fixture.test_case_name, fixture.expected_payload_len,
236                                          actual_payload_len);
237                 print_payload("sent", sent_buf, strlen((const char*)sent_buf));
238                 print_payload("received", p, actual_payload_len);
239                 result = 1;
240                 }
241         else
242                 {
243                 char* actual_payload = BUF_strndup((const char*)p, actual_payload_len);
244                 if (strcmp(actual_payload, fixture.expected_return_payload) != 0)
245                         {
246                         printf("%s failed:\n  expected payload: \"%s\"\n  received: \"%s\"\n",
247                                                  fixture.test_case_name, fixture.expected_return_payload,
248                                                  actual_payload);
249                         result = 1;
250                         }
251                 OPENSSL_free(actual_payload);
252                 }
253
254         if (result != 0)
255                 {
256                 printf("** %s failed **\n--------\n", fixture.test_case_name);
257                 }
258         return result;
259         }
260
261 static int honest_payload_size(unsigned char payload_buf[])
262         {
263         /* Omit three-byte pad at the beginning for type and payload length */
264         return strlen((const char*)&payload_buf[3]) - MIN_PADDING_SIZE;
265         }
266
267 #define SETUP_HEARTBEAT_TEST_FIXTURE(type)\
268   SETUP_TEST_FIXTURE(HEARTBEAT_TEST_FIXTURE, set_up_##type)
269
270 #define EXECUTE_HEARTBEAT_TEST()\
271   EXECUTE_TEST(execute_heartbeat, tear_down)
272
273 static int test_dtls1_not_bleeding()
274         {
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"
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
290 static 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 + 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);
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
310 static 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[4+MAX_PRINTABLE_CHARACTERS] =
315                 "   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
325 static 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[MAX_PRINTABLE_CHARACTERS + 4];
331         memset(payload_buf, ' ', MIN_PADDING_SIZE+2);
332         payload_buf[MIN_PADDING_SIZE+2] = '\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
342 static 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
359 static 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[MAX_PRINTABLE_CHARACTERS+4] =
364                         "   Not bleeding, sixteen spaces of padding"
365                         "                ";
366         const int payload_buf_len = honest_payload_size(payload_buf);
367
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();
374         }
375
376 static int test_tls1_not_bleeding_empty_payload()
377         {
378         int payload_buf_len;
379
380         SETUP_HEARTBEAT_TEST_FIXTURE(tls);
381         /* Three-byte pad at the beginning for type and payload length, plus a NUL
382          * at the end */
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);
387
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();
394         }
395
396 static int test_tls1_heartbleed()
397         {
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] =
401                         "   HEARTBLEED                ";
402
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();
409         }
410
411 static int test_tls1_heartbleed_empty_payload()
412         {
413         SETUP_HEARTBEAT_TEST_FIXTURE(tls);
414         /* Excluding the NUL at the end, one byte short of type + payload length +
415          * minimum padding */
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';
419
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();
426         }
427
428 #undef EXECUTE_HEARTBEAT_TEST
429 #undef SETUP_HEARTBEAT_TEST_FIXTURE
430
431 int main(int argc, char *argv[])
432         {
433         int result = 0;
434
435         SSL_library_init();
436         SSL_load_error_strings();
437
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);
447
448         result = run_tests(argv[0]);
449         ERR_print_errors_fp(stderr);
450         return result;
451         }
452
453 #else /* OPENSSL_NO_HEARTBEATS*/
454
455 int main(int argc, char *argv[])
456         {
457                 return EXIT_SUCCESS;
458         }
459 #endif /* OPENSSL_NO_HEARTBEATS */