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