xref: /freebsd/crypto/openssl/test/quic_wire_test.c (revision f25b8c9fb4f58cf61adb47d7570abe7caa6d385d)
1 /*
2  * Copyright 2022-2025 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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 "internal/packet.h"
11 #include "internal/quic_wire.h"
12 #include "internal/quic_wire_pkt.h"
13 #include "testutil.h"
14 
15 struct encode_test_case {
16     int (*serializer)(WPACKET *pkt);
17     const unsigned char *expect_buf;
18     size_t expect_buf_len;
19     /*
20      * fail: -1 if not truncated (function should test for success), else number
21      * of bytes to which the input has been truncated (function should test that
22      * decoding fails)
23      */
24     int (*deserializer)(PACKET *pkt, ossl_ssize_t fail);
25 };
26 
27 /* 1. PADDING */
encode_case_1_enc(WPACKET * pkt)28 static int encode_case_1_enc(WPACKET *pkt)
29 {
30     if (!TEST_int_eq(ossl_quic_wire_encode_padding(pkt, 3), 1))
31         return 0;
32 
33     return 1;
34 }
35 
encode_case_1_dec(PACKET * pkt,ossl_ssize_t fail)36 static int encode_case_1_dec(PACKET *pkt, ossl_ssize_t fail)
37 {
38     if (fail >= 0)
39         /* No failure modes for padding */
40         return 1;
41 
42     if (!TEST_int_eq(ossl_quic_wire_decode_padding(pkt), 3))
43         return 0;
44 
45     return 1;
46 }
47 
48 static const unsigned char encode_case_1_expect[] = {
49     0, 0, 0
50 };
51 
52 /* 2. PING */
encode_case_2_enc(WPACKET * pkt)53 static int encode_case_2_enc(WPACKET *pkt)
54 {
55 
56     if (!TEST_int_eq(ossl_quic_wire_encode_frame_ping(pkt), 1))
57         return 0;
58 
59     return 1;
60 }
61 
encode_case_2_dec(PACKET * pkt,ossl_ssize_t fail)62 static int encode_case_2_dec(PACKET *pkt, ossl_ssize_t fail)
63 {
64 
65     if (!TEST_int_eq(ossl_quic_wire_decode_frame_ping(pkt), fail < 0))
66         return 0;
67 
68     return 1;
69 }
70 
71 static const unsigned char encode_case_2_expect[] = {
72     0x01
73 };
74 
75 /* 3. ACK */
76 static const OSSL_QUIC_ACK_RANGE encode_case_3_ranges[] = {
77     { 20, 30 },
78     { 0, 10 }
79 };
80 
81 static const OSSL_QUIC_FRAME_ACK encode_case_3_f = {
82     (OSSL_QUIC_ACK_RANGE *)encode_case_3_ranges,
83     OSSL_NELEM(encode_case_3_ranges),
84     { OSSL_TIME_MS },
85     60, 70, 80, 1
86 };
87 
encode_case_3_enc(WPACKET * pkt)88 static int encode_case_3_enc(WPACKET *pkt)
89 {
90     if (!TEST_int_eq(ossl_quic_wire_encode_frame_ack(pkt, 3, &encode_case_3_f), 1))
91         return 0;
92 
93     return 1;
94 }
95 
encode_case_3_dec(PACKET * pkt,ossl_ssize_t fail)96 static int encode_case_3_dec(PACKET *pkt, ossl_ssize_t fail)
97 {
98     OSSL_QUIC_ACK_RANGE ranges[4] = { 0 };
99     OSSL_QUIC_FRAME_ACK f = { 0 };
100     uint64_t total_ranges = 0, peek_total_ranges = 0;
101     int ret;
102 
103     f.ack_ranges = ranges;
104     f.num_ack_ranges = OSSL_NELEM(ranges);
105 
106     ret = ossl_quic_wire_peek_frame_ack_num_ranges(pkt, &peek_total_ranges);
107     if (fail < 0 && !TEST_int_eq(ret, 1))
108         return 0;
109 
110     if (!TEST_int_eq(ossl_quic_wire_decode_frame_ack(pkt, 3, &f, &total_ranges), fail < 0))
111         return 0;
112 
113     if (ret == 1 && !TEST_uint64_t_eq(peek_total_ranges, 2))
114         return 0;
115 
116     if (fail >= 0)
117         return 1;
118 
119     if (!TEST_uint64_t_eq(total_ranges, peek_total_ranges))
120         return 0;
121 
122     if (!TEST_uint64_t_le(f.num_ack_ranges * sizeof(OSSL_QUIC_ACK_RANGE),
123             SIZE_MAX)
124         || !TEST_uint64_t_le(encode_case_3_f.num_ack_ranges
125                 * sizeof(OSSL_QUIC_ACK_RANGE),
126             SIZE_MAX))
127         return 0;
128 
129     if (!TEST_mem_eq(f.ack_ranges,
130             (size_t)f.num_ack_ranges * sizeof(OSSL_QUIC_ACK_RANGE),
131             encode_case_3_f.ack_ranges,
132             (size_t)encode_case_3_f.num_ack_ranges * sizeof(OSSL_QUIC_ACK_RANGE)))
133         return 0;
134 
135     if (!TEST_uint64_t_eq(ossl_time2ticks(f.delay_time),
136             ossl_time2ticks(encode_case_3_f.delay_time)))
137         return 0;
138 
139     if (!TEST_true(f.ecn_present))
140         return 0;
141 
142     if (!TEST_uint64_t_eq(f.ect0, encode_case_3_f.ect0))
143         return 0;
144 
145     if (!TEST_uint64_t_eq(f.ect1, encode_case_3_f.ect1))
146         return 0;
147 
148     if (!TEST_uint64_t_eq(f.ecnce, encode_case_3_f.ecnce))
149         return 0;
150 
151     return 1;
152 }
153 
154 static const unsigned char encode_case_3_expect[] = {
155     0x03, /* Type */
156     0x1E, /* Largest Acknowledged */
157     0x40,
158     0x7d, /* ACK Delay */
159     1, /* ACK Range Count */
160     10, /* First ACK Range */
161 
162     8, /* Gap */
163     10, /* Length */
164 
165     0x3c, /* ECT0 Count */
166     0x40,
167     0x46, /* ECT1 Count */
168     0x40,
169     0x50, /* ECNCE Count */
170 };
171 
172 /* 4. RESET_STREAM */
173 static const OSSL_QUIC_FRAME_RESET_STREAM encode_case_4_f = {
174     0x1234, 0x9781, 0x11717
175 };
176 
encode_case_4_enc(WPACKET * pkt)177 static int encode_case_4_enc(WPACKET *pkt)
178 {
179     if (!TEST_int_eq(ossl_quic_wire_encode_frame_reset_stream(pkt,
180                          &encode_case_4_f),
181             1))
182         return 0;
183 
184     return 1;
185 }
186 
encode_case_4_dec(PACKET * pkt,ossl_ssize_t fail)187 static int encode_case_4_dec(PACKET *pkt, ossl_ssize_t fail)
188 {
189     OSSL_QUIC_FRAME_RESET_STREAM f = { 0 };
190 
191     if (!TEST_int_eq(ossl_quic_wire_decode_frame_reset_stream(pkt, &f), fail < 0))
192         return 0;
193 
194     if (fail >= 0)
195         return 1;
196 
197     if (!TEST_mem_eq(&f, sizeof(f), &encode_case_4_f, sizeof(encode_case_4_f)))
198         return 0;
199 
200     return 1;
201 }
202 
203 static const unsigned char encode_case_4_expect[] = {
204     0x04, /* Type */
205     0x52,
206     0x34, /* Stream ID */
207     0x80,
208     0x00,
209     0x97,
210     0x81, /* App Error Code */
211     0x80,
212     0x01,
213     0x17,
214     0x17, /* Final Size */
215 };
216 
217 /* 5. STOP_SENDING */
218 static const OSSL_QUIC_FRAME_STOP_SENDING encode_case_5_f = {
219     0x1234, 0x9781
220 };
221 
encode_case_5_enc(WPACKET * pkt)222 static int encode_case_5_enc(WPACKET *pkt)
223 {
224     if (!TEST_int_eq(ossl_quic_wire_encode_frame_stop_sending(pkt,
225                          &encode_case_5_f),
226             1))
227         return 0;
228 
229     return 1;
230 }
231 
encode_case_5_dec(PACKET * pkt,ossl_ssize_t fail)232 static int encode_case_5_dec(PACKET *pkt, ossl_ssize_t fail)
233 {
234     OSSL_QUIC_FRAME_STOP_SENDING f = { 0 };
235 
236     if (!TEST_int_eq(ossl_quic_wire_decode_frame_stop_sending(pkt, &f), fail < 0))
237         return 0;
238 
239     if (fail >= 0)
240         return 1;
241 
242     if (!TEST_mem_eq(&f, sizeof(f), &encode_case_5_f, sizeof(encode_case_5_f)))
243         return 0;
244 
245     return 1;
246 }
247 
248 static const unsigned char encode_case_5_expect[] = {
249     0x05, /* Type */
250     0x52, 0x34, /* Stream ID */
251     0x80, 0x00, 0x97, 0x81 /* App Error Code */
252 };
253 
254 /* 6. CRYPTO */
255 static const unsigned char encode_case_6_data[] = {
256     93, 18, 17, 102, 33
257 };
258 
259 static const OSSL_QUIC_FRAME_CRYPTO encode_case_6_f = {
260     0x1234, sizeof(encode_case_6_data), encode_case_6_data
261 };
262 
encode_case_6_enc(WPACKET * pkt)263 static int encode_case_6_enc(WPACKET *pkt)
264 {
265     if (!TEST_ptr(ossl_quic_wire_encode_frame_crypto(pkt,
266             &encode_case_6_f)))
267         return 0;
268 
269     return 1;
270 }
271 
encode_case_6_dec(PACKET * pkt,ossl_ssize_t fail)272 static int encode_case_6_dec(PACKET *pkt, ossl_ssize_t fail)
273 {
274     OSSL_QUIC_FRAME_CRYPTO f = { 0 };
275 
276     if (!TEST_int_eq(ossl_quic_wire_decode_frame_crypto(pkt, 0, &f), fail < 0))
277         return 0;
278 
279     if (fail >= 0)
280         return 1;
281 
282     if (!TEST_uint64_t_eq(f.offset, 0x1234))
283         return 0;
284 
285     if (!TEST_uint64_t_le(f.len, SIZE_MAX))
286         return 0;
287 
288     if (!TEST_mem_eq(f.data, (size_t)f.len,
289             encode_case_6_data, sizeof(encode_case_6_data)))
290         return 0;
291 
292     return 1;
293 }
294 
295 static const unsigned char encode_case_6_expect[] = {
296     0x06, /* Type */
297     0x52, 0x34, /* Offset */
298     0x05, /* Length */
299     93, 18, 17, 102, 33 /* Data */
300 };
301 
302 /* 7. NEW_TOKEN */
303 static const unsigned char encode_case_7_token[] = {
304     0xde, 0x06, 0xcb, 0x76, 0x5d, 0xb1, 0xa7, 0x71,
305     0x78, 0x09, 0xbb, 0xe8, 0x50, 0x19, 0x12, 0x9a
306 };
307 
encode_case_7_enc(WPACKET * pkt)308 static int encode_case_7_enc(WPACKET *pkt)
309 {
310     if (!TEST_int_eq(ossl_quic_wire_encode_frame_new_token(pkt,
311                          encode_case_7_token,
312                          sizeof(encode_case_7_token)),
313             1))
314         return 0;
315 
316     return 1;
317 }
318 
encode_case_7_dec(PACKET * pkt,ossl_ssize_t fail)319 static int encode_case_7_dec(PACKET *pkt, ossl_ssize_t fail)
320 {
321     const unsigned char *token = NULL;
322     size_t token_len = 0;
323 
324     if (!TEST_int_eq(ossl_quic_wire_decode_frame_new_token(pkt,
325                          &token,
326                          &token_len),
327             fail < 0))
328         return 0;
329 
330     if (fail >= 0)
331         return 1;
332 
333     if (!TEST_mem_eq(token, token_len,
334             encode_case_7_token, sizeof(encode_case_7_token)))
335         return 0;
336 
337     return 1;
338 }
339 
340 static const unsigned char encode_case_7_expect[] = {
341     0x07, /* Type */
342     0x10, /* Length */
343     0xde, 0x06, 0xcb, 0x76, 0x5d, 0xb1, 0xa7, 0x71, /* Token */
344     0x78, 0x09, 0xbb, 0xe8, 0x50, 0x19, 0x12, 0x9a
345 };
346 
347 /* 8. STREAM (no length, no offset, no fin) */
348 static const unsigned char encode_case_8_data[] = {
349     0xde, 0x06, 0xcb, 0x76, 0x5d
350 };
351 static const OSSL_QUIC_FRAME_STREAM encode_case_8_f = {
352     0x1234, 0, 5, encode_case_8_data, 0, 0
353 };
354 
encode_case_8_enc(WPACKET * pkt)355 static int encode_case_8_enc(WPACKET *pkt)
356 {
357     if (!TEST_ptr(ossl_quic_wire_encode_frame_stream(pkt,
358             &encode_case_8_f)))
359         return 0;
360 
361     return 1;
362 }
363 
encode_case_8_dec(PACKET * pkt,ossl_ssize_t fail)364 static int encode_case_8_dec(PACKET *pkt, ossl_ssize_t fail)
365 {
366     OSSL_QUIC_FRAME_STREAM f = { 0 };
367 
368     if (fail >= 3)
369         /*
370          * This case uses implicit length signalling so truncation will not
371          * cause it to fail unless the header (which is 3 bytes) is truncated.
372          */
373         return 1;
374 
375     if (!TEST_int_eq(ossl_quic_wire_decode_frame_stream(pkt, 0, &f), fail < 0))
376         return 0;
377 
378     if (fail >= 0)
379         return 1;
380 
381     if (!TEST_uint64_t_le(f.len, SIZE_MAX))
382         return 0;
383 
384     if (!TEST_mem_eq(f.data, (size_t)f.len,
385             encode_case_8_data, sizeof(encode_case_8_data)))
386         return 0;
387 
388     if (!TEST_uint64_t_eq(f.stream_id, 0x1234))
389         return 0;
390 
391     if (!TEST_uint64_t_eq(f.offset, 0))
392         return 0;
393 
394     if (!TEST_int_eq(f.has_explicit_len, 0))
395         return 0;
396 
397     if (!TEST_int_eq(f.is_fin, 0))
398         return 0;
399 
400     return 1;
401 }
402 
403 static const unsigned char encode_case_8_expect[] = {
404     0x08, /* Type (OFF=0, LEN=0, FIN=0) */
405     0x52, 0x34, /* Stream ID */
406     0xde, 0x06, 0xcb, 0x76, 0x5d /* Data */
407 };
408 
409 /* 9. STREAM (length, offset, fin) */
410 static const unsigned char encode_case_9_data[] = {
411     0xde, 0x06, 0xcb, 0x76, 0x5d
412 };
413 static const OSSL_QUIC_FRAME_STREAM encode_case_9_f = {
414     0x1234, 0x39, 5, encode_case_9_data, 1, 1
415 };
416 
encode_case_9_enc(WPACKET * pkt)417 static int encode_case_9_enc(WPACKET *pkt)
418 {
419     if (!TEST_ptr(ossl_quic_wire_encode_frame_stream(pkt,
420             &encode_case_9_f)))
421         return 0;
422 
423     return 1;
424 }
425 
encode_case_9_dec(PACKET * pkt,ossl_ssize_t fail)426 static int encode_case_9_dec(PACKET *pkt, ossl_ssize_t fail)
427 {
428     OSSL_QUIC_FRAME_STREAM f = { 0 };
429 
430     if (!TEST_int_eq(ossl_quic_wire_decode_frame_stream(pkt, 0, &f), fail < 0))
431         return 0;
432 
433     if (fail >= 0)
434         return 1;
435 
436     if (!TEST_uint64_t_le(f.len, SIZE_MAX))
437         return 0;
438 
439     if (!TEST_mem_eq(f.data, (size_t)f.len,
440             encode_case_9_data, sizeof(encode_case_9_data)))
441         return 0;
442 
443     if (!TEST_uint64_t_eq(f.stream_id, 0x1234))
444         return 0;
445 
446     if (!TEST_uint64_t_eq(f.offset, 0x39))
447         return 0;
448 
449     if (!TEST_int_eq(f.has_explicit_len, 1))
450         return 0;
451 
452     if (!TEST_int_eq(f.is_fin, 1))
453         return 0;
454 
455     return 1;
456 }
457 
458 static const unsigned char encode_case_9_expect[] = {
459     0x0f, /* Type (OFF=1, LEN=1, FIN=1) */
460     0x52, 0x34, /* Stream ID */
461     0x39, /* Offset */
462     0x05, /* Length */
463     0xde, 0x06, 0xcb, 0x76, 0x5d /* Data */
464 };
465 
466 /* 10. MAX_DATA */
encode_case_10_enc(WPACKET * pkt)467 static int encode_case_10_enc(WPACKET *pkt)
468 {
469     if (!TEST_int_eq(ossl_quic_wire_encode_frame_max_data(pkt, 0x1234), 1))
470         return 0;
471 
472     return 1;
473 }
474 
encode_case_10_dec(PACKET * pkt,ossl_ssize_t fail)475 static int encode_case_10_dec(PACKET *pkt, ossl_ssize_t fail)
476 {
477     uint64_t max_data = 0;
478 
479     if (!TEST_int_eq(ossl_quic_wire_decode_frame_max_data(pkt, &max_data), fail < 0))
480         return 0;
481 
482     if (fail >= 0)
483         return 1;
484 
485     if (!TEST_uint64_t_eq(max_data, 0x1234))
486         return 0;
487 
488     return 1;
489 }
490 
491 static const unsigned char encode_case_10_expect[] = {
492     0x10, /* Type */
493     0x52,
494     0x34, /* Max Data */
495 };
496 
497 /* 11. MAX_STREAM_DATA */
encode_case_11_enc(WPACKET * pkt)498 static int encode_case_11_enc(WPACKET *pkt)
499 {
500     if (!TEST_int_eq(ossl_quic_wire_encode_frame_max_stream_data(pkt,
501                          0x1234,
502                          0x9781),
503             1))
504         return 0;
505 
506     return 1;
507 }
508 
encode_case_11_dec(PACKET * pkt,ossl_ssize_t fail)509 static int encode_case_11_dec(PACKET *pkt, ossl_ssize_t fail)
510 {
511     uint64_t stream_id = 0, max_data = 0;
512 
513     if (!TEST_int_eq(ossl_quic_wire_decode_frame_max_stream_data(pkt,
514                          &stream_id,
515                          &max_data),
516             fail < 0))
517         return 0;
518 
519     if (fail >= 0)
520         return 1;
521 
522     if (!TEST_uint64_t_eq(stream_id, 0x1234))
523         return 0;
524 
525     if (!TEST_uint64_t_eq(max_data, 0x9781))
526         return 0;
527 
528     return 1;
529 }
530 
531 static const unsigned char encode_case_11_expect[] = {
532     0x11, /* Type */
533     0x52,
534     0x34, /* Stream ID */
535     0x80,
536     0x00,
537     0x97,
538     0x81, /* Max Data */
539 };
540 
541 /* 12. MAX_STREAMS */
encode_case_12_enc(WPACKET * pkt)542 static int encode_case_12_enc(WPACKET *pkt)
543 {
544     if (!TEST_int_eq(ossl_quic_wire_encode_frame_max_streams(pkt, 0, 0x1234), 1))
545         return 0;
546 
547     if (!TEST_int_eq(ossl_quic_wire_encode_frame_max_streams(pkt, 1, 0x9781), 1))
548         return 0;
549 
550     return 1;
551 }
552 
encode_case_12_dec(PACKET * pkt,ossl_ssize_t fail)553 static int encode_case_12_dec(PACKET *pkt, ossl_ssize_t fail)
554 {
555     uint64_t max_streams_1 = 0, max_streams_2 = 0,
556              frame_type_1 = 0, frame_type_2 = 0;
557     int is_minimal = 1, success_if;
558 
559     success_if = (fail < 0 || fail >= 1);
560     if (!TEST_int_eq(ossl_quic_wire_peek_frame_header(pkt, &frame_type_1,
561                          &is_minimal),
562             success_if))
563         return 0;
564 
565     if (!TEST_true(!success_if || is_minimal))
566         return 0;
567 
568     success_if = (fail < 0 || fail >= 3);
569     if (!TEST_int_eq(ossl_quic_wire_decode_frame_max_streams(pkt,
570                          &max_streams_1),
571             success_if))
572         return 0;
573 
574     success_if = (fail < 0 || fail >= 4);
575     if (!TEST_int_eq(ossl_quic_wire_peek_frame_header(pkt, &frame_type_2,
576                          &is_minimal),
577             success_if))
578         return 0;
579 
580     if (!TEST_true(!success_if || is_minimal))
581         return 0;
582 
583     success_if = (fail < 0);
584     if (!TEST_int_eq(ossl_quic_wire_decode_frame_max_streams(pkt,
585                          &max_streams_2),
586             success_if))
587         return 0;
588 
589     if ((fail < 0 || fail >= 3)
590         && !TEST_uint64_t_eq(frame_type_1, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI))
591         return 0;
592 
593     if ((fail < 0 || fail >= 3)
594         && !TEST_uint64_t_eq(max_streams_1, 0x1234))
595         return 0;
596 
597     if ((fail < 0 || fail >= 8)
598         && !TEST_uint64_t_eq(frame_type_2, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI))
599         return 0;
600 
601     if ((fail < 0 || fail >= 8)
602         && !TEST_uint64_t_eq(max_streams_2, 0x9781))
603         return 0;
604 
605     return 1;
606 }
607 
608 static const unsigned char encode_case_12_expect[] = {
609     0x12, /* Type (MAX_STREAMS Bidirectional) */
610     0x52,
611     0x34, /* Max Streams */
612     0x13, /* Type (MAX_STREAMS Unidirectional) */
613     0x80,
614     0x00,
615     0x97,
616     0x81, /* Max Streams */
617 };
618 
619 /* 13. DATA_BLOCKED */
encode_case_13_enc(WPACKET * pkt)620 static int encode_case_13_enc(WPACKET *pkt)
621 {
622     if (!TEST_int_eq(ossl_quic_wire_encode_frame_data_blocked(pkt, 0x1234), 1))
623         return 0;
624 
625     return 1;
626 }
627 
encode_case_13_dec(PACKET * pkt,ossl_ssize_t fail)628 static int encode_case_13_dec(PACKET *pkt, ossl_ssize_t fail)
629 {
630     uint64_t max_data = 0;
631 
632     if (!TEST_int_eq(ossl_quic_wire_decode_frame_data_blocked(pkt,
633                          &max_data),
634             fail < 0))
635         return 0;
636 
637     if (fail >= 0)
638         return 1;
639 
640     if (!TEST_uint64_t_eq(max_data, 0x1234))
641         return 0;
642 
643     return 1;
644 }
645 
646 static const unsigned char encode_case_13_expect[] = {
647     0x14, /* Type */
648     0x52,
649     0x34, /* Max Data */
650 };
651 
652 /* 14. STREAM_DATA_BLOCKED */
encode_case_14_enc(WPACKET * pkt)653 static int encode_case_14_enc(WPACKET *pkt)
654 {
655     if (!TEST_int_eq(ossl_quic_wire_encode_frame_stream_data_blocked(pkt,
656                          0x1234,
657                          0x9781),
658             1))
659         return 0;
660 
661     return 1;
662 }
663 
encode_case_14_dec(PACKET * pkt,ossl_ssize_t fail)664 static int encode_case_14_dec(PACKET *pkt, ossl_ssize_t fail)
665 {
666     uint64_t stream_id = 0, max_data = 0;
667 
668     if (!TEST_int_eq(ossl_quic_wire_decode_frame_stream_data_blocked(pkt,
669                          &stream_id,
670                          &max_data),
671             fail < 0))
672         return 0;
673 
674     if (fail >= 0)
675         return 1;
676 
677     if (!TEST_uint64_t_eq(stream_id, 0x1234))
678         return 0;
679 
680     if (!TEST_uint64_t_eq(max_data, 0x9781))
681         return 0;
682 
683     return 1;
684 }
685 
686 static const unsigned char encode_case_14_expect[] = {
687     0x15, /* Type */
688     0x52,
689     0x34, /* Stream ID */
690     0x80,
691     0x00,
692     0x97,
693     0x81, /* Max Data */
694 };
695 
696 /* 15. STREAMS_BLOCKED */
encode_case_15_enc(WPACKET * pkt)697 static int encode_case_15_enc(WPACKET *pkt)
698 {
699     if (!TEST_int_eq(ossl_quic_wire_encode_frame_streams_blocked(pkt, 0, 0x1234), 1))
700         return 0;
701 
702     if (!TEST_int_eq(ossl_quic_wire_encode_frame_streams_blocked(pkt, 1, 0x9781), 1))
703         return 0;
704 
705     return 1;
706 }
707 
encode_case_15_dec(PACKET * pkt,ossl_ssize_t fail)708 static int encode_case_15_dec(PACKET *pkt, ossl_ssize_t fail)
709 {
710     uint64_t max_streams_1 = 0, max_streams_2 = 0,
711              frame_type_1 = 0, frame_type_2 = 0;
712     int is_minimal = 1, success_if;
713 
714     success_if = (fail < 0 || fail >= 1);
715     if (!TEST_int_eq(ossl_quic_wire_peek_frame_header(pkt, &frame_type_1,
716                          &is_minimal),
717             success_if))
718         return 0;
719 
720     if (!TEST_true(!success_if || is_minimal))
721         return 0;
722 
723     success_if = (fail < 0 || fail >= 3);
724     if (!TEST_int_eq(ossl_quic_wire_decode_frame_streams_blocked(pkt,
725                          &max_streams_1),
726             success_if))
727         return 0;
728 
729     success_if = (fail < 0 || fail >= 4);
730     if (!TEST_int_eq(ossl_quic_wire_peek_frame_header(pkt, &frame_type_2,
731                          &is_minimal),
732             success_if))
733         return 0;
734 
735     if (!TEST_true(!success_if || is_minimal))
736         return 0;
737 
738     if (!TEST_int_eq(ossl_quic_wire_decode_frame_streams_blocked(pkt,
739                          &max_streams_2),
740             fail < 0 || fail >= 8))
741         return 0;
742 
743     if ((fail < 0 || fail >= 1)
744         && !TEST_uint64_t_eq(frame_type_1, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_BIDI))
745         return 0;
746 
747     if ((fail < 0 || fail >= 3)
748         && !TEST_uint64_t_eq(max_streams_1, 0x1234))
749         return 0;
750 
751     if ((fail < 0 || fail >= 4)
752         && !TEST_uint64_t_eq(frame_type_2, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_UNI))
753         return 0;
754 
755     if ((fail < 0 || fail >= 8)
756         && !TEST_uint64_t_eq(max_streams_2, 0x9781))
757         return 0;
758 
759     return 1;
760 }
761 
762 static const unsigned char encode_case_15_expect[] = {
763     0x16, /* Type (STREAMS_BLOCKED Bidirectional) */
764     0x52,
765     0x34, /* Max Streams */
766     0x17, /* Type (STREAMS_BLOCKED Unidirectional) */
767     0x80,
768     0x00,
769     0x97,
770     0x81, /* Max Streams */
771 };
772 
773 /* 16. NEW_CONNECTION_ID */
774 static const unsigned char encode_case_16_conn_id[] = {
775     0x33, 0x44, 0x55, 0x66
776 };
777 
778 static const OSSL_QUIC_FRAME_NEW_CONN_ID encode_case_16_f = {
779     0x9781,
780     0x1234,
781     { 0x4,
782         { 0x33, 0x44, 0x55, 0x66 } },
783     { { 0xde, 0x06, 0xcb, 0x76, 0x5d, 0xb1, 0xa7, 0x71,
784         0x78, 0x09, 0xbb, 0xe8, 0x50, 0x19, 0x12, 0x9a } }
785 };
786 
encode_case_16_enc(WPACKET * pkt)787 static int encode_case_16_enc(WPACKET *pkt)
788 {
789     if (!TEST_int_eq(ossl_quic_wire_encode_frame_new_conn_id(pkt,
790                          &encode_case_16_f),
791             1))
792         return 0;
793 
794     return 1;
795 }
796 
encode_case_16_dec(PACKET * pkt,ossl_ssize_t fail)797 static int encode_case_16_dec(PACKET *pkt, ossl_ssize_t fail)
798 {
799     OSSL_QUIC_FRAME_NEW_CONN_ID f = { 0 };
800 
801     if (!TEST_int_eq(ossl_quic_wire_decode_frame_new_conn_id(pkt, &f), fail < 0))
802         return 0;
803 
804     if (fail >= 0)
805         return 1;
806 
807     if (!TEST_uint64_t_eq(f.seq_num, 0x9781))
808         return 0;
809 
810     if (!TEST_uint64_t_eq(f.retire_prior_to, 0x1234))
811         return 0;
812 
813     if (!TEST_uint64_t_eq(f.conn_id.id_len, sizeof(encode_case_16_conn_id)))
814         return 0;
815 
816     if (!TEST_mem_eq(f.conn_id.id, f.conn_id.id_len,
817             encode_case_16_conn_id, sizeof(encode_case_16_conn_id)))
818         return 0;
819 
820     if (!TEST_mem_eq(f.stateless_reset.token,
821             sizeof(f.stateless_reset.token),
822             encode_case_16_f.stateless_reset.token,
823             sizeof(encode_case_16_f.stateless_reset.token)))
824         return 0;
825 
826     return 1;
827 }
828 
829 static const unsigned char encode_case_16_expect[] = {
830     0x18, /* Type */
831     0x80, 0x00, 0x97, 0x81, /* Sequence Number */
832     0x52, 0x34, /* Retire Prior To */
833     0x04, /* Connection ID Length */
834     0x33, 0x44, 0x55, 0x66, /* Connection ID */
835     0xde, 0x06, 0xcb, 0x76, 0x5d, 0xb1, 0xa7, 0x71, /* Stateless Reset Token */
836     0x78, 0x09, 0xbb, 0xe8, 0x50, 0x19, 0x12, 0x9a
837 };
838 
839 /* 16b. NEW_CONNECTION_ID seq_num < retire_prior_to */
840 static const OSSL_QUIC_FRAME_NEW_CONN_ID encode_case_16b_f = {
841     0x1234,
842     0x9781,
843     { 0x4,
844         { 0x33, 0x44, 0x55, 0x66 } },
845     { { 0xde, 0x06, 0xcb, 0x76, 0x5d, 0xb1, 0xa7, 0x71,
846         0x78, 0x09, 0xbb, 0xe8, 0x50, 0x19, 0x12, 0x9a } }
847 };
848 
encode_case_16b_enc(WPACKET * pkt)849 static int encode_case_16b_enc(WPACKET *pkt)
850 {
851     if (!TEST_int_eq(ossl_quic_wire_encode_frame_new_conn_id(pkt,
852                          &encode_case_16b_f),
853             1))
854         return 0;
855 
856     return 1;
857 }
858 
encode_case_16b_dec(PACKET * pkt,ossl_ssize_t fail)859 static int encode_case_16b_dec(PACKET *pkt, ossl_ssize_t fail)
860 {
861     OSSL_QUIC_FRAME_NEW_CONN_ID f = { 0 };
862 
863     if (!TEST_int_eq(ossl_quic_wire_decode_frame_new_conn_id(pkt, &f), 0))
864         return 0;
865 
866     if (!TEST_true(PACKET_forward(pkt, PACKET_remaining(pkt))))
867         return 0;
868 
869     return 1;
870 }
871 
872 static const unsigned char encode_case_16b_expect[] = {
873     0x18, /* Type */
874     0x52, 0x34, /* Sequence Number */
875     0x80, 0x00, 0x97, 0x81, /* Retire Prior To */
876     0x04, /* Connection ID Length */
877     0x33, 0x44, 0x55, 0x66, /* Connection ID */
878     0xde, 0x06, 0xcb, 0x76, 0x5d, 0xb1, 0xa7, 0x71, /* Stateless Reset Token */
879     0x78, 0x09, 0xbb, 0xe8, 0x50, 0x19, 0x12, 0x9a
880 };
881 
882 /* 17. RETIRE_CONNECTION_ID */
encode_case_17_enc(WPACKET * pkt)883 static int encode_case_17_enc(WPACKET *pkt)
884 {
885     if (!TEST_int_eq(ossl_quic_wire_encode_frame_retire_conn_id(pkt, 0x1234), 1))
886         return 0;
887 
888     return 1;
889 }
890 
encode_case_17_dec(PACKET * pkt,ossl_ssize_t fail)891 static int encode_case_17_dec(PACKET *pkt, ossl_ssize_t fail)
892 {
893     uint64_t seq_num = 0;
894 
895     if (!TEST_int_eq(ossl_quic_wire_decode_frame_retire_conn_id(pkt, &seq_num), fail < 0))
896         return 0;
897 
898     if (fail >= 0)
899         return 1;
900 
901     if (!TEST_uint64_t_eq(seq_num, 0x1234))
902         return 0;
903 
904     return 1;
905 }
906 
907 static const unsigned char encode_case_17_expect[] = {
908     0x19, /* Type */
909     0x52,
910     0x34, /* Seq Num */
911 };
912 
913 /* 18. PATH_CHALLENGE */
914 static const uint64_t encode_case_18_data
915     = (((uint64_t)0x5f4b12) << 40) | (uint64_t)0x731834UL;
916 
encode_case_18_enc(WPACKET * pkt)917 static int encode_case_18_enc(WPACKET *pkt)
918 {
919     if (!TEST_int_eq(ossl_quic_wire_encode_frame_path_challenge(pkt,
920                          encode_case_18_data),
921             1))
922         return 0;
923 
924     return 1;
925 }
926 
encode_case_18_dec(PACKET * pkt,ossl_ssize_t fail)927 static int encode_case_18_dec(PACKET *pkt, ossl_ssize_t fail)
928 {
929     uint64_t challenge = 0;
930 
931     if (!TEST_int_eq(ossl_quic_wire_decode_frame_path_challenge(pkt, &challenge), fail < 0))
932         return 0;
933 
934     if (fail >= 0)
935         return 1;
936 
937     if (!TEST_uint64_t_eq(challenge, encode_case_18_data))
938         return 0;
939 
940     return 1;
941 }
942 
943 static const unsigned char encode_case_18_expect[] = {
944     0x1A, /* Type */
945     0x5f,
946     0x4b,
947     0x12,
948     0x00,
949     0x00,
950     0x73,
951     0x18,
952     0x34, /* Data */
953 };
954 
955 /* 19. PATH_RESPONSE */
956 static const uint64_t encode_case_19_data
957     = (((uint64_t)0x5f4b12) << 40) | (uint64_t)0x731834UL;
958 
encode_case_19_enc(WPACKET * pkt)959 static int encode_case_19_enc(WPACKET *pkt)
960 {
961     if (!TEST_int_eq(ossl_quic_wire_encode_frame_path_response(pkt,
962                          encode_case_19_data),
963             1))
964         return 0;
965 
966     return 1;
967 }
968 
encode_case_19_dec(PACKET * pkt,ossl_ssize_t fail)969 static int encode_case_19_dec(PACKET *pkt, ossl_ssize_t fail)
970 {
971     uint64_t challenge = 0;
972 
973     if (!TEST_int_eq(ossl_quic_wire_decode_frame_path_response(pkt, &challenge), fail < 0))
974         return 0;
975 
976     if (fail >= 0)
977         return 1;
978 
979     if (!TEST_uint64_t_eq(challenge, encode_case_19_data))
980         return 0;
981 
982     return 1;
983 }
984 
985 static const unsigned char encode_case_19_expect[] = {
986     0x1B, /* Type */
987     0x5f,
988     0x4b,
989     0x12,
990     0x00,
991     0x00,
992     0x73,
993     0x18,
994     0x34, /* Data */
995 };
996 
997 /* 20. CONNECTION_CLOSE (transport) */
998 static const char encode_case_20_reason[] = {
999     /* "reason for closure" */
1000     0x72, 0x65, 0x61, 0x73, 0x6f, 0x6e, 0x20, 0x66, 0x6f,
1001     0x72, 0x20, 0x63, 0x6c, 0x6f, 0x73, 0x75, 0x72, 0x65
1002 };
1003 
1004 static const OSSL_QUIC_FRAME_CONN_CLOSE encode_case_20_f = {
1005     0,
1006     0x1234,
1007     0x9781,
1008     (char *)encode_case_20_reason,
1009     sizeof(encode_case_20_reason)
1010 };
1011 
encode_case_20_enc(WPACKET * pkt)1012 static int encode_case_20_enc(WPACKET *pkt)
1013 {
1014     if (!TEST_int_eq(ossl_quic_wire_encode_frame_conn_close(pkt,
1015                          &encode_case_20_f),
1016             1))
1017         return 0;
1018 
1019     return 1;
1020 }
1021 
encode_case_20_dec(PACKET * pkt,ossl_ssize_t fail)1022 static int encode_case_20_dec(PACKET *pkt, ossl_ssize_t fail)
1023 {
1024     OSSL_QUIC_FRAME_CONN_CLOSE f = { 0 };
1025 
1026     if (!TEST_int_eq(ossl_quic_wire_decode_frame_conn_close(pkt, &f), fail < 0))
1027         return 0;
1028 
1029     if (fail >= 0)
1030         return 1;
1031 
1032     if (!TEST_int_eq(f.is_app, 0))
1033         return 0;
1034 
1035     if (!TEST_uint64_t_eq(f.error_code, 0x1234))
1036         return 0;
1037 
1038     if (!TEST_uint64_t_eq(f.frame_type, 0x9781))
1039         return 0;
1040 
1041     if (!TEST_size_t_eq(f.reason_len, 18))
1042         return 0;
1043 
1044     if (!TEST_mem_eq(f.reason, f.reason_len,
1045             encode_case_20_f.reason, encode_case_20_f.reason_len))
1046         return 0;
1047 
1048     return 1;
1049 }
1050 
1051 static const unsigned char encode_case_20_expect[] = {
1052     0x1C, /* Type */
1053     0x52, 0x34, /* Sequence Number */
1054     0x80, 0x00, 0x97, 0x81, /* Frame Type */
1055     0x12, /* Reason Length */
1056     0x72, 0x65, 0x61, 0x73, 0x6f, 0x6e, 0x20, 0x66, 0x6f, /* Reason */
1057     0x72, 0x20, 0x63, 0x6c, 0x6f, 0x73, 0x75, 0x72, 0x65
1058 };
1059 
1060 /* 21. HANDSHAKE_DONE */
encode_case_21_enc(WPACKET * pkt)1061 static int encode_case_21_enc(WPACKET *pkt)
1062 {
1063 
1064     if (!TEST_int_eq(ossl_quic_wire_encode_frame_handshake_done(pkt), 1))
1065         return 0;
1066 
1067     return 1;
1068 }
1069 
encode_case_21_dec(PACKET * pkt,ossl_ssize_t fail)1070 static int encode_case_21_dec(PACKET *pkt, ossl_ssize_t fail)
1071 {
1072 
1073     if (!TEST_int_eq(ossl_quic_wire_decode_frame_handshake_done(pkt), fail < 0))
1074         return 0;
1075 
1076     return 1;
1077 }
1078 
1079 static const unsigned char encode_case_21_expect[] = {
1080     0x1E
1081 };
1082 
1083 /* 22. Buffer Transport Parameter */
1084 static const unsigned char encode_case_22_data[] = { 0x55, 0x77, 0x32, 0x46, 0x99 };
1085 
encode_case_22_enc(WPACKET * pkt)1086 static int encode_case_22_enc(WPACKET *pkt)
1087 {
1088     unsigned char *p;
1089 
1090     if (!TEST_ptr(ossl_quic_wire_encode_transport_param_bytes(pkt, 0x1234,
1091             encode_case_22_data,
1092             sizeof(encode_case_22_data))))
1093         return 0;
1094 
1095     if (!TEST_ptr(p = ossl_quic_wire_encode_transport_param_bytes(pkt, 0x9781,
1096                       NULL, 2)))
1097         return 0;
1098 
1099     p[0] = 0x33;
1100     p[1] = 0x44;
1101 
1102     return 1;
1103 }
1104 
encode_case_22_dec(PACKET * pkt,ossl_ssize_t fail)1105 static int encode_case_22_dec(PACKET *pkt, ossl_ssize_t fail)
1106 {
1107     uint64_t id = 0;
1108     size_t len = 0;
1109     const unsigned char *p;
1110     static const unsigned char data[] = { 0x33, 0x44 };
1111 
1112     if (!TEST_int_eq(ossl_quic_wire_peek_transport_param(pkt, &id),
1113             fail < 0 || fail >= 2))
1114         return 0;
1115 
1116     if ((fail < 0 || fail >= 2)
1117         && !TEST_uint64_t_eq(id, 0x1234))
1118         return 0;
1119 
1120     id = 0;
1121 
1122     p = ossl_quic_wire_decode_transport_param_bytes(pkt, &id, &len);
1123     if (fail < 0 || fail >= 8) {
1124         if (!TEST_ptr(p))
1125             return 0;
1126     } else {
1127         if (!TEST_ptr_null(p))
1128             return 0;
1129     }
1130 
1131     if ((fail < 0 || fail >= 8)
1132         && !TEST_uint64_t_eq(id, 0x1234))
1133         return 0;
1134 
1135     if ((fail < 0 || fail >= 8)
1136         && !TEST_mem_eq(p, len, encode_case_22_data, sizeof(encode_case_22_data)))
1137         return 0;
1138 
1139     if ((fail < 0 || fail >= 8)
1140         && !TEST_int_eq(ossl_quic_wire_peek_transport_param(pkt, &id),
1141             fail < 0 || fail >= 12))
1142         return 0;
1143 
1144     if ((fail < 0 || fail >= 12)
1145         && !TEST_uint64_t_eq(id, 0x9781))
1146         return 0;
1147 
1148     id = 0;
1149 
1150     p = ossl_quic_wire_decode_transport_param_bytes(pkt, &id, &len);
1151     if (fail < 0 || fail >= 15) {
1152         if (!TEST_ptr(p))
1153             return 0;
1154     } else {
1155         if (!TEST_ptr_null(p))
1156             return 0;
1157     }
1158 
1159     if ((fail < 0 || fail >= 15)
1160         && !TEST_uint64_t_eq(id, 0x9781))
1161         return 0;
1162 
1163     if ((fail < 0 || fail >= 15)
1164         && !TEST_mem_eq(p, len, data, sizeof(data)))
1165         return 0;
1166 
1167     return 1;
1168 }
1169 
1170 static const unsigned char encode_case_22_expect[] = {
1171     0x52, 0x34, /* ID */
1172     0x05, /* Length */
1173     0x55, 0x77, 0x32, 0x46, 0x99, /* Data */
1174 
1175     0x80, 0x00, 0x97, 0x81, /* ID */
1176     0x02, /* Length */
1177     0x33, 0x44 /* Data */
1178 };
1179 
1180 /* 23. Integer Transport Parameter */
encode_case_23_enc(WPACKET * pkt)1181 static int encode_case_23_enc(WPACKET *pkt)
1182 {
1183     if (!TEST_int_eq(ossl_quic_wire_encode_transport_param_int(pkt, 0x1234, 0x9781), 1))
1184         return 0;
1185 
1186     if (!TEST_int_eq(ossl_quic_wire_encode_transport_param_int(pkt, 0x2233, 0x4545), 1))
1187         return 0;
1188 
1189     return 1;
1190 }
1191 
encode_case_23_dec(PACKET * pkt,ossl_ssize_t fail)1192 static int encode_case_23_dec(PACKET *pkt, ossl_ssize_t fail)
1193 {
1194     uint64_t id = 0, value = 0;
1195 
1196     if (!TEST_int_eq(ossl_quic_wire_decode_transport_param_int(pkt,
1197                          &id, &value),
1198             fail < 0 || fail >= 7))
1199         return 0;
1200 
1201     if ((fail < 0 || fail >= 7)
1202         && !TEST_uint64_t_eq(id, 0x1234))
1203         return 0;
1204 
1205     if ((fail < 0 || fail >= 7)
1206         && !TEST_uint64_t_eq(value, 0x9781))
1207         return 0;
1208 
1209     if (!TEST_int_eq(ossl_quic_wire_decode_transport_param_int(pkt,
1210                          &id, &value),
1211             fail < 0 || fail >= 14))
1212         return 0;
1213 
1214     if ((fail < 0 || fail >= 14)
1215         && !TEST_uint64_t_eq(id, 0x2233))
1216         return 0;
1217 
1218     if ((fail < 0 || fail >= 14)
1219         && !TEST_uint64_t_eq(value, 0x4545))
1220         return 0;
1221 
1222     return 1;
1223 }
1224 
1225 static const unsigned char encode_case_23_expect[] = {
1226     0x52,
1227     0x34,
1228     0x04,
1229     0x80,
1230     0x00,
1231     0x97,
1232     0x81,
1233 
1234     0x62,
1235     0x33,
1236     0x04,
1237     0x80,
1238     0x00,
1239     0x45,
1240     0x45,
1241 };
1242 
1243 #define ENCODE_CASE(n)                        \
1244     {                                         \
1245         encode_case_##n##_enc,                \
1246         encode_case_##n##_expect,             \
1247         OSSL_NELEM(encode_case_##n##_expect), \
1248         encode_case_##n##_dec                 \
1249     },
1250 
1251 static const struct encode_test_case encode_cases[] = {
1252     ENCODE_CASE(1)
1253         ENCODE_CASE(2)
1254             ENCODE_CASE(3)
1255                 ENCODE_CASE(4)
1256                     ENCODE_CASE(5)
1257                         ENCODE_CASE(6)
1258                             ENCODE_CASE(7)
1259                                 ENCODE_CASE(8)
1260                                     ENCODE_CASE(9)
1261                                         ENCODE_CASE(10)
1262                                             ENCODE_CASE(11)
1263                                                 ENCODE_CASE(12)
1264                                                     ENCODE_CASE(13)
1265                                                         ENCODE_CASE(14)
1266                                                             ENCODE_CASE(15)
1267                                                                 ENCODE_CASE(16)
1268                                                                     ENCODE_CASE(16b)
1269                                                                         ENCODE_CASE(17)
1270                                                                             ENCODE_CASE(18)
1271                                                                                 ENCODE_CASE(19)
1272                                                                                     ENCODE_CASE(20)
1273                                                                                         ENCODE_CASE(21)
1274                                                                                             ENCODE_CASE(22)
1275                                                                                                 ENCODE_CASE(23)
1276 };
1277 
test_wire_encode(int idx)1278 static int test_wire_encode(int idx)
1279 {
1280     int testresult = 0;
1281     WPACKET wpkt;
1282     PACKET pkt;
1283     BUF_MEM *buf = NULL;
1284     size_t written;
1285     const struct encode_test_case *c = &encode_cases[idx];
1286     int have_wpkt = 0;
1287     size_t i;
1288 
1289     if (!TEST_ptr(buf = BUF_MEM_new()))
1290         goto err;
1291 
1292     if (!TEST_int_eq(WPACKET_init(&wpkt, buf), 1))
1293         goto err;
1294 
1295     have_wpkt = 1;
1296     if (!TEST_int_eq(c->serializer(&wpkt), 1))
1297         goto err;
1298 
1299     if (!TEST_int_eq(WPACKET_get_total_written(&wpkt, &written), 1))
1300         goto err;
1301 
1302     if (!TEST_mem_eq(buf->data, written, c->expect_buf, c->expect_buf_len))
1303         goto err;
1304 
1305     if (!TEST_int_eq(PACKET_buf_init(&pkt, (unsigned char *)buf->data, written), 1))
1306         goto err;
1307 
1308     if (!TEST_int_eq(c->deserializer(&pkt, -1), 1))
1309         goto err;
1310 
1311     if (!TEST_false(PACKET_remaining(&pkt)))
1312         goto err;
1313 
1314     for (i = 0; i < c->expect_buf_len; ++i) {
1315         PACKET pkt2;
1316 
1317         /*
1318          * Check parsing truncated (i.e., malformed) input is handled correctly.
1319          * Generate all possible truncations of our reference encoding and
1320          * verify that they are handled correctly. The number of bytes of the
1321          * truncated encoding is passed as an argument to the deserializer to
1322          * help it determine whether decoding should fail or not.
1323          */
1324         if (!TEST_int_eq(PACKET_buf_init(&pkt2, (unsigned char *)c->expect_buf, i), 1))
1325             goto err;
1326 
1327         if (!TEST_int_eq(c->deserializer(&pkt2, i), 1))
1328             goto err;
1329     }
1330 
1331     testresult = 1;
1332 err:
1333     if (have_wpkt)
1334         WPACKET_finish(&wpkt);
1335     BUF_MEM_free(buf);
1336     return testresult;
1337 }
1338 
1339 struct ack_test_case {
1340     const unsigned char *input_buf;
1341     size_t input_buf_len;
1342     int (*deserializer)(PACKET *pkt);
1343     int expect_fail;
1344 };
1345 
1346 /* ACK Frame with Excessive First ACK Range Field */
1347 static const unsigned char ack_case_1_input[] = {
1348     0x02, /* ACK Without ECN */
1349     0x08, /* Largest Acknowledged */
1350     0x01, /* ACK Delay */
1351     0x00, /* ACK Range Count */
1352     0x09, /* First ACK Range */
1353 };
1354 
1355 /* ACK Frame with Valid ACK Range Field */
1356 static const unsigned char ack_case_2_input[] = {
1357     0x02, /* ACK Without ECN */
1358     0x08, /* Largest Acknowledged */
1359     0x01, /* ACK Delay */
1360     0x00, /* ACK Range Count */
1361     0x08, /* First ACK Range */
1362 };
1363 
1364 /* ACK Frame with Excessive ACK Range Gap */
1365 static const unsigned char ack_case_3_input[] = {
1366     0x02, /* ACK Without ECN */
1367     0x08, /* Largest Acknowledged */
1368     0x01, /* ACK Delay */
1369     0x01, /* ACK Range Count */
1370     0x01, /* First ACK Range */
1371 
1372     0x05, /* Gap */
1373     0x01, /* ACK Range Length */
1374 };
1375 
1376 /* ACK Frame with Valid ACK Range */
1377 static const unsigned char ack_case_4_input[] = {
1378     0x02, /* ACK Without ECN */
1379     0x08, /* Largest Acknowledged */
1380     0x01, /* ACK Delay */
1381     0x01, /* ACK Range Count */
1382     0x01, /* First ACK Range */
1383 
1384     0x04, /* Gap */
1385     0x01, /* ACK Range Length */
1386 };
1387 
1388 /* ACK Frame with Excessive ACK Range Length */
1389 static const unsigned char ack_case_5_input[] = {
1390     0x02, /* ACK Without ECN */
1391     0x08, /* Largest Acknowledged */
1392     0x01, /* ACK Delay */
1393     0x01, /* ACK Range Count */
1394     0x01, /* First ACK Range */
1395 
1396     0x04, /* Gap */
1397     0x02, /* ACK Range Length */
1398 };
1399 
1400 /* ACK Frame with Multiple ACK Ranges, Final Having Excessive Length */
1401 static const unsigned char ack_case_6_input[] = {
1402     0x02, /* ACK Without ECN */
1403     0x08, /* Largest Acknowledged */
1404     0x01, /* ACK Delay */
1405     0x02, /* ACK Range Count */
1406     0x01, /* First ACK Range */
1407 
1408     0x01, /* Gap */
1409     0x02, /* ACK Range Length */
1410 
1411     0x00, /* Gap */
1412     0x01, /* ACK Range Length */
1413 };
1414 
1415 /* ACK Frame with Multiple ACK Ranges, Valid */
1416 static const unsigned char ack_case_7_input[] = {
1417     0x02, /* ACK Without ECN */
1418     0x08, /* Largest Acknowledged */
1419     0x01, /* ACK Delay */
1420     0x02, /* ACK Range Count */
1421     0x01, /* First ACK Range */
1422 
1423     0x01, /* Gap */
1424     0x02, /* ACK Range Length */
1425 
1426     0x00, /* Gap */
1427     0x00, /* ACK Range Length */
1428 };
1429 
ack_generic_decode(PACKET * pkt)1430 static int ack_generic_decode(PACKET *pkt)
1431 {
1432     OSSL_QUIC_ACK_RANGE ranges[8] = { 0 };
1433     OSSL_QUIC_FRAME_ACK f = { 0 };
1434     uint64_t total_ranges = 0, peek_total_ranges = 0;
1435     int r;
1436     size_t i;
1437 
1438     f.ack_ranges = ranges;
1439     f.num_ack_ranges = OSSL_NELEM(ranges);
1440 
1441     if (!TEST_int_eq(ossl_quic_wire_peek_frame_ack_num_ranges(pkt,
1442                          &peek_total_ranges),
1443             1))
1444         return 0;
1445 
1446     r = ossl_quic_wire_decode_frame_ack(pkt, 3, &f, &total_ranges);
1447     if (r == 0)
1448         return 0;
1449 
1450     if (!TEST_uint64_t_eq(total_ranges, peek_total_ranges))
1451         return 0;
1452 
1453     for (i = 0; i < f.num_ack_ranges; ++i) {
1454         if (!TEST_uint64_t_le(f.ack_ranges[i].start, f.ack_ranges[i].end))
1455             return 0;
1456         if (!TEST_uint64_t_lt(f.ack_ranges[i].end, 1000))
1457             return 0;
1458     }
1459 
1460     return 1;
1461 }
1462 
1463 #define ACK_CASE(n, expect_fail, dec) \
1464     {                                 \
1465         ack_case_##n##_input,         \
1466         sizeof(ack_case_##n##_input), \
1467         (dec),                        \
1468         (expect_fail)                 \
1469     },
1470 
1471 static const struct ack_test_case ack_cases[] = {
1472     ACK_CASE(1, 1, ack_generic_decode)
1473         ACK_CASE(2, 0, ack_generic_decode)
1474             ACK_CASE(3, 1, ack_generic_decode)
1475                 ACK_CASE(4, 0, ack_generic_decode)
1476                     ACK_CASE(5, 1, ack_generic_decode)
1477                         ACK_CASE(6, 1, ack_generic_decode)
1478                             ACK_CASE(7, 0, ack_generic_decode)
1479 };
1480 
test_wire_ack(int idx)1481 static int test_wire_ack(int idx)
1482 {
1483     int testresult = 0, r;
1484     PACKET pkt;
1485     const struct ack_test_case *c = &ack_cases[idx];
1486 
1487     if (!TEST_int_eq(PACKET_buf_init(&pkt,
1488                          (unsigned char *)c->input_buf,
1489                          c->input_buf_len),
1490             1))
1491         goto err;
1492 
1493     r = c->deserializer(&pkt);
1494     if (c->expect_fail) {
1495         if (!TEST_int_eq(r, 0))
1496             goto err;
1497     } else {
1498         if (!TEST_int_eq(r, 1))
1499             goto err;
1500 
1501         if (!TEST_false(PACKET_remaining(&pkt)))
1502             goto err;
1503     }
1504 
1505     testresult = 1;
1506 err:
1507     return testresult;
1508 }
1509 
1510 /* Packet Header PN Encoding Tests */
1511 struct pn_test {
1512     QUIC_PN pn, tx_largest_acked, rx_largest_pn;
1513     char expected_len;
1514     unsigned char expected_bytes[4];
1515 };
1516 
1517 static const struct pn_test pn_tests[] = {
1518     /* RFC 9000 Section A.2 */
1519     { 0xac5c02, 0xabe8b3, 0xabe8b3, 2, { 0x5c, 0x02 } },
1520     { 0xace8fe, 0xabe8b3, 0xabe8b3, 3, { 0xac, 0xe8, 0xfe } },
1521     /* RFC 9000 Section A.3 */
1522     { 0xa82f9b32, 0xa82f30ea, 0xa82f30ea, 2, { 0x9b, 0x32 } },
1523     /* Boundary Cases */
1524     { 1, 0, 0, 1, { 0x01 } },
1525     { 256, 255, 255, 1, { 0x00 } },
1526     { 257, 255, 255, 1, { 0x01 } },
1527     { 256, 128, 128, 1, { 0x00 } },
1528     { 256, 127, 127, 2, { 0x01, 0x00 } },
1529     { 65536, 32768, 32768, 2, { 0x00, 0x00 } },
1530     { 65537, 32769, 32769, 2, { 0x00, 0x01 } },
1531     { 65536, 32767, 32767, 3, { 0x01, 0x00, 0x00 } },
1532     { 65537, 32768, 32768, 3, { 0x01, 0x00, 0x01 } },
1533     { 16777216, 8388608, 8388608, 3, { 0x00, 0x00, 0x00 } },
1534     { 16777217, 8388609, 8388609, 3, { 0x00, 0x00, 0x01 } },
1535     { 16777216, 8388607, 8388607, 4, { 0x01, 0x00, 0x00, 0x00 } },
1536     { 16777217, 8388608, 8388608, 4, { 0x01, 0x00, 0x00, 0x01 } },
1537     { 4294967296, 2147483648, 2147483648, 4, { 0x00, 0x00, 0x00, 0x00 } },
1538     { 4294967297, 2147483648, 2147483648, 4, { 0x00, 0x00, 0x00, 0x01 } },
1539 };
1540 
test_wire_pkt_hdr_pn(int tidx)1541 static int test_wire_pkt_hdr_pn(int tidx)
1542 {
1543     int testresult = 0;
1544     const struct pn_test *t = &pn_tests[tidx];
1545     unsigned char buf[4];
1546     int pn_len;
1547     QUIC_PN res_pn;
1548 
1549     pn_len = ossl_quic_wire_determine_pn_len(t->pn, t->tx_largest_acked);
1550     if (!TEST_int_eq(pn_len, (int)t->expected_len))
1551         goto err;
1552 
1553     if (!TEST_true(ossl_quic_wire_encode_pkt_hdr_pn(t->pn, buf, pn_len)))
1554         goto err;
1555 
1556     if (!TEST_mem_eq(t->expected_bytes, t->expected_len, buf, pn_len))
1557         goto err;
1558 
1559     if (!TEST_true(ossl_quic_wire_decode_pkt_hdr_pn(buf, pn_len,
1560             t->rx_largest_pn, &res_pn)))
1561         goto err;
1562 
1563     if (!TEST_uint64_t_eq(res_pn, t->pn))
1564         goto err;
1565 
1566     testresult = 1;
1567 err:
1568     return testresult;
1569 }
1570 
1571 /* RFC 9001 s. A.4 */
1572 static const QUIC_CONN_ID retry_orig_dcid = {
1573     8, { 0x83, 0x94, 0xc8, 0xf0, 0x3e, 0x51, 0x57, 0x08 }
1574 };
1575 
1576 static const unsigned char retry_encoded[] = {
1577     0xff, /* Long Header, Retry */
1578     0x00, 0x00, 0x00, 0x01, /* Version 1 */
1579     0x00, /* DCID */
1580     0x08, 0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5, /* SCID */
1581 
1582     /* Retry Token */
1583     0x74, 0x6f, 0x6b, 0x65, 0x6e,
1584 
1585     /* Retry Integrity Tag */
1586     0x04, 0xa2, 0x65, 0xba, 0x2e, 0xff, 0x4d, 0x82, 0x90, 0x58, 0xfb, 0x3f, 0x0f,
1587     0x24, 0x96, 0xba
1588 };
1589 
test_wire_retry_integrity_tag(void)1590 static int test_wire_retry_integrity_tag(void)
1591 {
1592     int testresult = 0;
1593     PACKET pkt = { 0 };
1594     QUIC_PKT_HDR hdr = { 0 };
1595     unsigned char got_tag[QUIC_RETRY_INTEGRITY_TAG_LEN] = { 0 };
1596 
1597     if (!TEST_true(PACKET_buf_init(&pkt, retry_encoded, sizeof(retry_encoded))))
1598         goto err;
1599 
1600     if (!TEST_true(ossl_quic_wire_decode_pkt_hdr(&pkt, 0, 0, 0, &hdr, NULL, NULL)))
1601         goto err;
1602 
1603     if (!TEST_int_eq(hdr.type, QUIC_PKT_TYPE_RETRY))
1604         goto err;
1605 
1606     if (!TEST_true(ossl_quic_calculate_retry_integrity_tag(NULL, NULL, &hdr,
1607             &retry_orig_dcid,
1608             got_tag)))
1609         goto err;
1610 
1611     if (!TEST_mem_eq(got_tag, sizeof(got_tag),
1612             retry_encoded + sizeof(retry_encoded)
1613                 - QUIC_RETRY_INTEGRITY_TAG_LEN,
1614             QUIC_RETRY_INTEGRITY_TAG_LEN))
1615         goto err;
1616 
1617     if (!TEST_true(ossl_quic_validate_retry_integrity_tag(NULL, NULL, &hdr,
1618             &retry_orig_dcid)))
1619         goto err;
1620 
1621     testresult = 1;
1622 err:
1623     return testresult;
1624 }
1625 
1626 /* is_minimal=0 test */
1627 static const unsigned char non_minimal_1[] = {
1628     0x40,
1629     0x00,
1630 };
1631 
1632 static const unsigned char non_minimal_2[] = {
1633     0x40,
1634     0x3F,
1635 };
1636 
1637 static const unsigned char non_minimal_3[] = {
1638     0x80,
1639     0x00,
1640     0x00,
1641     0x00,
1642 };
1643 
1644 static const unsigned char non_minimal_4[] = {
1645     0x80,
1646     0x00,
1647     0x3F,
1648     0xFF,
1649 };
1650 
1651 static const unsigned char non_minimal_5[] = {
1652     0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
1653 };
1654 
1655 static const unsigned char non_minimal_6[] = {
1656     0xC0, 0x00, 0x00, 0x00, 0x3F, 0xFF, 0xFF, 0xFF
1657 };
1658 
1659 static const unsigned char *const non_minimal[] = {
1660     non_minimal_1,
1661     non_minimal_2,
1662     non_minimal_3,
1663     non_minimal_4,
1664     non_minimal_5,
1665     non_minimal_6,
1666 };
1667 
1668 static const size_t non_minimal_len[] = {
1669     OSSL_NELEM(non_minimal_1),
1670     OSSL_NELEM(non_minimal_2),
1671     OSSL_NELEM(non_minimal_3),
1672     OSSL_NELEM(non_minimal_4),
1673     OSSL_NELEM(non_minimal_5),
1674     OSSL_NELEM(non_minimal_6),
1675 };
1676 
test_wire_minimal(int idx)1677 static int test_wire_minimal(int idx)
1678 {
1679     int testresult = 0;
1680     int is_minimal;
1681     uint64_t frame_type;
1682     PACKET pkt;
1683 
1684     if (!TEST_true(PACKET_buf_init(&pkt, non_minimal[idx],
1685             non_minimal_len[idx])))
1686         goto err;
1687 
1688     if (!TEST_true(ossl_quic_wire_peek_frame_header(&pkt, &frame_type,
1689             &is_minimal)))
1690         goto err;
1691 
1692     if (!TEST_false(is_minimal))
1693         goto err;
1694 
1695     testresult = 1;
1696 err:
1697     return testresult;
1698 }
1699 
setup_tests(void)1700 int setup_tests(void)
1701 {
1702     ADD_ALL_TESTS(test_wire_encode, OSSL_NELEM(encode_cases));
1703     ADD_ALL_TESTS(test_wire_ack, OSSL_NELEM(ack_cases));
1704     ADD_ALL_TESTS(test_wire_pkt_hdr_pn, OSSL_NELEM(pn_tests));
1705     ADD_TEST(test_wire_retry_integrity_tag);
1706     ADD_ALL_TESTS(test_wire_minimal, OSSL_NELEM(non_minimal_len));
1707     return 1;
1708 }
1709