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