blob: 52c98271da26bef8b32f4ed856ff42c60d580aad [file] [log] [blame]
Matt Caswelld6c4cc22016-09-08 10:01:24 +01001/*
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"
Emilia Kaspere364c3b2016-11-07 16:53:15 +010014#include "test_main_custom.h"
Matt Caswelld6c4cc22016-09-08 10:01:24 +010015
16const static unsigned char simple1 = 0xff;
17const static unsigned char simple2[] = { 0x01, 0xff };
18const static unsigned char simple3[] = { 0x00, 0x00, 0x00, 0x01, 0xff };
19const static unsigned char nestedsub[] = { 0x03, 0xff, 0x01, 0xff };
20const static unsigned char seqsub[] = { 0x01, 0xff, 0x01, 0xff };
21const static unsigned char empty = 0x00;
22const static unsigned char alloc[] = { 0x02, 0xfe, 0xff };
23const static unsigned char submem[] = { 0x03, 0x02, 0xfe, 0xff };
Matt Caswell9b36b7d2016-11-08 10:33:35 +000024const static unsigned char fixed[] = { 0xff, 0xff, 0xff };
Matt Caswelld6c4cc22016-09-08 10:01:24 +010025
26static BUF_MEM *buf;
27
28static void testfail(const char *msg, WPACKET *pkt)
29{
30 fprintf(stderr, "%s", msg);
31 WPACKET_cleanup(pkt);
32}
33
34static int test_WPACKET_init(void)
35{
36 WPACKET pkt;
37 int i;
38 size_t written;
Matt Caswell9b36b7d2016-11-08 10:33:35 +000039 unsigned char sbuf[3];
Matt Caswelld6c4cc22016-09-08 10:01:24 +010040
Matt Caswellc9216d12016-09-13 14:17:09 +010041 if (!WPACKET_init(&pkt, buf)
Matt Caswell08029df2016-09-20 14:47:44 +010042 || !WPACKET_put_bytes_u8(&pkt, 0xff)
Matt Caswelld6c4cc22016-09-08 10:01:24 +010043 /* 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 */
Matt Caswellc9216d12016-09-13 14:17:09 +010061 if (!WPACKET_init_len(&pkt, buf, 1)
Matt Caswell08029df2016-09-20 14:47:44 +010062 || !WPACKET_put_bytes_u8(&pkt, 0xff)
Matt Caswelld6c4cc22016-09-08 10:01:24 +010063 || !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 */
Matt Caswellc9216d12016-09-13 14:17:09 +010072 if (!WPACKET_init_len(&pkt, buf, 4)
Matt Caswell08029df2016-09-20 14:47:44 +010073 || !WPACKET_put_bytes_u8(&pkt, 0xff)
Matt Caswelld6c4cc22016-09-08 10:01:24 +010074 || !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 */
Matt Caswell08029df2016-09-20 14:47:44 +010091 if ((!WPACKET_put_bytes_u8(&pkt, 0xff)) == (i != 256)) {
Matt Caswelld6c4cc22016-09-08 10:01:24 +010092 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
Matt Caswell9b36b7d2016-11-08 10:33:35 +0000101 /* 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
Matt Caswelld6c4cc22016-09-08 10:01:24 +0100126 return 1;
127}
128
Matt Caswelld6c4cc22016-09-08 10:01:24 +0100129static int test_WPACKET_set_max_size(void)
130{
131 WPACKET pkt;
132 size_t written;
Matt Caswelld6c4cc22016-09-08 10:01:24 +0100133
Matt Caswellc9216d12016-09-13 14:17:09 +0100134 if (!WPACKET_init(&pkt, buf)
Matt Caswelld6c4cc22016-09-08 10:01:24 +0100135 /*
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)
Matt Caswellde451852016-09-09 00:13:41 +0100144 || !WPACKET_finish(&pkt)) {
Matt Caswelld6c4cc22016-09-08 10:01:24 +0100145 testfail("test_WPACKET_set_max_size():1 failed\n", &pkt);
David Benjamin609b0852016-10-10 12:01:24 -0400146 return 0;
Matt Caswelld6c4cc22016-09-08 10:01:24 +0100147 }
148
Matt Caswellc9216d12016-09-13 14:17:09 +0100149 if (!WPACKET_init_len(&pkt, buf, 1)
Matt Caswelld6c4cc22016-09-08 10:01:24 +0100150 /*
151 * Should fail because we already consumed 1 byte with the
152 * length
153 */
154 || WPACKET_set_max_size(&pkt, 0)
Matt Caswellde451852016-09-09 00:13:41 +0100155 /*
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 */
Matt Caswell08029df2016-09-20 14:47:44 +0100167 || WPACKET_put_bytes_u8(&pkt, 0xff)
Matt Caswellde451852016-09-09 00:13:41 +0100168 /*
169 * You can't put in more bytes than max size
170 */
171 || !WPACKET_set_max_size(&pkt, 0x02)
Matt Caswell08029df2016-09-20 14:47:44 +0100172 || !WPACKET_put_bytes_u8(&pkt, 0xff)
173 || WPACKET_put_bytes_u8(&pkt, 0xff)
Matt Caswelld6c4cc22016-09-08 10:01:24 +0100174 || !WPACKET_finish(&pkt)
175 || !WPACKET_get_total_written(&pkt, &written)
176 || written != sizeof(simple2)
Matt Caswellde451852016-09-09 00:13:41 +0100177 || memcmp(buf->data, &simple2, written) != 0) {
Matt Caswelld6c4cc22016-09-08 10:01:24 +0100178 testfail("test_WPACKET_set_max_size():2 failed\n", &pkt);
179 return 0;
180 }
181
182 return 1;
183}
184
185static int test_WPACKET_start_sub_packet(void)
186{
187 WPACKET pkt;
188 size_t written;
189 size_t len;
190
Matt Caswellc9216d12016-09-13 14:17:09 +0100191 if (!WPACKET_init(&pkt, buf)
Matt Caswelld6c4cc22016-09-08 10:01:24 +0100192 || !WPACKET_start_sub_packet(&pkt)
Matt Caswell08029df2016-09-20 14:47:44 +0100193 || !WPACKET_put_bytes_u8(&pkt, 0xff)
Matt Caswelld6c4cc22016-09-08 10:01:24 +0100194 /* 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 */
Matt Caswellc9216d12016-09-13 14:17:09 +0100209 if (!WPACKET_init(&pkt, buf)
Matt Caswell869d0a32016-09-13 15:42:12 +0100210 || !WPACKET_start_sub_packet_u8(&pkt)
Matt Caswell08029df2016-09-20 14:47:44 +0100211 || !WPACKET_put_bytes_u8(&pkt, 0xff)
Matt Caswelld6c4cc22016-09-08 10:01:24 +0100212 || !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 */
Matt Caswellc9216d12016-09-13 14:17:09 +0100222 if (!WPACKET_init(&pkt, buf)
Matt Caswell869d0a32016-09-13 15:42:12 +0100223 || !WPACKET_start_sub_packet_u8(&pkt)
Matt Caswell08029df2016-09-20 14:47:44 +0100224 || !WPACKET_put_bytes_u8(&pkt, 0xff)
Matt Caswell869d0a32016-09-13 15:42:12 +0100225 || !WPACKET_start_sub_packet_u8(&pkt)
Matt Caswell08029df2016-09-20 14:47:44 +0100226 || !WPACKET_put_bytes_u8(&pkt, 0xff)
Matt Caswelld6c4cc22016-09-08 10:01:24 +0100227 || !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 */
Matt Caswellc9216d12016-09-13 14:17:09 +0100242 if (!WPACKET_init(&pkt, buf)
Matt Caswell869d0a32016-09-13 15:42:12 +0100243 || !WPACKET_start_sub_packet_u8(&pkt)
Matt Caswell08029df2016-09-20 14:47:44 +0100244 || !WPACKET_put_bytes_u8(&pkt, 0xff)
Matt Caswelld6c4cc22016-09-08 10:01:24 +0100245 || !WPACKET_close(&pkt)
Matt Caswell869d0a32016-09-13 15:42:12 +0100246 || !WPACKET_start_sub_packet_u8(&pkt)
Matt Caswell08029df2016-09-20 14:47:44 +0100247 || !WPACKET_put_bytes_u8(&pkt, 0xff)
Matt Caswelld6c4cc22016-09-08 10:01:24 +0100248 || !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
261static int test_WPACKET_set_flags(void)
262{
263 WPACKET pkt;
264 size_t written;
265
266 /* Set packet to be non-zero length */
Matt Caswellc9216d12016-09-13 14:17:09 +0100267 if (!WPACKET_init(&pkt, buf)
Matt Caswellde451852016-09-09 00:13:41 +0100268 || !WPACKET_set_flags(&pkt, WPACKET_FLAGS_NON_ZERO_LENGTH)
Matt Caswelld6c4cc22016-09-08 10:01:24 +0100269 /* Should fail because of zero length */
270 || WPACKET_finish(&pkt)
Matt Caswell08029df2016-09-20 14:47:44 +0100271 || !WPACKET_put_bytes_u8(&pkt, 0xff)
Matt Caswelld6c4cc22016-09-08 10:01:24 +0100272 || !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 */
Matt Caswellc9216d12016-09-13 14:17:09 +0100281 if (!WPACKET_init(&pkt, buf)
Matt Caswelld6c4cc22016-09-08 10:01:24 +0100282 || !WPACKET_start_sub_packet(&pkt)
Matt Caswellde451852016-09-09 00:13:41 +0100283 || !WPACKET_set_flags(&pkt, WPACKET_FLAGS_NON_ZERO_LENGTH)
Matt Caswelld6c4cc22016-09-08 10:01:24 +0100284 /* Should fail because of zero length */
285 || WPACKET_close(&pkt)
Matt Caswell08029df2016-09-20 14:47:44 +0100286 || !WPACKET_put_bytes_u8(&pkt, 0xff)
Matt Caswelld6c4cc22016-09-08 10:01:24 +0100287 || !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 */
Matt Caswellc9216d12016-09-13 14:17:09 +0100297 if (!WPACKET_init_len(&pkt, buf, 1)
Matt Caswellde451852016-09-09 00:13:41 +0100298 || !WPACKET_set_flags(&pkt, WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH)
Matt Caswelld6c4cc22016-09-08 10:01:24 +0100299 || !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 */
Matt Caswellc9216d12016-09-13 14:17:09 +0100307 if (!WPACKET_init_len(&pkt, buf, 1)
Matt Caswell869d0a32016-09-13 15:42:12 +0100308 || !WPACKET_start_sub_packet_u8(&pkt)
Matt Caswellde451852016-09-09 00:13:41 +0100309 || !WPACKET_set_flags(&pkt, WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH)
Matt Caswelld6c4cc22016-09-08 10:01:24 +0100310 || !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 */
Matt Caswellc9216d12016-09-13 14:17:09 +0100320 if (!WPACKET_init(&pkt, buf)
Matt Caswell869d0a32016-09-13 15:42:12 +0100321 || !WPACKET_start_sub_packet_u8(&pkt)
Matt Caswellde451852016-09-09 00:13:41 +0100322 || !WPACKET_set_flags(&pkt, WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH)
Matt Caswell08029df2016-09-20 14:47:44 +0100323 || !WPACKET_put_bytes_u8(&pkt, 0xff)
Matt Caswelld6c4cc22016-09-08 10:01:24 +0100324 || !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
335static int test_WPACKET_allocate_bytes(void)
336{
337 WPACKET pkt;
338 size_t written;
339 unsigned char *bytes;
340
Matt Caswellc9216d12016-09-13 14:17:09 +0100341 if (!WPACKET_init_len(&pkt, buf, 1)
Matt Caswelld6c4cc22016-09-08 10:01:24 +0100342 || !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;
Matt Caswellc9216d12016-09-13 14:17:09 +0100348 if (!WPACKET_finish(&pkt)
Matt Caswelld6c4cc22016-09-08 10:01:24 +0100349 || !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
Matt Caswellb2b30242016-09-13 11:32:52 +0100356 /* Repeat with WPACKET_sub_allocate_bytes */
Matt Caswellc9216d12016-09-13 14:17:09 +0100357 if (!WPACKET_init_len(&pkt, buf, 1)
Matt Caswell869d0a32016-09-13 15:42:12 +0100358 || !WPACKET_sub_allocate_bytes_u8(&pkt, 2, &bytes)) {
Matt Caswellb2b30242016-09-13 11:32:52 +0100359 testfail("test_WPACKET_allocate_bytes():3 failed\n", &pkt);
360 return 0;
361 }
362 bytes[0] = 0xfe;
363 bytes[1] = 0xff;
Matt Caswellc9216d12016-09-13 14:17:09 +0100364 if (!WPACKET_finish(&pkt)
Matt Caswellb2b30242016-09-13 11:32:52 +0100365 || !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
Matt Caswelld6c4cc22016-09-08 10:01:24 +0100372 return 1;
373}
374
375static int test_WPACKET_memcpy(void)
376{
377 WPACKET pkt;
378 size_t written;
379 const unsigned char bytes[] = { 0xfe, 0xff };
380
Matt Caswellc9216d12016-09-13 14:17:09 +0100381 if (!WPACKET_init_len(&pkt, buf, 1)
Matt Caswelld6c4cc22016-09-08 10:01:24 +0100382 || !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() */
Matt Caswellc9216d12016-09-13 14:17:09 +0100392 if (!WPACKET_init_len(&pkt, buf, 1)
Matt Caswell869d0a32016-09-13 15:42:12 +0100393 || !WPACKET_sub_memcpy_u8(&pkt, bytes, sizeof(bytes))
Matt Caswelld6c4cc22016-09-08 10:01:24 +0100394 || !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
Emilia Kaspere364c3b2016-11-07 16:53:15 +0100405int test_main(int argc, char *argv[])
Matt Caswelld6c4cc22016-09-08 10:01:24 +0100406{
Matt Caswelld6c4cc22016-09-08 10:01:24 +0100407 int testresult = 0;
408
Matt Caswelld6c4cc22016-09-08 10:01:24 +0100409 buf = BUF_MEM_new();
410 if (buf != NULL) {
411 ADD_TEST(test_WPACKET_init);
Matt Caswelld6c4cc22016-09-08 10:01:24 +0100412 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
Matt Caswelld6c4cc22016-09-08 10:01:24 +0100423 return testresult;
424}