]> git.ipfire.org Git - thirdparty/openssl.git/blob - test/packettest.c
Refactor ClientHello extension parsing
[thirdparty/openssl.git] / test / packettest.c
1 /*
2 * Written by Matt Caswell for the OpenSSL project.
3 */
4 /* ====================================================================
5 * Copyright (c) 2015 The OpenSSL Project. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * 3. All advertising materials mentioning features or use of this
20 * software must display the following acknowledgment:
21 * "This product includes software developed by the OpenSSL Project
22 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
23 *
24 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
25 * endorse or promote products derived from this software without
26 * prior written permission. For written permission, please contact
27 * openssl-core@openssl.org.
28 *
29 * 5. Products derived from this software may not be called "OpenSSL"
30 * nor may "OpenSSL" appear in their names without prior written
31 * permission of the OpenSSL Project.
32 *
33 * 6. Redistributions of any form whatsoever must retain the following
34 * acknowledgment:
35 * "This product includes software developed by the OpenSSL Project
36 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
37 *
38 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
39 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
41 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
42 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
47 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
49 * OF THE POSSIBILITY OF SUCH DAMAGE.
50 * ====================================================================
51 *
52 * This product includes cryptographic software written by Eric Young
53 * (eay@cryptsoft.com). This product includes software written by Tim
54 * Hudson (tjh@cryptsoft.com).
55 *
56 */
57
58
59 #include "../ssl/packet_locl.h"
60
61 #define BUF_LEN 255
62
63 static int test_PACKET_remaining(unsigned char buf[BUF_LEN])
64 {
65 PACKET pkt;
66
67 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
68 || PACKET_remaining(&pkt) != BUF_LEN
69 || !PACKET_forward(&pkt, BUF_LEN - 1)
70 || PACKET_remaining(&pkt) != 1
71 || !PACKET_forward(&pkt, 1)
72 || PACKET_remaining(&pkt) != 0) {
73 fprintf(stderr, "test_PACKET_remaining() failed\n");
74 return 0;
75 }
76
77 return 1;
78 }
79
80 static int test_PACKET_end(unsigned char buf[BUF_LEN])
81 {
82 PACKET pkt;
83
84 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
85 || PACKET_remaining(&pkt) != BUF_LEN
86 || PACKET_end(&pkt) != buf + BUF_LEN
87 || !PACKET_forward(&pkt, BUF_LEN - 1)
88 || PACKET_end(&pkt) != buf + BUF_LEN
89 || !PACKET_forward(&pkt, 1)
90 || PACKET_end(&pkt) != buf + BUF_LEN) {
91 fprintf(stderr, "test_PACKET_end() failed\n");
92 return 0;
93 }
94
95 return 1;
96 }
97
98 static int test_PACKET_get_1(unsigned char buf[BUF_LEN])
99 {
100 unsigned int i;
101 PACKET pkt;
102
103 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
104 || !PACKET_get_1(&pkt, &i)
105 || i != 0x02
106 || !PACKET_forward(&pkt, BUF_LEN - 2)
107 || !PACKET_get_1(&pkt, &i)
108 || i != 0xfe
109 || PACKET_get_1(&pkt, &i)) {
110 fprintf(stderr, "test_PACKET_get_1() failed\n");
111 return 0;
112 }
113
114 return 1;
115 }
116
117 static int test_PACKET_get_4(unsigned char buf[BUF_LEN])
118 {
119 unsigned long i;
120 PACKET pkt;
121
122 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
123 || !PACKET_get_4(&pkt, &i)
124 || i != 0x08060402UL
125 || !PACKET_forward(&pkt, BUF_LEN - 8)
126 || !PACKET_get_4(&pkt, &i)
127 || i != 0xfefcfaf8UL
128 || PACKET_get_4(&pkt, &i)) {
129 fprintf(stderr, "test_PACKET_get_4() failed\n");
130 return 0;
131 }
132
133 return 1;
134 }
135
136 static int test_PACKET_get_net_2(unsigned char buf[BUF_LEN])
137 {
138 unsigned int i;
139 PACKET pkt;
140
141 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
142 || !PACKET_get_net_2(&pkt, &i)
143 || i != 0x0204
144 || !PACKET_forward(&pkt, BUF_LEN - 4)
145 || !PACKET_get_net_2(&pkt, &i)
146 || i != 0xfcfe
147 || PACKET_get_net_2(&pkt, &i)) {
148 fprintf(stderr, "test_PACKET_get_net_2() failed\n");
149 return 0;
150 }
151
152 return 1;
153 }
154
155 static int test_PACKET_get_net_3(unsigned char buf[BUF_LEN])
156 {
157 unsigned long i;
158 PACKET pkt;
159
160 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
161 || !PACKET_get_net_3(&pkt, &i)
162 || i != 0x020406UL
163 || !PACKET_forward(&pkt, BUF_LEN - 6)
164 || !PACKET_get_net_3(&pkt, &i)
165 || i != 0xfafcfeUL
166 || PACKET_get_net_3(&pkt, &i)) {
167 fprintf(stderr, "test_PACKET_get_net_3() failed\n");
168 return 0;
169 }
170
171 return 1;
172 }
173
174 static int test_PACKET_get_net_4(unsigned char buf[BUF_LEN])
175 {
176 unsigned long i;
177 PACKET pkt;
178
179 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
180 || !PACKET_get_net_4(&pkt, &i)
181 || i != 0x02040608UL
182 || !PACKET_forward(&pkt, BUF_LEN - 8)
183 || !PACKET_get_net_4(&pkt, &i)
184 || i != 0xf8fafcfeUL
185 || PACKET_get_net_4(&pkt, &i)) {
186 fprintf(stderr, "test_PACKET_get_net_4() failed\n");
187 return 0;
188 }
189
190 return 1;
191 }
192
193 static int test_PACKET_get_sub_packet(unsigned char buf[BUF_LEN])
194 {
195 PACKET pkt, subpkt;
196 unsigned long i;
197
198 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
199 || !PACKET_get_sub_packet(&pkt, &subpkt, 4)
200 || !PACKET_get_net_4(&subpkt, &i)
201 || i != 0x02040608UL
202 || PACKET_remaining(&subpkt)
203 || !PACKET_forward(&pkt, BUF_LEN - 8)
204 || !PACKET_get_sub_packet(&pkt, &subpkt, 4)
205 || !PACKET_get_net_4(&subpkt, &i)
206 || i != 0xf8fafcfeUL
207 || PACKET_remaining(&subpkt)
208 || PACKET_get_sub_packet(&pkt, &subpkt, 4)) {
209 fprintf(stderr, "test_PACKET_get_sub_packet() failed\n");
210 return 0;
211 }
212
213 return 1;
214 }
215
216 static int test_PACKET_get_bytes(unsigned char buf[BUF_LEN])
217 {
218 const unsigned char *bytes;
219 PACKET pkt;
220
221 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
222 || !PACKET_get_bytes(&pkt, &bytes, 4)
223 || bytes[0] != 2 || bytes[1] != 4
224 || bytes[2] != 6 || bytes[3] != 8
225 || PACKET_remaining(&pkt) != BUF_LEN -4
226 || !PACKET_forward(&pkt, BUF_LEN - 8)
227 || !PACKET_get_bytes(&pkt, &bytes, 4)
228 || bytes[0] != 0xf8 || bytes[1] != 0xfa
229 || bytes[2] != 0xfc || bytes[3] != 0xfe
230 || PACKET_remaining(&pkt)) {
231 fprintf(stderr, "test_PACKET_get_bytes() failed\n");
232 return 0;
233 }
234
235 return 1;
236 }
237
238 static int test_PACKET_copy_bytes(unsigned char buf[BUF_LEN])
239 {
240 unsigned char bytes[4];
241 PACKET pkt;
242
243 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
244 || !PACKET_copy_bytes(&pkt, bytes, 4)
245 || bytes[0] != 2 || bytes[1] != 4
246 || bytes[2] != 6 || bytes[3] != 8
247 || PACKET_remaining(&pkt) != BUF_LEN - 4
248 || !PACKET_forward(&pkt, BUF_LEN - 8)
249 || !PACKET_copy_bytes(&pkt, bytes, 4)
250 || bytes[0] != 0xf8 || bytes[1] != 0xfa
251 || bytes[2] != 0xfc || bytes[3] != 0xfe
252 || PACKET_remaining(&pkt)) {
253 fprintf(stderr, "test_PACKET_copy_bytes() failed\n");
254 return 0;
255 }
256
257 return 1;
258 }
259
260 static int test_PACKET_copy_all(unsigned char buf[BUF_LEN])
261 {
262 unsigned char tmp[BUF_LEN];
263 PACKET pkt;
264 size_t len;
265
266 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
267 || !PACKET_copy_all(&pkt, tmp, BUF_LEN, &len)
268 || len != BUF_LEN
269 || memcmp(buf, tmp, BUF_LEN) != 0
270 || PACKET_remaining(&pkt) != BUF_LEN
271 || PACKET_copy_all(&pkt, tmp, BUF_LEN - 1, &len)) {
272 fprintf(stderr, "test_PACKET_copy_bytes() failed\n");
273 return 0;
274 }
275
276 return 1;
277 }
278
279 static int test_PACKET_memdup(unsigned char buf[BUF_LEN])
280 {
281 unsigned char *data = NULL;
282 size_t len;
283 PACKET pkt;
284
285 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
286 || !PACKET_memdup(&pkt, &data, &len)
287 || len != BUF_LEN
288 || memcmp(data, PACKET_data(&pkt), len)
289 || !PACKET_forward(&pkt, 10)
290 || !PACKET_memdup(&pkt, &data, &len)
291 || len != BUF_LEN - 10
292 || memcmp(data, PACKET_data(&pkt), len)) {
293 fprintf(stderr, "test_PACKET_memdup() failed\n");
294 OPENSSL_free(data);
295 return 0;
296 }
297
298 OPENSSL_free(data);
299 return 1;
300 }
301
302 static int test_PACKET_strndup()
303 {
304 char buf[10], buf2[10];
305 char *data = NULL;
306 PACKET pkt;
307
308 memset(buf, 'x', 10);
309 memset(buf2, 'y', 10);
310 buf2[5] = '\0';
311
312 if ( !PACKET_buf_init(&pkt, (unsigned char*)buf, 10)
313 || !PACKET_strndup(&pkt, &data)
314 || strlen(data) != 10
315 || strncmp(data, buf, 10)
316 || !PACKET_buf_init(&pkt, (unsigned char*)buf2, 10)
317 || !PACKET_strndup(&pkt, &data)
318 || strlen(data) != 5
319 || strcmp(data, buf2)) {
320 fprintf(stderr, "test_PACKET_strndup failed\n");
321 OPENSSL_free(data);
322 return 0;
323 }
324
325 OPENSSL_free(data);
326 return 1;
327 }
328
329 static int test_PACKET_contains_zero_byte()
330 {
331 char buf[10], buf2[10];
332 PACKET pkt;
333
334 memset(buf, 'x', 10);
335 memset(buf2, 'y', 10);
336 buf2[5] = '\0';
337
338 if ( !PACKET_buf_init(&pkt, (unsigned char*)buf, 10)
339 || PACKET_contains_zero_byte(&pkt)
340 || !PACKET_buf_init(&pkt, (unsigned char*)buf2, 10)
341 || !PACKET_contains_zero_byte(&pkt)) {
342 fprintf(stderr, "test_PACKET_contains_zero_byte failed\n");
343 return 0;
344 }
345
346 return 1;
347 }
348
349 static int test_PACKET_forward(unsigned char buf[BUF_LEN])
350 {
351 const unsigned char *byte;
352 PACKET pkt;
353
354 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
355 || !PACKET_forward(&pkt, 1)
356 || !PACKET_get_bytes(&pkt, &byte, 1)
357 || byte[0] != 4
358 || !PACKET_forward(&pkt, BUF_LEN - 3)
359 || !PACKET_get_bytes(&pkt, &byte, 1)
360 || byte[0] != 0xfe) {
361 fprintf(stderr, "test_PACKET_forward() failed\n");
362 return 0;
363 }
364
365 return 1;
366 }
367
368 static int test_PACKET_buf_init()
369 {
370 unsigned char buf[BUF_LEN];
371 PACKET pkt;
372
373 /* Also tests PACKET_remaining() */
374 if ( !PACKET_buf_init(&pkt, buf, 4)
375 || PACKET_remaining(&pkt) != 4
376 || !PACKET_buf_init(&pkt, buf, BUF_LEN)
377 || PACKET_remaining(&pkt) != BUF_LEN
378 || PACKET_buf_init(&pkt, buf, -1)) {
379 fprintf(stderr, "test_PACKET_buf_init() failed\n");
380 return 0;
381 }
382
383 return 1;
384 }
385
386 static int test_PACKET_null_init()
387 {
388 PACKET pkt;
389
390 PACKET_null_init(&pkt);
391 if ( PACKET_remaining(&pkt) != 0
392 || PACKET_forward(&pkt, 1)) {
393 fprintf(stderr, "test_PACKET_null_init() failed\n");
394 return 0;
395 }
396
397 return 1;
398 }
399
400 static int test_PACKET_equal(unsigned char buf[BUF_LEN])
401 {
402 PACKET pkt;
403
404 if ( !PACKET_buf_init(&pkt, buf, 4)
405 || !PACKET_equal(&pkt, buf, 4)
406 || PACKET_equal(&pkt, buf + 1, 4)
407 || !PACKET_buf_init(&pkt, buf, BUF_LEN)
408 || !PACKET_equal(&pkt, buf, BUF_LEN)
409 || PACKET_equal(&pkt, buf, BUF_LEN - 1)
410 || PACKET_equal(&pkt, buf, BUF_LEN + 1)
411 || PACKET_equal(&pkt, buf, 0)) {
412 fprintf(stderr, "test_PACKET_equal() failed\n");
413 return 0;
414 }
415
416 return 1;
417 }
418
419 static int test_PACKET_get_length_prefixed_1()
420 {
421 unsigned char buf[BUF_LEN];
422 const size_t len = 16;
423 unsigned int i;
424 PACKET pkt, short_pkt, subpkt;
425
426 buf[0] = len;
427 for (i = 1; i < BUF_LEN; i++) {
428 buf[i] = (i * 2) & 0xff;
429 }
430
431 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
432 || !PACKET_buf_init(&short_pkt, buf, len)
433 || !PACKET_get_length_prefixed_1(&pkt, &subpkt)
434 || PACKET_remaining(&subpkt) != len
435 || !PACKET_get_net_2(&subpkt, &i)
436 || i != 0x0204
437 || PACKET_get_length_prefixed_1(&short_pkt, &subpkt)
438 || PACKET_remaining(&short_pkt) != len) {
439 fprintf(stderr, "test_PACKET_get_length_prefixed_1() failed\n");
440 return 0;
441 }
442
443 return 1;
444 }
445
446 static int test_PACKET_get_length_prefixed_2()
447 {
448 unsigned char buf[1024];
449 const size_t len = 516; /* 0x0204 */
450 unsigned int i;
451 PACKET pkt, short_pkt, subpkt;
452
453 for (i = 1; i <= 1024; i++) {
454 buf[i-1] = (i * 2) & 0xff;
455 }
456
457 if ( !PACKET_buf_init(&pkt, buf, 1024)
458 || !PACKET_buf_init(&short_pkt, buf, len)
459 || !PACKET_get_length_prefixed_2(&pkt, &subpkt)
460 || PACKET_remaining(&subpkt) != len
461 || !PACKET_get_net_2(&subpkt, &i)
462 || i != 0x0608
463 || PACKET_get_length_prefixed_2(&short_pkt, &subpkt)
464 || PACKET_remaining(&short_pkt) != len) {
465 fprintf(stderr, "test_PACKET_get_length_prefixed_2() failed\n");
466 return 0;
467 }
468
469 return 1;
470 }
471
472 static int test_PACKET_get_length_prefixed_3()
473 {
474 unsigned char buf[1024];
475 const size_t len = 516; /* 0x000204 */
476 unsigned int i;
477 PACKET pkt, short_pkt, subpkt;
478
479 for (i = 0; i < 1024; i++) {
480 buf[i] = (i * 2) & 0xff;
481 }
482
483 if ( !PACKET_buf_init(&pkt, buf, 1024)
484 || !PACKET_buf_init(&short_pkt, buf, len)
485 || !PACKET_get_length_prefixed_3(&pkt, &subpkt)
486 || PACKET_remaining(&subpkt) != len
487 || !PACKET_get_net_2(&subpkt, &i)
488 || i != 0x0608
489 || PACKET_get_length_prefixed_3(&short_pkt, &subpkt)
490 || PACKET_remaining(&short_pkt) != len) {
491 fprintf(stderr, "test_PACKET_get_length_prefixed_3() failed\n");
492 return 0;
493 }
494
495 return 1;
496 }
497
498 static int test_PACKET_as_length_prefixed_1()
499 {
500 unsigned char buf[BUF_LEN];
501 const size_t len = 16;
502 unsigned int i;
503 PACKET pkt, exact_pkt, subpkt;
504
505 buf[0] = len;
506 for (i = 1; i < BUF_LEN; i++) {
507 buf[i] = (i * 2) & 0xff;
508 }
509
510 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
511 || !PACKET_buf_init(&exact_pkt, buf, len + 1)
512 || PACKET_as_length_prefixed_1(&pkt, &subpkt)
513 || PACKET_remaining(&pkt) != BUF_LEN
514 || !PACKET_as_length_prefixed_1(&exact_pkt, &subpkt)
515 || PACKET_remaining(&exact_pkt) != 0
516 || PACKET_remaining(&subpkt) != len) {
517 fprintf(stderr, "test_PACKET_as_length_prefixed_1() failed\n");
518 return 0;
519 }
520
521 return 1;
522 }
523
524 static int test_PACKET_as_length_prefixed_2()
525 {
526 unsigned char buf[1024];
527 const size_t len = 516; /* 0x0204 */
528 unsigned int i;
529 PACKET pkt, exact_pkt, subpkt;
530
531 for (i = 1; i <= 1024; i++) {
532 buf[i-1] = (i * 2) & 0xff;
533 }
534
535 if ( !PACKET_buf_init(&pkt, buf, 1024)
536 || !PACKET_buf_init(&exact_pkt, buf, len + 2)
537 || PACKET_as_length_prefixed_2(&pkt, &subpkt)
538 || PACKET_remaining(&pkt) != 1024
539 || !PACKET_as_length_prefixed_2(&exact_pkt, &subpkt)
540 || PACKET_remaining(&exact_pkt) != 0
541 || PACKET_remaining(&subpkt) != len) {
542 fprintf(stderr, "test_PACKET_as_length_prefixed_2() failed\n");
543 return 0;
544 }
545
546 return 1;
547 }
548
549 int main(int argc, char **argv)
550 {
551 unsigned char buf[BUF_LEN];
552 unsigned int i;
553
554 for (i=1; i<=BUF_LEN; i++) {
555 buf[i-1] = (i * 2) & 0xff;
556 }
557 i = 0;
558
559 if ( !test_PACKET_buf_init()
560 || !test_PACKET_null_init()
561 || !test_PACKET_remaining(buf)
562 || !test_PACKET_end(buf)
563 || !test_PACKET_equal(buf)
564 || !test_PACKET_get_1(buf)
565 || !test_PACKET_get_4(buf)
566 || !test_PACKET_get_net_2(buf)
567 || !test_PACKET_get_net_3(buf)
568 || !test_PACKET_get_net_4(buf)
569 || !test_PACKET_get_sub_packet(buf)
570 || !test_PACKET_get_bytes(buf)
571 || !test_PACKET_copy_bytes(buf)
572 || !test_PACKET_copy_all(buf)
573 || !test_PACKET_memdup(buf)
574 || !test_PACKET_strndup()
575 || !test_PACKET_contains_zero_byte()
576 || !test_PACKET_forward(buf)
577 || !test_PACKET_get_length_prefixed_1()
578 || !test_PACKET_get_length_prefixed_2()
579 || !test_PACKET_get_length_prefixed_3()
580 || !test_PACKET_as_length_prefixed_1()
581 || !test_PACKET_as_length_prefixed_2()) {
582 return 1;
583 }
584 printf("PASS\n");
585 return 0;
586 }