]>
Commit | Line | Data |
---|---|---|
d6c4cc29 MC |
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" | |
e364c3b2 | 14 | #include "test_main_custom.h" |
d6c4cc29 MC |
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 }; | |
9b36b7d9 | 24 | const static unsigned char fixed[] = { 0xff, 0xff, 0xff }; |
d6c4cc29 MC |
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; | |
9b36b7d9 | 39 | unsigned char sbuf[3]; |
d6c4cc29 | 40 | |
c9216d14 | 41 | if (!WPACKET_init(&pkt, buf) |
08029dfa | 42 | || !WPACKET_put_bytes_u8(&pkt, 0xff) |
d6c4cc29 MC |
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 */ | |
c9216d14 | 61 | if (!WPACKET_init_len(&pkt, buf, 1) |
08029dfa | 62 | || !WPACKET_put_bytes_u8(&pkt, 0xff) |
d6c4cc29 MC |
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 */ | |
c9216d14 | 72 | if (!WPACKET_init_len(&pkt, buf, 4) |
08029dfa | 73 | || !WPACKET_put_bytes_u8(&pkt, 0xff) |
d6c4cc29 MC |
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 | */ | |
08029dfa | 91 | if ((!WPACKET_put_bytes_u8(&pkt, 0xff)) == (i != 256)) { |
d6c4cc29 MC |
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 | ||
9b36b7d9 MC |
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 | ||
d6c4cc29 MC |
126 | return 1; |
127 | } | |
128 | ||
d6c4cc29 MC |
129 | static int test_WPACKET_set_max_size(void) |
130 | { | |
131 | WPACKET pkt; | |
132 | size_t written; | |
d6c4cc29 | 133 | |
c9216d14 | 134 | if (!WPACKET_init(&pkt, buf) |
d6c4cc29 MC |
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) | |
de451856 MC |
144 | || !WPACKET_finish(&pkt)) { |
145 | testfail("test_WPACKET_set_max_size():1 failed\n", &pkt); | |
609b0852 | 146 | return 0; |
de451856 MC |
147 | } |
148 | ||
c9216d14 | 149 | if (!WPACKET_init_len(&pkt, buf, 1) |
de451856 MC |
150 | /* |
151 | * Should fail because we already consumed 1 byte with the | |
152 | * length | |
153 | */ | |
154 | || WPACKET_set_max_size(&pkt, 0) | |
d6c4cc29 MC |
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 */ | |
de451856 | 161 | || !WPACKET_set_max_size(&pkt, 0x0100) |
d6c4cc29 | 162 | /* Or it can be less */ |
de451856 | 163 | || !WPACKET_set_max_size(&pkt, 0x01) |
d6c4cc29 MC |
164 | /* |
165 | * Should fail because packet is already filled | |
166 | */ | |
08029dfa | 167 | || WPACKET_put_bytes_u8(&pkt, 0xff) |
d6c4cc29 MC |
168 | /* |
169 | * You can't put in more bytes than max size | |
170 | */ | |
de451856 | 171 | || !WPACKET_set_max_size(&pkt, 0x02) |
08029dfa MC |
172 | || !WPACKET_put_bytes_u8(&pkt, 0xff) |
173 | || WPACKET_put_bytes_u8(&pkt, 0xff) | |
d6c4cc29 MC |
174 | || !WPACKET_finish(&pkt) |
175 | || !WPACKET_get_total_written(&pkt, &written) | |
176 | || written != sizeof(simple2) | |
de451856 | 177 | || memcmp(buf->data, &simple2, written) != 0) { |
d6c4cc29 MC |
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 | ||
c9216d14 | 191 | if (!WPACKET_init(&pkt, buf) |
d6c4cc29 | 192 | || !WPACKET_start_sub_packet(&pkt) |
08029dfa | 193 | || !WPACKET_put_bytes_u8(&pkt, 0xff) |
d6c4cc29 MC |
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 */ | |
c9216d14 | 209 | if (!WPACKET_init(&pkt, buf) |
869d0a37 | 210 | || !WPACKET_start_sub_packet_u8(&pkt) |
08029dfa | 211 | || !WPACKET_put_bytes_u8(&pkt, 0xff) |
d6c4cc29 MC |
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 */ | |
c9216d14 | 222 | if (!WPACKET_init(&pkt, buf) |
869d0a37 | 223 | || !WPACKET_start_sub_packet_u8(&pkt) |
08029dfa | 224 | || !WPACKET_put_bytes_u8(&pkt, 0xff) |
869d0a37 | 225 | || !WPACKET_start_sub_packet_u8(&pkt) |
08029dfa | 226 | || !WPACKET_put_bytes_u8(&pkt, 0xff) |
d6c4cc29 MC |
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 */ | |
c9216d14 | 242 | if (!WPACKET_init(&pkt, buf) |
869d0a37 | 243 | || !WPACKET_start_sub_packet_u8(&pkt) |
08029dfa | 244 | || !WPACKET_put_bytes_u8(&pkt, 0xff) |
d6c4cc29 | 245 | || !WPACKET_close(&pkt) |
869d0a37 | 246 | || !WPACKET_start_sub_packet_u8(&pkt) |
08029dfa | 247 | || !WPACKET_put_bytes_u8(&pkt, 0xff) |
d6c4cc29 MC |
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 | ||
34254342 MC |
257 | /* Nested sub-packets with lengths filled before finish */ |
258 | if (!WPACKET_init(&pkt, buf) | |
259 | || !WPACKET_start_sub_packet_u8(&pkt) | |
260 | || !WPACKET_put_bytes_u8(&pkt, 0xff) | |
261 | || !WPACKET_start_sub_packet_u8(&pkt) | |
262 | || !WPACKET_put_bytes_u8(&pkt, 0xff) | |
263 | || !WPACKET_get_length(&pkt, &len) | |
264 | || len != 1 | |
265 | || !WPACKET_close(&pkt) | |
266 | || !WPACKET_get_length(&pkt, &len) | |
267 | || len != 3 | |
268 | || !WPACKET_close(&pkt) | |
269 | || !WPACKET_fill_lengths(&pkt) | |
270 | || !WPACKET_get_total_written(&pkt, &written) | |
271 | || written != sizeof(nestedsub) | |
272 | || memcmp(buf->data, &nestedsub, written) != 0 | |
273 | || !WPACKET_finish(&pkt)) { | |
274 | testfail("test_WPACKET_start_sub_packet():5 failed\n", &pkt); | |
275 | return 0; | |
276 | } | |
277 | ||
d6c4cc29 MC |
278 | return 1; |
279 | } | |
280 | ||
281 | ||
282 | static int test_WPACKET_set_flags(void) | |
283 | { | |
284 | WPACKET pkt; | |
285 | size_t written; | |
286 | ||
287 | /* Set packet to be non-zero length */ | |
c9216d14 | 288 | if (!WPACKET_init(&pkt, buf) |
de451856 | 289 | || !WPACKET_set_flags(&pkt, WPACKET_FLAGS_NON_ZERO_LENGTH) |
d6c4cc29 MC |
290 | /* Should fail because of zero length */ |
291 | || WPACKET_finish(&pkt) | |
08029dfa | 292 | || !WPACKET_put_bytes_u8(&pkt, 0xff) |
d6c4cc29 MC |
293 | || !WPACKET_finish(&pkt) |
294 | || !WPACKET_get_total_written(&pkt, &written) | |
295 | || written != sizeof(simple1) | |
296 | || memcmp(buf->data, &simple1, written) != 0) { | |
297 | testfail("test_WPACKET_set_flags():1 failed\n", &pkt); | |
298 | return 0; | |
299 | } | |
300 | ||
301 | /* Repeat above test in a sub-packet */ | |
c9216d14 | 302 | if (!WPACKET_init(&pkt, buf) |
d6c4cc29 | 303 | || !WPACKET_start_sub_packet(&pkt) |
de451856 | 304 | || !WPACKET_set_flags(&pkt, WPACKET_FLAGS_NON_ZERO_LENGTH) |
d6c4cc29 MC |
305 | /* Should fail because of zero length */ |
306 | || WPACKET_close(&pkt) | |
08029dfa | 307 | || !WPACKET_put_bytes_u8(&pkt, 0xff) |
d6c4cc29 MC |
308 | || !WPACKET_close(&pkt) |
309 | || !WPACKET_finish(&pkt) | |
310 | || !WPACKET_get_total_written(&pkt, &written) | |
311 | || written != sizeof(simple1) | |
312 | || memcmp(buf->data, &simple1, written) != 0) { | |
313 | testfail("test_WPACKET_set_flags():2 failed\n", &pkt); | |
314 | return 0; | |
315 | } | |
316 | ||
317 | /* Set packet to abandon non-zero length */ | |
c9216d14 | 318 | if (!WPACKET_init_len(&pkt, buf, 1) |
de451856 | 319 | || !WPACKET_set_flags(&pkt, WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH) |
d6c4cc29 MC |
320 | || !WPACKET_finish(&pkt) |
321 | || !WPACKET_get_total_written(&pkt, &written) | |
322 | || written != 0) { | |
323 | testfail("test_WPACKET_set_flags():3 failed\n", &pkt); | |
324 | return 0; | |
325 | } | |
326 | ||
327 | /* Repeat above test but only abandon a sub-packet */ | |
c9216d14 | 328 | if (!WPACKET_init_len(&pkt, buf, 1) |
869d0a37 | 329 | || !WPACKET_start_sub_packet_u8(&pkt) |
de451856 | 330 | || !WPACKET_set_flags(&pkt, WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH) |
d6c4cc29 MC |
331 | || !WPACKET_close(&pkt) |
332 | || !WPACKET_finish(&pkt) | |
333 | || !WPACKET_get_total_written(&pkt, &written) | |
334 | || written != sizeof(empty) | |
335 | || memcmp(buf->data, &empty, written) != 0) { | |
336 | testfail("test_WPACKET_set_flags():4 failed\n", &pkt); | |
337 | return 0; | |
338 | } | |
339 | ||
340 | /* And repeat with a non empty sub-packet */ | |
c9216d14 | 341 | if (!WPACKET_init(&pkt, buf) |
869d0a37 | 342 | || !WPACKET_start_sub_packet_u8(&pkt) |
de451856 | 343 | || !WPACKET_set_flags(&pkt, WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH) |
08029dfa | 344 | || !WPACKET_put_bytes_u8(&pkt, 0xff) |
d6c4cc29 MC |
345 | || !WPACKET_close(&pkt) |
346 | || !WPACKET_finish(&pkt) | |
347 | || !WPACKET_get_total_written(&pkt, &written) | |
348 | || written != sizeof(simple2) | |
349 | || memcmp(buf->data, &simple2, written) != 0) { | |
350 | testfail("test_WPACKET_set_flags():5 failed\n", &pkt); | |
351 | return 0; | |
352 | } | |
353 | return 1; | |
354 | } | |
355 | ||
356 | static int test_WPACKET_allocate_bytes(void) | |
357 | { | |
358 | WPACKET pkt; | |
359 | size_t written; | |
360 | unsigned char *bytes; | |
361 | ||
c9216d14 | 362 | if (!WPACKET_init_len(&pkt, buf, 1) |
d6c4cc29 MC |
363 | || !WPACKET_allocate_bytes(&pkt, 2, &bytes)) { |
364 | testfail("test_WPACKET_allocate_bytes():1 failed\n", &pkt); | |
365 | return 0; | |
366 | } | |
367 | bytes[0] = 0xfe; | |
368 | bytes[1] = 0xff; | |
c9216d14 | 369 | if (!WPACKET_finish(&pkt) |
d6c4cc29 MC |
370 | || !WPACKET_get_total_written(&pkt, &written) |
371 | || written != sizeof(alloc) | |
372 | || memcmp(buf->data, &alloc, written) != 0) { | |
373 | testfail("test_WPACKET_allocate_bytes():2 failed\n", &pkt); | |
374 | return 0; | |
375 | } | |
376 | ||
b2b3024e | 377 | /* Repeat with WPACKET_sub_allocate_bytes */ |
c9216d14 | 378 | if (!WPACKET_init_len(&pkt, buf, 1) |
869d0a37 | 379 | || !WPACKET_sub_allocate_bytes_u8(&pkt, 2, &bytes)) { |
b2b3024e MC |
380 | testfail("test_WPACKET_allocate_bytes():3 failed\n", &pkt); |
381 | return 0; | |
382 | } | |
383 | bytes[0] = 0xfe; | |
384 | bytes[1] = 0xff; | |
c9216d14 | 385 | if (!WPACKET_finish(&pkt) |
b2b3024e MC |
386 | || !WPACKET_get_total_written(&pkt, &written) |
387 | || written != sizeof(submem) | |
388 | || memcmp(buf->data, &submem, written) != 0) { | |
389 | testfail("test_WPACKET_allocate_bytes():4 failed\n", &pkt); | |
390 | return 0; | |
391 | } | |
392 | ||
d6c4cc29 MC |
393 | return 1; |
394 | } | |
395 | ||
396 | static int test_WPACKET_memcpy(void) | |
397 | { | |
398 | WPACKET pkt; | |
399 | size_t written; | |
400 | const unsigned char bytes[] = { 0xfe, 0xff }; | |
401 | ||
c9216d14 | 402 | if (!WPACKET_init_len(&pkt, buf, 1) |
d6c4cc29 MC |
403 | || !WPACKET_memcpy(&pkt, bytes, sizeof(bytes)) |
404 | || !WPACKET_finish(&pkt) | |
405 | || !WPACKET_get_total_written(&pkt, &written) | |
406 | || written != sizeof(alloc) | |
407 | || memcmp(buf->data, &alloc, written) != 0) { | |
408 | testfail("test_WPACKET_memcpy():1 failed\n", &pkt); | |
409 | return 0; | |
410 | } | |
411 | ||
412 | /* Repeat with WPACKET_sub_memcpy() */ | |
c9216d14 | 413 | if (!WPACKET_init_len(&pkt, buf, 1) |
869d0a37 | 414 | || !WPACKET_sub_memcpy_u8(&pkt, bytes, sizeof(bytes)) |
d6c4cc29 MC |
415 | || !WPACKET_finish(&pkt) |
416 | || !WPACKET_get_total_written(&pkt, &written) | |
417 | || written != sizeof(submem) | |
418 | || memcmp(buf->data, &submem, written) != 0) { | |
419 | testfail("test_WPACKET_memcpy():2 failed\n", &pkt); | |
420 | return 0; | |
421 | } | |
422 | ||
423 | return 1; | |
424 | } | |
425 | ||
e364c3b2 | 426 | int test_main(int argc, char *argv[]) |
d6c4cc29 | 427 | { |
d6c4cc29 MC |
428 | int testresult = 0; |
429 | ||
d6c4cc29 MC |
430 | buf = BUF_MEM_new(); |
431 | if (buf != NULL) { | |
432 | ADD_TEST(test_WPACKET_init); | |
d6c4cc29 MC |
433 | ADD_TEST(test_WPACKET_set_max_size); |
434 | ADD_TEST(test_WPACKET_start_sub_packet); | |
435 | ADD_TEST(test_WPACKET_set_flags); | |
436 | ADD_TEST(test_WPACKET_allocate_bytes); | |
437 | ADD_TEST(test_WPACKET_memcpy); | |
438 | ||
439 | testresult = run_tests(argv[0]); | |
440 | ||
441 | BUF_MEM_free(buf); | |
442 | } | |
443 | ||
d6c4cc29 MC |
444 | return testresult; |
445 | } |