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