Add a test for the TLS1.3 secret generation
[openssl.git] / test / heartbeat_test.c
1 /*
2  * Copyright 2014-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 /*-
11  * Unit test for TLS heartbeats.
12  *
13  * Acts as a regression test against the Heartbleed bug (CVE-2014-0160).
14  *
15  * Author:  Mike Bland (mbland@acm.org, http://mike-bland.com/)
16  * Date:    2014-04-12
17  * License: Creative Commons Attribution 4.0 International (CC By 4.0)
18  *          http://creativecommons.org/licenses/by/4.0/deed.en_US
19  *
20  * OUTPUT
21  * ------
22  * The program returns zero on success. It will print a message with a count
23  * of the number of failed tests and return nonzero if any tests fail.
24  *
25  * It will print the contents of the request and response buffers for each
26  * failing test. In a "fixed" version, all the tests should pass and there
27  * should be no output.
28  *
29  * In a "bleeding" version, you'll see:
30  *
31  *   test_dtls1_heartbleed failed:
32  *     expected payload len: 0
33  *     received: 1024
34  *   sent 26 characters
35  *     "HEARTBLEED                "
36  *   received 1024 characters
37  *     "HEARTBLEED                \xde\xad\xbe\xef..."
38  *   ** test_dtls1_heartbleed failed **
39  *
40  * The contents of the returned buffer in the failing test will depend on the
41  * contents of memory on your machine.
42  *
43  * MORE INFORMATION
44  * ----------------
45  * http://mike-bland.com/2014/04/12/heartbleed.html
46  * http://mike-bland.com/tags/heartbleed.html
47  */
48
49 #define OPENSSL_UNIT_TEST
50
51 #include "../ssl/ssl_locl.h"
52
53 #include "testutil.h"
54 #include <ctype.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58
59 #if !defined(OPENSSL_NO_HEARTBEATS) && !defined(OPENSSL_NO_UNIT_TEST)
60
61 /* As per https://tools.ietf.org/html/rfc6520#section-4 */
62 # define MIN_PADDING_SIZE        16
63
64 /* Maximum number of payload characters to print as test output */
65 # define MAX_PRINTABLE_CHARACTERS        1024
66
67 typedef struct heartbeat_test_fixture {
68     SSL_CTX *ctx;
69     SSL *s;
70     const char *test_case_name;
71     int (*process_heartbeat) (SSL *s, unsigned char *p, unsigned int length);
72     unsigned char *payload;
73     int sent_payload_len;
74     int expected_return_value;
75     int return_payload_offset;
76     int expected_payload_len;
77     const char *expected_return_payload;
78 } HEARTBEAT_TEST_FIXTURE;
79
80 static HEARTBEAT_TEST_FIXTURE set_up(const char *const test_case_name,
81                                      const SSL_METHOD *meth)
82 {
83     HEARTBEAT_TEST_FIXTURE fixture;
84     int setup_ok = 1;
85     memset(&fixture, 0, sizeof(fixture));
86     fixture.test_case_name = test_case_name;
87
88     fixture.ctx = SSL_CTX_new(meth);
89     if (!fixture.ctx) {
90         fprintf(stderr, "Failed to allocate SSL_CTX for test: %s\n",
91                 test_case_name);
92         setup_ok = 0;
93         goto fail;
94     }
95
96     fixture.s = SSL_new(fixture.ctx);
97     if (!fixture.s) {
98         fprintf(stderr, "Failed to allocate SSL for test: %s\n",
99                 test_case_name);
100         setup_ok = 0;
101         goto fail;
102     }
103
104     if (!ssl_init_wbio_buffer(fixture.s)) {
105         fprintf(stderr, "Failed to set up wbio buffer for test: %s\n",
106                 test_case_name);
107         setup_ok = 0;
108         goto fail;
109     }
110
111     if (!ssl3_setup_buffers(fixture.s)) {
112         fprintf(stderr, "Failed to setup buffers for test: %s\n",
113                 test_case_name);
114         setup_ok = 0;
115         goto fail;
116     }
117
118     /*
119      * Clear the memory for the return buffer, since this isn't automatically
120      * zeroed in opt mode and will cause spurious test failures that will
121      * change with each execution.
122      */
123     memset(fixture.s->rlayer.wbuf.buf, 0, fixture.s->rlayer.wbuf.len);
124
125  fail:
126     if (!setup_ok) {
127         ERR_print_errors_fp(stderr);
128         exit(EXIT_FAILURE);
129     }
130     return fixture;
131 }
132
133 static HEARTBEAT_TEST_FIXTURE set_up_dtls(const char *const test_case_name)
134 {
135     HEARTBEAT_TEST_FIXTURE fixture = set_up(test_case_name,
136                                             DTLS_server_method());
137     fixture.process_heartbeat = dtls1_process_heartbeat;
138
139     /*
140      * As per dtls1_get_record(), skipping the following from the beginning
141      * of the returned heartbeat message: type-1 byte; version-2 bytes;
142      * sequence number-8 bytes; length-2 bytes And then skipping the 1-byte
143      * type encoded by process_heartbeat for a total of 14 bytes, at which
144      * point we can grab the length and the payload we seek.
145      */
146     fixture.return_payload_offset = 14;
147     return fixture;
148 }
149
150 /* Needed by ssl3_write_bytes() */
151 static int dummy_handshake(SSL *s)
152 {
153     return 1;
154 }
155
156 static void tear_down(HEARTBEAT_TEST_FIXTURE fixture)
157 {
158     SSL_free(fixture.s);
159     SSL_CTX_free(fixture.ctx);
160 }
161
162 static void print_payload(const char *const prefix,
163                           const unsigned char *payload, const int n)
164 {
165     const int end = n < MAX_PRINTABLE_CHARACTERS ? n
166         : MAX_PRINTABLE_CHARACTERS;
167     int i = 0;
168
169     printf("%s %d character%s", prefix, n, n == 1 ? "" : "s");
170     if (end != n)
171         printf(" (first %d shown)", end);
172     printf("\n  \"");
173
174     for (; i != end; ++i) {
175         const unsigned char c = payload[i];
176         if (isprint(c))
177             fputc(c, stdout);
178         else
179             printf("\\x%02x", c);
180     }
181     printf("\"\n");
182 }
183
184 static int execute_heartbeat(HEARTBEAT_TEST_FIXTURE fixture)
185 {
186     int result = 0;
187     SSL *s = fixture.s;
188     unsigned char *payload = fixture.payload;
189     unsigned char sent_buf[MAX_PRINTABLE_CHARACTERS + 1];
190     int return_value;
191     unsigned const char *p;
192     int actual_payload_len;
193
194     s->rlayer.rrec.data = payload;
195     s->rlayer.rrec.length = strlen((const char *)payload);
196     *payload++ = TLS1_HB_REQUEST;
197     s2n(fixture.sent_payload_len, payload);
198
199     /*
200      * Make a local copy of the request, since it gets overwritten at some
201      * point
202      */
203     memcpy(sent_buf, payload, sizeof(sent_buf));
204
205     return_value = fixture.process_heartbeat(s, s->rlayer.rrec.data,
206         s->rlayer.rrec.length);
207
208     if (return_value != fixture.expected_return_value) {
209         printf("%s failed: expected return value %d, received %d\n",
210                fixture.test_case_name, fixture.expected_return_value,
211                return_value);
212         result = 1;
213     }
214
215     /*
216      * If there is any byte alignment, it will be stored in wbuf.offset.
217      */
218     p = &(s->rlayer.
219           wbuf.buf[fixture.return_payload_offset + s->rlayer.wbuf.offset]);
220     actual_payload_len = 0;
221     n2s(p, actual_payload_len);
222
223     if (actual_payload_len != fixture.expected_payload_len) {
224         printf("%s failed:\n  expected payload len: %d\n  received: %d\n",
225                fixture.test_case_name, fixture.expected_payload_len,
226                actual_payload_len);
227         print_payload("sent", sent_buf, strlen((const char *)sent_buf));
228         print_payload("received", p, actual_payload_len);
229         result = 1;
230     } else {
231         char *actual_payload =
232             OPENSSL_strndup((const char *)p, actual_payload_len);
233         if (strcmp(actual_payload, fixture.expected_return_payload) != 0) {
234             printf
235                 ("%s failed:\n  expected payload: \"%s\"\n  received: \"%s\"\n",
236                  fixture.test_case_name, fixture.expected_return_payload,
237                  actual_payload);
238             result = 1;
239         }
240         OPENSSL_free(actual_payload);
241     }
242
243     if (result != 0) {
244         printf("** %s failed **\n--------\n", fixture.test_case_name);
245     }
246     return result;
247 }
248
249 static int honest_payload_size(unsigned char payload_buf[])
250 {
251     /* Omit three-byte pad at the beginning for type and payload length */
252     return strlen((const char *)&payload_buf[3]) - MIN_PADDING_SIZE;
253 }
254
255 # define SETUP_HEARTBEAT_TEST_FIXTURE(type)\
256   SETUP_TEST_FIXTURE(HEARTBEAT_TEST_FIXTURE, set_up_##type)
257
258 # define EXECUTE_HEARTBEAT_TEST()\
259   EXECUTE_TEST(execute_heartbeat, tear_down)
260
261 static int test_dtls1_not_bleeding()
262 {
263     SETUP_HEARTBEAT_TEST_FIXTURE(dtls);
264     /* Three-byte pad at the beginning for type and payload length */
265     unsigned char payload_buf[MAX_PRINTABLE_CHARACTERS + 4] =
266         "   Not bleeding, sixteen spaces of padding" "                ";
267     const int payload_buf_len = honest_payload_size(payload_buf);
268
269     fixture.payload = &payload_buf[0];
270     fixture.sent_payload_len = payload_buf_len;
271     fixture.expected_return_value = 0;
272     fixture.expected_payload_len = payload_buf_len;
273     fixture.expected_return_payload =
274         "Not bleeding, sixteen spaces of padding";
275     EXECUTE_HEARTBEAT_TEST();
276 }
277
278 static int test_dtls1_not_bleeding_empty_payload()
279 {
280     int payload_buf_len;
281
282     SETUP_HEARTBEAT_TEST_FIXTURE(dtls);
283     /*
284      * Three-byte pad at the beginning for type and payload length, plus a
285      * NUL at the end
286      */
287     unsigned char payload_buf[4 + MAX_PRINTABLE_CHARACTERS];
288     memset(payload_buf, ' ', MIN_PADDING_SIZE + 3);
289     payload_buf[MIN_PADDING_SIZE + 3] = '\0';
290     payload_buf_len = honest_payload_size(payload_buf);
291
292     fixture.payload = &payload_buf[0];
293     fixture.sent_payload_len = payload_buf_len;
294     fixture.expected_return_value = 0;
295     fixture.expected_payload_len = payload_buf_len;
296     fixture.expected_return_payload = "";
297     EXECUTE_HEARTBEAT_TEST();
298 }
299
300 static int test_dtls1_heartbleed()
301 {
302     SETUP_HEARTBEAT_TEST_FIXTURE(dtls);
303     /* Three-byte pad at the beginning for type and payload length */
304     unsigned char payload_buf[4 + MAX_PRINTABLE_CHARACTERS] =
305         "   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     /*
319      * Excluding the NUL at the end, one byte short of type + payload length
320      * + minimum padding
321      */
322     unsigned char payload_buf[MAX_PRINTABLE_CHARACTERS + 4];
323     memset(payload_buf, ' ', MIN_PADDING_SIZE + 2);
324     payload_buf[MIN_PADDING_SIZE + 2] = '\0';
325
326     fixture.payload = &payload_buf[0];
327     fixture.sent_payload_len = MAX_PRINTABLE_CHARACTERS;
328     fixture.expected_return_value = 0;
329     fixture.expected_payload_len = 0;
330     fixture.expected_return_payload = "";
331     EXECUTE_HEARTBEAT_TEST();
332 }
333
334 static int test_dtls1_heartbleed_excessive_plaintext_length()
335 {
336     SETUP_HEARTBEAT_TEST_FIXTURE(dtls);
337     /*
338      * Excluding the NUL at the end, one byte in excess of maximum allowed
339      * heartbeat message length
340      */
341     unsigned char payload_buf[SSL3_RT_MAX_PLAIN_LENGTH + 2];
342     memset(payload_buf, ' ', sizeof(payload_buf));
343     payload_buf[sizeof(payload_buf) - 1] = '\0';
344
345     fixture.payload = &payload_buf[0];
346     fixture.sent_payload_len = honest_payload_size(payload_buf);
347     fixture.expected_return_value = 0;
348     fixture.expected_payload_len = 0;
349     fixture.expected_return_payload = "";
350     EXECUTE_HEARTBEAT_TEST();
351 }
352
353 # undef EXECUTE_HEARTBEAT_TEST
354 # undef SETUP_HEARTBEAT_TEST_FIXTURE
355
356 int main(int argc, char *argv[])
357 {
358     int result = 0;
359
360     ADD_TEST(test_dtls1_not_bleeding);
361     ADD_TEST(test_dtls1_not_bleeding_empty_payload);
362     ADD_TEST(test_dtls1_heartbleed);
363     ADD_TEST(test_dtls1_heartbleed_empty_payload);
364     ADD_TEST(test_dtls1_heartbleed_excessive_plaintext_length);
365
366     result = run_tests(argv[0]);
367     return result;
368 }
369
370 #else                           /* OPENSSL_NO_HEARTBEATS */
371
372 int main(int argc, char *argv[])
373 {
374     return EXIT_SUCCESS;
375 }
376 #endif                          /* OPENSSL_NO_HEARTBEATS */