80-test_ssl_new.t: Make 19-mac-then-encrypt.conf work without TLSv1.2
[openssl.git] / test / wpackettest.c
1 /*
2  * Copyright 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 #include <string.h>
11 #include <openssl/buffer.h>
12 #include "../ssl/packet_locl.h"
13 #include "testutil.h"
14 #include "test_main_custom.h"
15
16 const static unsigned char simple1 = 0xff;
17 const static unsigned char simple2[] = { 0x01, 0xff };
18 const static unsigned char simple3[] = { 0x00, 0x00, 0x00, 0x01, 0xff };
19 const static unsigned char nestedsub[] = { 0x03, 0xff, 0x01, 0xff };
20 const static unsigned char seqsub[] = { 0x01, 0xff, 0x01, 0xff };
21 const static unsigned char empty = 0x00;
22 const static unsigned char alloc[] = { 0x02, 0xfe, 0xff };
23 const static unsigned char submem[] = { 0x03, 0x02, 0xfe, 0xff };
24 const static unsigned char fixed[] = { 0xff, 0xff, 0xff };
25
26 static BUF_MEM *buf;
27
28 static void testfail(const char *msg, WPACKET *pkt)
29 {
30     fprintf(stderr, "%s", msg);
31     WPACKET_cleanup(pkt);
32 }
33
34 static int test_WPACKET_init(void)
35 {
36     WPACKET pkt;
37     int i;
38     size_t written;
39     unsigned char sbuf[3];
40
41     if (!WPACKET_init(&pkt, buf)
42             || !WPACKET_put_bytes_u8(&pkt, 0xff)
43                 /* Closing a top level WPACKET should fail */
44             ||  WPACKET_close(&pkt)
45                 /* Finishing a top level WPACKET should succeed */
46             || !WPACKET_finish(&pkt)
47                 /*
48                  * Can't call close or finish on a WPACKET that's already
49                  * finished.
50                  */
51             ||  WPACKET_close(&pkt)
52             ||  WPACKET_finish(&pkt)
53             || !WPACKET_get_total_written(&pkt, &written)
54             ||  written != sizeof(simple1)
55             ||  memcmp(buf->data, &simple1, written) != 0) {
56         testfail("test_WPACKET_init():1 failed\n", &pkt);
57         return 0;
58     }
59
60     /* Now try with a one byte length prefix */
61     if (!WPACKET_init_len(&pkt, buf, 1)
62             || !WPACKET_put_bytes_u8(&pkt, 0xff)
63             || !WPACKET_finish(&pkt)
64             || !WPACKET_get_total_written(&pkt, &written)
65             ||  written != sizeof(simple2)
66             ||  memcmp(buf->data, &simple2, written) != 0) {
67         testfail("test_WPACKET_init():2 failed\n", &pkt);
68         return 0;
69     }
70
71     /* And a longer length prefix */
72     if (!WPACKET_init_len(&pkt, buf, 4)
73             || !WPACKET_put_bytes_u8(&pkt, 0xff)
74             || !WPACKET_finish(&pkt)
75             || !WPACKET_get_total_written(&pkt, &written)
76             ||  written != sizeof(simple3)
77             ||  memcmp(buf->data, &simple3, written) != 0) {
78         testfail("test_WPACKET_init():3 failed\n", &pkt);
79         return 0;
80     }
81
82     if (!WPACKET_init_len(&pkt, buf, 1)) {
83         testfail("test_WPACKET_init():4 failed\n", &pkt);
84         return 0;
85     }
86     for (i = 1; i < 257; i++) {
87         /*
88          * Putting more bytes in than fit for the size of the length prefix
89          * should fail
90          */
91         if ((!WPACKET_put_bytes_u8(&pkt, 0xff)) == (i != 256)) {
92             testfail("test_WPACKET_init():4 failed\n", &pkt);
93             return 0;
94         }
95     }
96     if (!WPACKET_finish(&pkt)) {
97         testfail("test_WPACKET_init():4 failed\n", &pkt);
98         return 0;
99     }
100
101     /* Test initialising from a fixed size buffer */
102     if (!WPACKET_init_static_len(&pkt, sbuf, sizeof(sbuf), 0)
103                 /* Adding 3 bytes should succeed */
104             || !WPACKET_put_bytes_u24(&pkt, 0xffffff)
105                 /* Adding 1 more byte should fail */
106             ||  WPACKET_put_bytes_u8(&pkt, 0xff)
107                 /* Finishing the top level WPACKET should succeed */
108             || !WPACKET_finish(&pkt)
109             || !WPACKET_get_total_written(&pkt, &written)
110             ||  written != sizeof(fixed)
111             || memcmp(sbuf, fixed, sizeof(sbuf)) != 0
112                 /* Initialise with 1 len byte */
113             || !WPACKET_init_static_len(&pkt, sbuf, sizeof(sbuf), 1)
114                 /* Adding 2 bytes should succeed */
115             || !WPACKET_put_bytes_u16(&pkt, 0xfeff)
116                 /* Adding 1 more byte should fail */
117             ||  WPACKET_put_bytes_u8(&pkt, 0xff)
118             || !WPACKET_finish(&pkt)
119             || !WPACKET_get_total_written(&pkt, &written)
120             ||  written != sizeof(alloc)
121             ||  memcmp(sbuf, alloc, written) != 0) {
122         testfail("test_WPACKET_init():5 failed\n", &pkt);
123         return 0;
124     }
125
126     return 1;
127 }
128
129 static int test_WPACKET_set_max_size(void)
130 {
131     WPACKET pkt;
132     size_t written;
133
134     if (!WPACKET_init(&pkt, buf)
135                 /*
136                  * No previous lenbytes set so we should be ok to set the max
137                  * possible max size
138                  */
139             || !WPACKET_set_max_size(&pkt, SIZE_MAX)
140                 /* We should be able to set it smaller too */
141             || !WPACKET_set_max_size(&pkt, SIZE_MAX -1)
142                 /* And setting it bigger again should be ok */
143             || !WPACKET_set_max_size(&pkt, SIZE_MAX)
144             || !WPACKET_finish(&pkt)) {
145         testfail("test_WPACKET_set_max_size():1 failed\n", &pkt);
146         return 0;
147     }
148
149     if (!WPACKET_init_len(&pkt, buf, 1)
150                 /*
151                  * Should fail because we already consumed 1 byte with the
152                  * length
153                  */
154             ||  WPACKET_set_max_size(&pkt, 0)
155                 /*
156                  * Max size can't be bigger than biggest that will fit in
157                  * lenbytes
158                  */
159             ||  WPACKET_set_max_size(&pkt, 0x0101)
160                 /* It can be the same as the maximum possible size */
161             || !WPACKET_set_max_size(&pkt, 0x0100)
162                 /* Or it can be less */
163             || !WPACKET_set_max_size(&pkt, 0x01)
164                 /*
165                  * Should fail because packet is already filled
166                  */
167             ||  WPACKET_put_bytes_u8(&pkt, 0xff)
168                 /*
169                  * You can't put in more bytes than max size
170                  */
171             || !WPACKET_set_max_size(&pkt, 0x02)
172             || !WPACKET_put_bytes_u8(&pkt, 0xff)
173             ||  WPACKET_put_bytes_u8(&pkt, 0xff)
174             || !WPACKET_finish(&pkt)
175             || !WPACKET_get_total_written(&pkt, &written)
176             ||  written != sizeof(simple2)
177             ||  memcmp(buf->data, &simple2, written) != 0) {
178         testfail("test_WPACKET_set_max_size():2 failed\n", &pkt);
179         return 0;
180     }
181
182     return 1;
183 }
184
185 static int test_WPACKET_start_sub_packet(void)
186 {
187     WPACKET pkt;
188     size_t written;
189     size_t len;
190
191     if (!WPACKET_init(&pkt, buf)
192             || !WPACKET_start_sub_packet(&pkt)
193             || !WPACKET_put_bytes_u8(&pkt, 0xff)
194                 /* Can't finish because we have a sub packet */
195             ||  WPACKET_finish(&pkt)
196             || !WPACKET_close(&pkt)
197                 /* Sub packet is closed so can't close again */
198             ||  WPACKET_close(&pkt)
199                 /* Now a top level so finish should succeed */
200             || !WPACKET_finish(&pkt)
201             || !WPACKET_get_total_written(&pkt, &written)
202             ||  written != sizeof(simple1)
203             ||  memcmp(buf->data, &simple1, written) != 0) {
204         testfail("test_WPACKET_start_sub_packet():1 failed\n", &pkt);
205         return 0;
206     }
207
208    /* Single sub-packet with length prefix */
209     if (!WPACKET_init(&pkt, buf)
210             || !WPACKET_start_sub_packet_u8(&pkt)
211             || !WPACKET_put_bytes_u8(&pkt, 0xff)
212             || !WPACKET_close(&pkt)
213             || !WPACKET_finish(&pkt)
214             || !WPACKET_get_total_written(&pkt, &written)
215             ||  written != sizeof(simple2)
216             ||  memcmp(buf->data, &simple2, written) != 0) {
217         testfail("test_WPACKET_start_sub_packet():2 failed\n", &pkt);
218         return 0;
219     }
220
221     /* Nested sub-packets with length prefixes */
222     if (!WPACKET_init(&pkt, buf)
223             || !WPACKET_start_sub_packet_u8(&pkt)
224             || !WPACKET_put_bytes_u8(&pkt, 0xff)
225             || !WPACKET_start_sub_packet_u8(&pkt)
226             || !WPACKET_put_bytes_u8(&pkt, 0xff)
227             || !WPACKET_get_length(&pkt, &len)
228             || len != 1
229             || !WPACKET_close(&pkt)
230             || !WPACKET_get_length(&pkt, &len)
231             || len != 3
232             || !WPACKET_close(&pkt)
233             || !WPACKET_finish(&pkt)
234             || !WPACKET_get_total_written(&pkt, &written)
235             ||  written != sizeof(nestedsub)
236             ||  memcmp(buf->data, &nestedsub, written) != 0) {
237         testfail("test_WPACKET_start_sub_packet():3 failed\n", &pkt);
238         return 0;
239     }
240
241     /* Sequential sub-packets with length prefixes */
242     if (!WPACKET_init(&pkt, buf)
243             || !WPACKET_start_sub_packet_u8(&pkt)
244             || !WPACKET_put_bytes_u8(&pkt, 0xff)
245             || !WPACKET_close(&pkt)
246             || !WPACKET_start_sub_packet_u8(&pkt)
247             || !WPACKET_put_bytes_u8(&pkt, 0xff)
248             || !WPACKET_close(&pkt)
249             || !WPACKET_finish(&pkt)
250             || !WPACKET_get_total_written(&pkt, &written)
251             ||  written != sizeof(seqsub)
252             ||  memcmp(buf->data, &seqsub, written) != 0) {
253         testfail("test_WPACKET_start_sub_packet():4 failed\n", &pkt);
254         return 0;
255     }
256
257     return 1;
258 }
259
260
261 static int test_WPACKET_set_flags(void)
262 {
263     WPACKET pkt;
264     size_t written;
265
266     /* Set packet to be non-zero length */
267     if (!WPACKET_init(&pkt, buf)
268             || !WPACKET_set_flags(&pkt, WPACKET_FLAGS_NON_ZERO_LENGTH)
269                 /* Should fail because of zero length */
270             ||  WPACKET_finish(&pkt)
271             || !WPACKET_put_bytes_u8(&pkt, 0xff)
272             || !WPACKET_finish(&pkt)
273             || !WPACKET_get_total_written(&pkt, &written)
274             ||  written != sizeof(simple1)
275             ||  memcmp(buf->data, &simple1, written) != 0) {
276         testfail("test_WPACKET_set_flags():1 failed\n", &pkt);
277         return 0;
278     }
279
280     /* Repeat above test in a sub-packet */
281     if (!WPACKET_init(&pkt, buf)
282             || !WPACKET_start_sub_packet(&pkt)
283             || !WPACKET_set_flags(&pkt, WPACKET_FLAGS_NON_ZERO_LENGTH)
284                 /* Should fail because of zero length */
285             ||  WPACKET_close(&pkt)
286             || !WPACKET_put_bytes_u8(&pkt, 0xff)
287             || !WPACKET_close(&pkt)
288             || !WPACKET_finish(&pkt)
289             || !WPACKET_get_total_written(&pkt, &written)
290             ||  written != sizeof(simple1)
291             ||  memcmp(buf->data, &simple1, written) != 0) {
292         testfail("test_WPACKET_set_flags():2 failed\n", &pkt);
293         return 0;
294     }
295
296     /* Set packet to abandon non-zero length */
297     if (!WPACKET_init_len(&pkt, buf, 1)
298             || !WPACKET_set_flags(&pkt, WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH)
299             || !WPACKET_finish(&pkt)
300             || !WPACKET_get_total_written(&pkt, &written)
301             ||  written != 0) {
302         testfail("test_WPACKET_set_flags():3 failed\n", &pkt);
303         return 0;
304     }
305
306     /* Repeat above test but only abandon a sub-packet */
307     if (!WPACKET_init_len(&pkt, buf, 1)
308             || !WPACKET_start_sub_packet_u8(&pkt)
309             || !WPACKET_set_flags(&pkt, WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH)
310             || !WPACKET_close(&pkt)
311             || !WPACKET_finish(&pkt)
312             || !WPACKET_get_total_written(&pkt, &written)
313             ||  written != sizeof(empty)
314             ||  memcmp(buf->data, &empty, written) != 0) {
315         testfail("test_WPACKET_set_flags():4 failed\n", &pkt);
316         return 0;
317     }
318
319     /* And repeat with a non empty sub-packet */
320     if (!WPACKET_init(&pkt, buf)
321             || !WPACKET_start_sub_packet_u8(&pkt)
322             || !WPACKET_set_flags(&pkt, WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH)
323             || !WPACKET_put_bytes_u8(&pkt, 0xff)
324             || !WPACKET_close(&pkt)
325             || !WPACKET_finish(&pkt)
326             || !WPACKET_get_total_written(&pkt, &written)
327             ||  written != sizeof(simple2)
328             ||  memcmp(buf->data, &simple2, written) != 0) {
329         testfail("test_WPACKET_set_flags():5 failed\n", &pkt);
330         return 0;
331     }
332     return 1;
333 }
334
335 static int test_WPACKET_allocate_bytes(void)
336 {
337     WPACKET pkt;
338     size_t written;
339     unsigned char *bytes;
340
341     if (!WPACKET_init_len(&pkt, buf, 1)
342             || !WPACKET_allocate_bytes(&pkt, 2, &bytes)) {
343         testfail("test_WPACKET_allocate_bytes():1 failed\n", &pkt);
344         return 0;
345     }
346     bytes[0] = 0xfe;
347     bytes[1] = 0xff;
348     if (!WPACKET_finish(&pkt)
349             || !WPACKET_get_total_written(&pkt, &written)
350             ||  written != sizeof(alloc)
351             ||  memcmp(buf->data, &alloc, written) != 0) {
352         testfail("test_WPACKET_allocate_bytes():2 failed\n", &pkt);
353         return 0;
354     }
355
356     /* Repeat with WPACKET_sub_allocate_bytes */
357     if (!WPACKET_init_len(&pkt, buf, 1)
358             || !WPACKET_sub_allocate_bytes_u8(&pkt, 2, &bytes)) {
359         testfail("test_WPACKET_allocate_bytes():3 failed\n", &pkt);
360         return 0;
361     }
362     bytes[0] = 0xfe;
363     bytes[1] = 0xff;
364     if (!WPACKET_finish(&pkt)
365             || !WPACKET_get_total_written(&pkt, &written)
366             ||  written != sizeof(submem)
367             ||  memcmp(buf->data, &submem, written) != 0) {
368         testfail("test_WPACKET_allocate_bytes():4 failed\n", &pkt);
369         return 0;
370     }
371
372     return 1;
373 }
374
375 static int test_WPACKET_memcpy(void)
376 {
377     WPACKET pkt;
378     size_t written;
379     const unsigned char bytes[] = { 0xfe, 0xff };
380
381     if (!WPACKET_init_len(&pkt, buf, 1)
382             || !WPACKET_memcpy(&pkt, bytes, sizeof(bytes))
383             || !WPACKET_finish(&pkt)
384             || !WPACKET_get_total_written(&pkt, &written)
385             ||  written != sizeof(alloc)
386             ||  memcmp(buf->data, &alloc, written) != 0) {
387         testfail("test_WPACKET_memcpy():1 failed\n", &pkt);
388         return 0;
389     }
390
391     /* Repeat with WPACKET_sub_memcpy() */
392     if (!WPACKET_init_len(&pkt, buf, 1)
393             || !WPACKET_sub_memcpy_u8(&pkt, bytes, sizeof(bytes))
394             || !WPACKET_finish(&pkt)
395             || !WPACKET_get_total_written(&pkt, &written)
396             ||  written != sizeof(submem)
397             ||  memcmp(buf->data, &submem, written) != 0) {
398         testfail("test_WPACKET_memcpy():2 failed\n", &pkt);
399         return 0;
400     }
401
402     return 1;
403 }
404
405 int test_main(int argc, char *argv[])
406 {
407     int testresult = 0;
408
409     buf = BUF_MEM_new();
410     if (buf != NULL) {
411         ADD_TEST(test_WPACKET_init);
412         ADD_TEST(test_WPACKET_set_max_size);
413         ADD_TEST(test_WPACKET_start_sub_packet);
414         ADD_TEST(test_WPACKET_set_flags);
415         ADD_TEST(test_WPACKET_allocate_bytes);
416         ADD_TEST(test_WPACKET_memcpy);
417
418         testresult = run_tests(argv[0]);
419
420         BUF_MEM_free(buf);
421     }
422
423     return testresult;
424 }