/src/openssl/ssl/quic/quic_wire.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * Copyright 2022-2024 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 <openssl/macros.h>  | 
11  |  | #include <openssl/objects.h>  | 
12  |  | #include "internal/quic_ssl.h"  | 
13  |  | #include "internal/quic_vlint.h"  | 
14  |  | #include "internal/quic_wire.h"  | 
15  |  | #include "internal/quic_error.h"  | 
16  |  |  | 
17  |  | OSSL_SAFE_MATH_UNSIGNED(uint64_t, uint64_t)  | 
18  |  |  | 
19  |  | int ossl_quic_frame_ack_contains_pn(const OSSL_QUIC_FRAME_ACK *ack, QUIC_PN pn)  | 
20  | 0  | { | 
21  | 0  |     size_t i;  | 
22  |  | 
  | 
23  | 0  |     for (i = 0; i < ack->num_ack_ranges; ++i)  | 
24  | 0  |         if (pn >= ack->ack_ranges[i].start  | 
25  | 0  |             && pn <= ack->ack_ranges[i].end)  | 
26  | 0  |             return 1;  | 
27  |  |  | 
28  | 0  |     return 0;  | 
29  | 0  | }  | 
30  |  |  | 
31  |  | /*  | 
32  |  |  * QUIC Wire Format Encoding  | 
33  |  |  * =========================  | 
34  |  |  */  | 
35  |  |  | 
36  |  | int ossl_quic_wire_encode_padding(WPACKET *pkt, size_t num_bytes)  | 
37  | 0  | { | 
38  |  |     /*  | 
39  |  |      * PADDING is frame type zero, which as a variable-length integer is  | 
40  |  |      * represented as a single zero byte. As an optimisation, just use memset.  | 
41  |  |      */  | 
42  | 0  |     return WPACKET_memset(pkt, 0, num_bytes);  | 
43  | 0  | }  | 
44  |  |  | 
45  |  | static int encode_frame_hdr(WPACKET *pkt, uint64_t frame_type)  | 
46  | 0  | { | 
47  | 0  |     return WPACKET_quic_write_vlint(pkt, frame_type);  | 
48  | 0  | }  | 
49  |  |  | 
50  |  | int ossl_quic_wire_encode_frame_ping(WPACKET *pkt)  | 
51  | 0  | { | 
52  | 0  |     return encode_frame_hdr(pkt, OSSL_QUIC_FRAME_TYPE_PING);  | 
53  | 0  | }  | 
54  |  |  | 
55  |  | int ossl_quic_wire_encode_frame_ack(WPACKET *pkt,  | 
56  |  |                                     uint32_t ack_delay_exponent,  | 
57  |  |                                     const OSSL_QUIC_FRAME_ACK *ack)  | 
58  | 0  | { | 
59  | 0  |     uint64_t frame_type = ack->ecn_present ? OSSL_QUIC_FRAME_TYPE_ACK_WITH_ECN  | 
60  | 0  |                                            : OSSL_QUIC_FRAME_TYPE_ACK_WITHOUT_ECN;  | 
61  |  | 
  | 
62  | 0  |     uint64_t largest_ackd, first_ack_range, ack_delay_enc;  | 
63  | 0  |     uint64_t i, num_ack_ranges = ack->num_ack_ranges;  | 
64  | 0  |     OSSL_TIME delay;  | 
65  |  | 
  | 
66  | 0  |     if (num_ack_ranges == 0)  | 
67  | 0  |         return 0;  | 
68  |  |  | 
69  | 0  |     delay = ossl_time_divide(ossl_time_divide(ack->delay_time, OSSL_TIME_US),  | 
70  | 0  |                              (uint64_t)1 << ack_delay_exponent);  | 
71  | 0  |     ack_delay_enc   = ossl_time2ticks(delay);  | 
72  |  | 
  | 
73  | 0  |     largest_ackd    = ack->ack_ranges[0].end;  | 
74  | 0  |     first_ack_range = ack->ack_ranges[0].end - ack->ack_ranges[0].start;  | 
75  |  | 
  | 
76  | 0  |     if (!encode_frame_hdr(pkt, frame_type)  | 
77  | 0  |             || !WPACKET_quic_write_vlint(pkt, largest_ackd)  | 
78  | 0  |             || !WPACKET_quic_write_vlint(pkt, ack_delay_enc)  | 
79  | 0  |             || !WPACKET_quic_write_vlint(pkt, num_ack_ranges - 1)  | 
80  | 0  |             || !WPACKET_quic_write_vlint(pkt, first_ack_range))  | 
81  | 0  |         return 0;  | 
82  |  |  | 
83  | 0  |     for (i = 1; i < num_ack_ranges; ++i) { | 
84  | 0  |         uint64_t gap, range_len;  | 
85  |  | 
  | 
86  | 0  |         gap         = ack->ack_ranges[i - 1].start - ack->ack_ranges[i].end - 2;  | 
87  | 0  |         range_len   = ack->ack_ranges[i].end - ack->ack_ranges[i].start;  | 
88  |  | 
  | 
89  | 0  |         if (!WPACKET_quic_write_vlint(pkt, gap)  | 
90  | 0  |                 || !WPACKET_quic_write_vlint(pkt, range_len))  | 
91  | 0  |             return 0;  | 
92  | 0  |     }  | 
93  |  |  | 
94  | 0  |     if (ack->ecn_present)  | 
95  | 0  |         if (!WPACKET_quic_write_vlint(pkt, ack->ect0)  | 
96  | 0  |                 || !WPACKET_quic_write_vlint(pkt, ack->ect1)  | 
97  | 0  |                 || !WPACKET_quic_write_vlint(pkt, ack->ecnce))  | 
98  | 0  |             return 0;  | 
99  |  |  | 
100  | 0  |     return 1;  | 
101  | 0  | }  | 
102  |  |  | 
103  |  | int ossl_quic_wire_encode_frame_reset_stream(WPACKET *pkt,  | 
104  |  |                                              const OSSL_QUIC_FRAME_RESET_STREAM *f)  | 
105  | 0  | { | 
106  | 0  |     if (!encode_frame_hdr(pkt, OSSL_QUIC_FRAME_TYPE_RESET_STREAM)  | 
107  | 0  |             || !WPACKET_quic_write_vlint(pkt, f->stream_id)  | 
108  | 0  |             || !WPACKET_quic_write_vlint(pkt, f->app_error_code)  | 
109  | 0  |             || !WPACKET_quic_write_vlint(pkt, f->final_size))  | 
110  | 0  |         return 0;  | 
111  |  |  | 
112  | 0  |     return 1;  | 
113  | 0  | }  | 
114  |  |  | 
115  |  | int ossl_quic_wire_encode_frame_stop_sending(WPACKET *pkt,  | 
116  |  |                                              const OSSL_QUIC_FRAME_STOP_SENDING *f)  | 
117  | 0  | { | 
118  | 0  |     if (!encode_frame_hdr(pkt, OSSL_QUIC_FRAME_TYPE_STOP_SENDING)  | 
119  | 0  |             || !WPACKET_quic_write_vlint(pkt, f->stream_id)  | 
120  | 0  |             || !WPACKET_quic_write_vlint(pkt, f->app_error_code))  | 
121  | 0  |         return 0;  | 
122  |  |  | 
123  | 0  |     return 1;  | 
124  | 0  | }  | 
125  |  |  | 
126  |  | int ossl_quic_wire_encode_frame_crypto_hdr(WPACKET *pkt,  | 
127  |  |                                            const OSSL_QUIC_FRAME_CRYPTO *f)  | 
128  | 0  | { | 
129  | 0  |     if (!encode_frame_hdr(pkt, OSSL_QUIC_FRAME_TYPE_CRYPTO)  | 
130  | 0  |             || !WPACKET_quic_write_vlint(pkt, f->offset)  | 
131  | 0  |             || !WPACKET_quic_write_vlint(pkt, f->len))  | 
132  | 0  |         return 0;  | 
133  |  |  | 
134  | 0  |     return 1;  | 
135  | 0  | }  | 
136  |  |  | 
137  |  | size_t ossl_quic_wire_get_encoded_frame_len_crypto_hdr(const OSSL_QUIC_FRAME_CRYPTO *f)  | 
138  | 0  | { | 
139  | 0  |     size_t a, b, c;  | 
140  |  | 
  | 
141  | 0  |     a = ossl_quic_vlint_encode_len(OSSL_QUIC_FRAME_TYPE_CRYPTO);  | 
142  | 0  |     b = ossl_quic_vlint_encode_len(f->offset);  | 
143  | 0  |     c = ossl_quic_vlint_encode_len(f->len);  | 
144  | 0  |     if (a == 0 || b == 0 || c == 0)  | 
145  | 0  |         return 0;  | 
146  |  |  | 
147  | 0  |     return a + b + c;  | 
148  | 0  | }  | 
149  |  |  | 
150  |  | void *ossl_quic_wire_encode_frame_crypto(WPACKET *pkt,  | 
151  |  |                                          const OSSL_QUIC_FRAME_CRYPTO *f)  | 
152  | 0  | { | 
153  | 0  |     unsigned char *p = NULL;  | 
154  |  | 
  | 
155  | 0  |     if (!ossl_quic_wire_encode_frame_crypto_hdr(pkt, f)  | 
156  | 0  |             || f->len > SIZE_MAX /* sizeof(uint64_t) > sizeof(size_t)? */  | 
157  | 0  |             || !WPACKET_allocate_bytes(pkt, (size_t)f->len, &p))  | 
158  | 0  |         return NULL;  | 
159  |  |  | 
160  | 0  |     if (f->data != NULL)  | 
161  | 0  |         memcpy(p, f->data, (size_t)f->len);  | 
162  |  | 
  | 
163  | 0  |     return p;  | 
164  | 0  | }  | 
165  |  |  | 
166  |  | int ossl_quic_wire_encode_frame_new_token(WPACKET *pkt,  | 
167  |  |                                           const unsigned char *token,  | 
168  |  |                                           size_t token_len)  | 
169  | 0  | { | 
170  | 0  |     if (!encode_frame_hdr(pkt, OSSL_QUIC_FRAME_TYPE_NEW_TOKEN)  | 
171  | 0  |             || !WPACKET_quic_write_vlint(pkt, token_len)  | 
172  | 0  |             || !WPACKET_memcpy(pkt, token, token_len))  | 
173  | 0  |         return 0;  | 
174  |  |  | 
175  | 0  |     return 1;  | 
176  | 0  | }  | 
177  |  |  | 
178  |  | int ossl_quic_wire_encode_frame_stream_hdr(WPACKET *pkt,  | 
179  |  |                                            const OSSL_QUIC_FRAME_STREAM *f)  | 
180  | 0  | { | 
181  | 0  |     uint64_t frame_type = OSSL_QUIC_FRAME_TYPE_STREAM;  | 
182  |  | 
  | 
183  | 0  |     if (f->offset != 0)  | 
184  | 0  |         frame_type |= OSSL_QUIC_FRAME_FLAG_STREAM_OFF;  | 
185  | 0  |     if (f->has_explicit_len)  | 
186  | 0  |         frame_type |= OSSL_QUIC_FRAME_FLAG_STREAM_LEN;  | 
187  | 0  |     if (f->is_fin)  | 
188  | 0  |         frame_type |= OSSL_QUIC_FRAME_FLAG_STREAM_FIN;  | 
189  |  | 
  | 
190  | 0  |     if (!encode_frame_hdr(pkt, frame_type)  | 
191  | 0  |             || !WPACKET_quic_write_vlint(pkt, f->stream_id))  | 
192  | 0  |         return 0;  | 
193  |  |  | 
194  | 0  |     if (f->offset != 0 && !WPACKET_quic_write_vlint(pkt, f->offset))  | 
195  | 0  |         return 0;  | 
196  |  |  | 
197  | 0  |     if (f->has_explicit_len && !WPACKET_quic_write_vlint(pkt, f->len))  | 
198  | 0  |         return 0;  | 
199  |  |  | 
200  | 0  |     return 1;  | 
201  | 0  | }  | 
202  |  |  | 
203  |  | size_t ossl_quic_wire_get_encoded_frame_len_stream_hdr(const OSSL_QUIC_FRAME_STREAM *f)  | 
204  | 0  | { | 
205  | 0  |     size_t a, b, c, d;  | 
206  |  | 
  | 
207  | 0  |     a = ossl_quic_vlint_encode_len(OSSL_QUIC_FRAME_TYPE_STREAM);  | 
208  | 0  |     b = ossl_quic_vlint_encode_len(f->stream_id);  | 
209  | 0  |     if (a == 0 || b == 0)  | 
210  | 0  |         return 0;  | 
211  |  |  | 
212  | 0  |     if (f->offset > 0) { | 
213  | 0  |         c = ossl_quic_vlint_encode_len(f->offset);  | 
214  | 0  |         if (c == 0)  | 
215  | 0  |             return 0;  | 
216  | 0  |     } else { | 
217  | 0  |         c = 0;  | 
218  | 0  |     }  | 
219  |  |  | 
220  | 0  |     if (f->has_explicit_len) { | 
221  | 0  |         d = ossl_quic_vlint_encode_len(f->len);  | 
222  | 0  |         if (d == 0)  | 
223  | 0  |             return 0;  | 
224  | 0  |     } else { | 
225  | 0  |         d = 0;  | 
226  | 0  |     }  | 
227  |  |  | 
228  | 0  |     return a + b + c + d;  | 
229  | 0  | }  | 
230  |  |  | 
231  |  | void *ossl_quic_wire_encode_frame_stream(WPACKET *pkt,  | 
232  |  |                                          const OSSL_QUIC_FRAME_STREAM *f)  | 
233  | 0  | { | 
234  |  | 
  | 
235  | 0  |     unsigned char *p = NULL;  | 
236  |  | 
  | 
237  | 0  |     if (!ossl_quic_wire_encode_frame_stream_hdr(pkt, f)  | 
238  | 0  |             || f->len > SIZE_MAX /* sizeof(uint64_t) > sizeof(size_t)? */)  | 
239  | 0  |         return NULL;  | 
240  |  |  | 
241  | 0  |     if (!WPACKET_allocate_bytes(pkt, (size_t)f->len, &p))  | 
242  | 0  |         return NULL;  | 
243  |  |  | 
244  | 0  |     if (f->data != NULL)  | 
245  | 0  |         memcpy(p, f->data, (size_t)f->len);  | 
246  |  | 
  | 
247  | 0  |     return p;  | 
248  | 0  | }  | 
249  |  |  | 
250  |  | int ossl_quic_wire_encode_frame_max_data(WPACKET *pkt,  | 
251  |  |                                          uint64_t max_data)  | 
252  | 0  | { | 
253  | 0  |     if (!encode_frame_hdr(pkt, OSSL_QUIC_FRAME_TYPE_MAX_DATA)  | 
254  | 0  |             || !WPACKET_quic_write_vlint(pkt, max_data))  | 
255  | 0  |         return 0;  | 
256  |  |  | 
257  | 0  |     return 1;  | 
258  | 0  | }  | 
259  |  |  | 
260  |  | int ossl_quic_wire_encode_frame_max_stream_data(WPACKET *pkt,  | 
261  |  |                                                 uint64_t stream_id,  | 
262  |  |                                                 uint64_t max_data)  | 
263  | 0  | { | 
264  | 0  |     if (!encode_frame_hdr(pkt, OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA)  | 
265  | 0  |             || !WPACKET_quic_write_vlint(pkt, stream_id)  | 
266  | 0  |             || !WPACKET_quic_write_vlint(pkt, max_data))  | 
267  | 0  |         return 0;  | 
268  |  |  | 
269  | 0  |     return 1;  | 
270  | 0  | }  | 
271  |  |  | 
272  |  | int ossl_quic_wire_encode_frame_max_streams(WPACKET *pkt,  | 
273  |  |                                             char     is_uni,  | 
274  |  |                                             uint64_t max_streams)  | 
275  | 0  | { | 
276  | 0  |     if (!encode_frame_hdr(pkt, is_uni ? OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI  | 
277  | 0  |                                       : OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI)  | 
278  | 0  |             || !WPACKET_quic_write_vlint(pkt, max_streams))  | 
279  | 0  |         return 0;  | 
280  |  |  | 
281  | 0  |     return 1;  | 
282  | 0  | }  | 
283  |  |  | 
284  |  | int ossl_quic_wire_encode_frame_data_blocked(WPACKET *pkt,  | 
285  |  |                                              uint64_t max_data)  | 
286  | 0  | { | 
287  | 0  |     if (!encode_frame_hdr(pkt, OSSL_QUIC_FRAME_TYPE_DATA_BLOCKED)  | 
288  | 0  |             || !WPACKET_quic_write_vlint(pkt, max_data))  | 
289  | 0  |         return 0;  | 
290  |  |  | 
291  | 0  |     return 1;  | 
292  | 0  | }  | 
293  |  |  | 
294  |  |  | 
295  |  | int ossl_quic_wire_encode_frame_stream_data_blocked(WPACKET *pkt,  | 
296  |  |                                                     uint64_t stream_id,  | 
297  |  |                                                     uint64_t max_stream_data)  | 
298  | 0  | { | 
299  | 0  |     if (!encode_frame_hdr(pkt, OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED)  | 
300  | 0  |             || !WPACKET_quic_write_vlint(pkt, stream_id)  | 
301  | 0  |             || !WPACKET_quic_write_vlint(pkt, max_stream_data))  | 
302  | 0  |         return 0;  | 
303  |  |  | 
304  | 0  |     return 1;  | 
305  | 0  | }  | 
306  |  |  | 
307  |  | int ossl_quic_wire_encode_frame_streams_blocked(WPACKET *pkt,  | 
308  |  |                                                 char is_uni,  | 
309  |  |                                                 uint64_t max_streams)  | 
310  | 0  | { | 
311  | 0  |     if (!encode_frame_hdr(pkt, is_uni ? OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_UNI  | 
312  | 0  |                                       : OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_BIDI)  | 
313  | 0  |             || !WPACKET_quic_write_vlint(pkt, max_streams))  | 
314  | 0  |         return 0;  | 
315  |  |  | 
316  | 0  |     return 1;  | 
317  | 0  | }  | 
318  |  |  | 
319  |  | int ossl_quic_wire_encode_frame_new_conn_id(WPACKET *pkt,  | 
320  |  |                                             const OSSL_QUIC_FRAME_NEW_CONN_ID *f)  | 
321  | 0  | { | 
322  | 0  |     if (f->conn_id.id_len < 1  | 
323  | 0  |         || f->conn_id.id_len > QUIC_MAX_CONN_ID_LEN)  | 
324  | 0  |         return 0;  | 
325  |  |  | 
326  | 0  |     if (!encode_frame_hdr(pkt, OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID)  | 
327  | 0  |             || !WPACKET_quic_write_vlint(pkt, f->seq_num)  | 
328  | 0  |             || !WPACKET_quic_write_vlint(pkt, f->retire_prior_to)  | 
329  | 0  |             || !WPACKET_put_bytes_u8(pkt, f->conn_id.id_len)  | 
330  | 0  |             || !WPACKET_memcpy(pkt, f->conn_id.id, f->conn_id.id_len)  | 
331  | 0  |             || !WPACKET_memcpy(pkt, f->stateless_reset.token,  | 
332  | 0  |                                sizeof(f->stateless_reset.token)))  | 
333  | 0  |         return 0;  | 
334  |  |  | 
335  | 0  |     return 1;  | 
336  | 0  | }  | 
337  |  |  | 
338  |  | int ossl_quic_wire_encode_frame_retire_conn_id(WPACKET *pkt,  | 
339  |  |                                                uint64_t seq_num)  | 
340  | 0  | { | 
341  | 0  |     if (!encode_frame_hdr(pkt, OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID)  | 
342  | 0  |             || !WPACKET_quic_write_vlint(pkt, seq_num))  | 
343  | 0  |         return 0;  | 
344  |  |  | 
345  | 0  |     return 1;  | 
346  | 0  | }  | 
347  |  |  | 
348  |  | int ossl_quic_wire_encode_frame_path_challenge(WPACKET *pkt,  | 
349  |  |                                                uint64_t data)  | 
350  | 0  | { | 
351  | 0  |     if (!encode_frame_hdr(pkt, OSSL_QUIC_FRAME_TYPE_PATH_CHALLENGE)  | 
352  | 0  |             || !WPACKET_put_bytes_u64(pkt, data))  | 
353  | 0  |         return 0;  | 
354  |  |  | 
355  | 0  |     return 1;  | 
356  | 0  | }  | 
357  |  |  | 
358  |  | int ossl_quic_wire_encode_frame_path_response(WPACKET *pkt,  | 
359  |  |                                               uint64_t data)  | 
360  | 0  | { | 
361  | 0  |     if (!encode_frame_hdr(pkt, OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE)  | 
362  | 0  |             || !WPACKET_put_bytes_u64(pkt, data))  | 
363  | 0  |         return 0;  | 
364  |  |  | 
365  | 0  |     return 1;  | 
366  | 0  | }  | 
367  |  |  | 
368  |  | int ossl_quic_wire_encode_frame_conn_close(WPACKET *pkt,  | 
369  |  |                                            const OSSL_QUIC_FRAME_CONN_CLOSE *f)  | 
370  | 0  | { | 
371  | 0  |     if (!encode_frame_hdr(pkt, f->is_app ? OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_APP  | 
372  | 0  |                                          : OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_TRANSPORT)  | 
373  | 0  |             || !WPACKET_quic_write_vlint(pkt, f->error_code))  | 
374  | 0  |         return 0;  | 
375  |  |  | 
376  |  |     /*  | 
377  |  |      * RFC 9000 s. 19.19: The application-specific variant of CONNECTION_CLOSE  | 
378  |  |      * (type 0x1d) does not include this field.  | 
379  |  |      */  | 
380  | 0  |     if (!f->is_app && !WPACKET_quic_write_vlint(pkt, f->frame_type))  | 
381  | 0  |         return 0;  | 
382  |  |  | 
383  | 0  |     if (!WPACKET_quic_write_vlint(pkt, f->reason_len)  | 
384  | 0  |             || !WPACKET_memcpy(pkt, f->reason, f->reason_len))  | 
385  | 0  |         return 0;  | 
386  |  |  | 
387  | 0  |     return 1;  | 
388  | 0  | }  | 
389  |  |  | 
390  |  | int ossl_quic_wire_encode_frame_handshake_done(WPACKET *pkt)  | 
391  | 0  | { | 
392  | 0  |     return encode_frame_hdr(pkt, OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE);  | 
393  | 0  | }  | 
394  |  |  | 
395  |  | unsigned char *ossl_quic_wire_encode_transport_param_bytes(WPACKET *pkt,  | 
396  |  |                                                            uint64_t id,  | 
397  |  |                                                            const unsigned char *value,  | 
398  |  |                                                            size_t value_len)  | 
399  | 0  | { | 
400  | 0  |     unsigned char *b = NULL;  | 
401  |  | 
  | 
402  | 0  |     if (!WPACKET_quic_write_vlint(pkt, id)  | 
403  | 0  |         || !WPACKET_quic_write_vlint(pkt, value_len))  | 
404  | 0  |         return NULL;  | 
405  |  |  | 
406  | 0  |     if (value_len == 0)  | 
407  | 0  |         b = WPACKET_get_curr(pkt);  | 
408  | 0  |     else if (!WPACKET_allocate_bytes(pkt, value_len, (unsigned char **)&b))  | 
409  | 0  |         return NULL;  | 
410  |  |  | 
411  | 0  |     if (value != NULL)  | 
412  | 0  |         memcpy(b, value, value_len);  | 
413  |  | 
  | 
414  | 0  |     return b;  | 
415  | 0  | }  | 
416  |  |  | 
417  |  | int ossl_quic_wire_encode_transport_param_int(WPACKET *pkt,  | 
418  |  |                                               uint64_t id,  | 
419  |  |                                               uint64_t value)  | 
420  | 0  | { | 
421  | 0  |     if (!WPACKET_quic_write_vlint(pkt, id)  | 
422  | 0  |             || !WPACKET_quic_write_vlint(pkt, ossl_quic_vlint_encode_len(value))  | 
423  | 0  |             || !WPACKET_quic_write_vlint(pkt, value))  | 
424  | 0  |         return 0;  | 
425  |  |  | 
426  | 0  |     return 1;  | 
427  | 0  | }  | 
428  |  |  | 
429  |  | int ossl_quic_wire_encode_transport_param_cid(WPACKET *wpkt,  | 
430  |  |                                               uint64_t id,  | 
431  |  |                                               const QUIC_CONN_ID *cid)  | 
432  | 0  | { | 
433  | 0  |     if (cid->id_len > QUIC_MAX_CONN_ID_LEN)  | 
434  | 0  |         return 0;  | 
435  |  |  | 
436  | 0  |     if (ossl_quic_wire_encode_transport_param_bytes(wpkt, id,  | 
437  | 0  |                                                     cid->id,  | 
438  | 0  |                                                     cid->id_len) == NULL)  | 
439  | 0  |         return 0;  | 
440  |  |  | 
441  | 0  |     return 1;  | 
442  | 0  | }  | 
443  |  |  | 
444  |  | /*  | 
445  |  |  * QUIC Wire Format Decoding  | 
446  |  |  * =========================  | 
447  |  |  */  | 
448  |  | int ossl_quic_wire_peek_frame_header(PACKET *pkt, uint64_t *type,  | 
449  |  |                                      int *was_minimal)  | 
450  | 0  | { | 
451  | 0  |     return PACKET_peek_quic_vlint_ex(pkt, type, was_minimal);  | 
452  | 0  | }  | 
453  |  |  | 
454  |  | int ossl_quic_wire_skip_frame_header(PACKET *pkt, uint64_t *type)  | 
455  | 0  | { | 
456  | 0  |     return PACKET_get_quic_vlint(pkt, type);  | 
457  | 0  | }  | 
458  |  |  | 
459  |  | static int expect_frame_header_mask(PACKET *pkt,  | 
460  |  |                                     uint64_t expected_frame_type,  | 
461  |  |                                     uint64_t mask_bits,  | 
462  |  |                                     uint64_t *actual_frame_type)  | 
463  | 0  | { | 
464  | 0  |     uint64_t actual_frame_type_;  | 
465  |  | 
  | 
466  | 0  |     if (!ossl_quic_wire_skip_frame_header(pkt, &actual_frame_type_)  | 
467  | 0  |             || (actual_frame_type_ & ~mask_bits) != expected_frame_type)  | 
468  | 0  |         return 0;  | 
469  |  |  | 
470  | 0  |     if (actual_frame_type != NULL)  | 
471  | 0  |         *actual_frame_type = actual_frame_type_;  | 
472  |  | 
  | 
473  | 0  |     return 1;  | 
474  | 0  | }  | 
475  |  |  | 
476  |  | static int expect_frame_header(PACKET *pkt, uint64_t expected_frame_type)  | 
477  | 0  | { | 
478  | 0  |     uint64_t actual_frame_type;  | 
479  |  | 
  | 
480  | 0  |     if (!ossl_quic_wire_skip_frame_header(pkt, &actual_frame_type)  | 
481  | 0  |             || actual_frame_type != expected_frame_type)  | 
482  | 0  |         return 0;  | 
483  |  |  | 
484  | 0  |     return 1;  | 
485  | 0  | }  | 
486  |  |  | 
487  |  | int ossl_quic_wire_peek_frame_ack_num_ranges(const PACKET *orig_pkt,  | 
488  |  |                                              uint64_t *total_ranges)  | 
489  | 0  | { | 
490  | 0  |     PACKET pkt = *orig_pkt;  | 
491  | 0  |     uint64_t ack_range_count, i;  | 
492  |  | 
  | 
493  | 0  |     if (!expect_frame_header_mask(&pkt, OSSL_QUIC_FRAME_TYPE_ACK_WITHOUT_ECN,  | 
494  | 0  |                                   1, NULL)  | 
495  | 0  |             || !PACKET_skip_quic_vlint(&pkt)  | 
496  | 0  |             || !PACKET_skip_quic_vlint(&pkt)  | 
497  | 0  |             || !PACKET_get_quic_vlint(&pkt, &ack_range_count))  | 
498  | 0  |         return 0;  | 
499  |  |  | 
500  |  |     /*  | 
501  |  |      * Ensure the specified number of ack ranges listed in the ACK frame header  | 
502  |  |      * actually are available in the frame data. This naturally bounds the  | 
503  |  |      * number of ACK ranges which can be requested by the MDPL, and therefore by  | 
504  |  |      * the MTU. This ensures we do not allocate memory for an excessive number  | 
505  |  |      * of ACK ranges.  | 
506  |  |      */  | 
507  | 0  |     for (i = 0; i < ack_range_count; ++i)  | 
508  | 0  |         if (!PACKET_skip_quic_vlint(&pkt)  | 
509  | 0  |             || !PACKET_skip_quic_vlint(&pkt))  | 
510  | 0  |             return 0;  | 
511  |  |  | 
512  |  |     /* (cannot overflow because QUIC vlints can only encode up to 2**62-1) */  | 
513  | 0  |     *total_ranges = ack_range_count + 1;  | 
514  | 0  |     return 1;  | 
515  | 0  | }  | 
516  |  |  | 
517  |  | int ossl_quic_wire_decode_frame_ack(PACKET *pkt,  | 
518  |  |                                     uint32_t ack_delay_exponent,  | 
519  |  |                                     OSSL_QUIC_FRAME_ACK *ack,  | 
520  | 0  |                                     uint64_t *total_ranges) { | 
521  | 0  |     uint64_t frame_type, largest_ackd, ack_delay_raw;  | 
522  | 0  |     uint64_t ack_range_count, first_ack_range, start, end, i;  | 
523  |  |  | 
524  |  |     /* This call matches both ACK_WITHOUT_ECN and ACK_WITH_ECN. */  | 
525  | 0  |     if (!expect_frame_header_mask(pkt, OSSL_QUIC_FRAME_TYPE_ACK_WITHOUT_ECN,  | 
526  | 0  |                                   1, &frame_type)  | 
527  | 0  |             || !PACKET_get_quic_vlint(pkt, &largest_ackd)  | 
528  | 0  |             || !PACKET_get_quic_vlint(pkt, &ack_delay_raw)  | 
529  | 0  |             || !PACKET_get_quic_vlint(pkt, &ack_range_count)  | 
530  | 0  |             || !PACKET_get_quic_vlint(pkt, &first_ack_range))  | 
531  | 0  |         return 0;  | 
532  |  |  | 
533  | 0  |     if (first_ack_range > largest_ackd)  | 
534  | 0  |         return 0;  | 
535  |  |  | 
536  | 0  |     if (ack_range_count > SIZE_MAX /* sizeof(uint64_t) > sizeof(size_t)? */)  | 
537  | 0  |         return 0;  | 
538  |  |  | 
539  | 0  |     start = largest_ackd - first_ack_range;  | 
540  |  | 
  | 
541  | 0  |     if (ack != NULL) { | 
542  | 0  |         int err = 0;  | 
543  | 0  |         ack->delay_time  | 
544  | 0  |             = ossl_time_multiply(ossl_ticks2time(OSSL_TIME_US),  | 
545  | 0  |                                  safe_mul_uint64_t(ack_delay_raw,  | 
546  | 0  |                                                    (uint64_t)1 << ack_delay_exponent,  | 
547  | 0  |                                                    &err));  | 
548  | 0  |         if (err)  | 
549  | 0  |             ack->delay_time = ossl_time_infinite();  | 
550  |  | 
  | 
551  | 0  |         if (ack->num_ack_ranges > 0) { | 
552  | 0  |             ack->ack_ranges[0].end   = largest_ackd;  | 
553  | 0  |             ack->ack_ranges[0].start = start;  | 
554  | 0  |         }  | 
555  | 0  |     }  | 
556  |  | 
  | 
557  | 0  |     for (i = 0; i < ack_range_count; ++i) { | 
558  | 0  |         uint64_t gap, len;  | 
559  |  | 
  | 
560  | 0  |         if (!PACKET_get_quic_vlint(pkt, &gap)  | 
561  | 0  |                 || !PACKET_get_quic_vlint(pkt, &len))  | 
562  | 0  |             return 0;  | 
563  |  |  | 
564  | 0  |         end = start - gap - 2;  | 
565  | 0  |         if (start < gap + 2 || len > end)  | 
566  | 0  |             return 0;  | 
567  |  |  | 
568  | 0  |         if (ack != NULL && i + 1 < ack->num_ack_ranges) { | 
569  | 0  |             ack->ack_ranges[i + 1].start = start = end - len;  | 
570  | 0  |             ack->ack_ranges[i + 1].end   = end;  | 
571  | 0  |         }  | 
572  | 0  |     }  | 
573  |  |  | 
574  | 0  |     if (ack != NULL && ack_range_count + 1 < ack->num_ack_ranges)  | 
575  | 0  |         ack->num_ack_ranges = (size_t)ack_range_count + 1;  | 
576  |  | 
  | 
577  | 0  |     if (total_ranges != NULL)  | 
578  | 0  |         *total_ranges = ack_range_count + 1;  | 
579  |  | 
  | 
580  | 0  |     if (frame_type == OSSL_QUIC_FRAME_TYPE_ACK_WITH_ECN) { | 
581  | 0  |         uint64_t ect0, ect1, ecnce;  | 
582  |  | 
  | 
583  | 0  |         if (!PACKET_get_quic_vlint(pkt, &ect0)  | 
584  | 0  |                 || !PACKET_get_quic_vlint(pkt, &ect1)  | 
585  | 0  |                 || !PACKET_get_quic_vlint(pkt, &ecnce))  | 
586  | 0  |             return 0;  | 
587  |  |  | 
588  | 0  |         if (ack != NULL) { | 
589  | 0  |             ack->ect0           = ect0;  | 
590  | 0  |             ack->ect1           = ect1;  | 
591  | 0  |             ack->ecnce          = ecnce;  | 
592  | 0  |             ack->ecn_present    = 1;  | 
593  | 0  |         }  | 
594  | 0  |     } else if (ack != NULL) { | 
595  | 0  |         ack->ecn_present = 0;  | 
596  | 0  |     }  | 
597  |  |  | 
598  | 0  |     return 1;  | 
599  | 0  | }  | 
600  |  |  | 
601  |  | int ossl_quic_wire_decode_frame_reset_stream(PACKET *pkt,  | 
602  |  |                                              OSSL_QUIC_FRAME_RESET_STREAM *f)  | 
603  | 0  | { | 
604  | 0  |     if (!expect_frame_header(pkt, OSSL_QUIC_FRAME_TYPE_RESET_STREAM)  | 
605  | 0  |             || !PACKET_get_quic_vlint(pkt, &f->stream_id)  | 
606  | 0  |             || !PACKET_get_quic_vlint(pkt, &f->app_error_code)  | 
607  | 0  |             || !PACKET_get_quic_vlint(pkt, &f->final_size))  | 
608  | 0  |         return 0;  | 
609  |  |  | 
610  | 0  |     return 1;  | 
611  | 0  | }  | 
612  |  |  | 
613  |  | int ossl_quic_wire_decode_frame_stop_sending(PACKET *pkt,  | 
614  |  |                                              OSSL_QUIC_FRAME_STOP_SENDING *f)  | 
615  | 0  | { | 
616  | 0  |     if (!expect_frame_header(pkt, OSSL_QUIC_FRAME_TYPE_STOP_SENDING)  | 
617  | 0  |             || !PACKET_get_quic_vlint(pkt, &f->stream_id)  | 
618  | 0  |             || !PACKET_get_quic_vlint(pkt, &f->app_error_code))  | 
619  | 0  |         return 0;  | 
620  |  |  | 
621  | 0  |     return 1;  | 
622  | 0  | }  | 
623  |  |  | 
624  |  | int ossl_quic_wire_decode_frame_crypto(PACKET *pkt,  | 
625  |  |                                        int nodata,  | 
626  |  |                                        OSSL_QUIC_FRAME_CRYPTO *f)  | 
627  | 0  | { | 
628  | 0  |     if (!expect_frame_header(pkt, OSSL_QUIC_FRAME_TYPE_CRYPTO)  | 
629  | 0  |             || !PACKET_get_quic_vlint(pkt, &f->offset)  | 
630  | 0  |             || !PACKET_get_quic_vlint(pkt, &f->len)  | 
631  | 0  |             || f->len > SIZE_MAX /* sizeof(uint64_t) > sizeof(size_t)? */)  | 
632  | 0  |         return 0;  | 
633  |  |  | 
634  | 0  |     if (f->offset + f->len > (((uint64_t)1) << 62) - 1)  | 
635  |  |         /* RFC 9000 s. 19.6 */  | 
636  | 0  |         return 0;  | 
637  |  |  | 
638  | 0  |     if (nodata) { | 
639  | 0  |         f->data = NULL;  | 
640  | 0  |     } else { | 
641  | 0  |         if (PACKET_remaining(pkt) < f->len)  | 
642  | 0  |             return 0;  | 
643  |  |  | 
644  | 0  |         f->data = PACKET_data(pkt);  | 
645  |  | 
  | 
646  | 0  |         if (!PACKET_forward(pkt, (size_t)f->len))  | 
647  | 0  |             return 0;  | 
648  | 0  |     }  | 
649  |  |  | 
650  | 0  |     return 1;  | 
651  | 0  | }  | 
652  |  |  | 
653  |  | int ossl_quic_wire_decode_frame_new_token(PACKET               *pkt,  | 
654  |  |                                           const unsigned char **token,  | 
655  |  |                                           size_t               *token_len)  | 
656  | 0  | { | 
657  | 0  |     uint64_t token_len_;  | 
658  |  | 
  | 
659  | 0  |     if (!expect_frame_header(pkt, OSSL_QUIC_FRAME_TYPE_NEW_TOKEN)  | 
660  | 0  |             || !PACKET_get_quic_vlint(pkt, &token_len_))  | 
661  | 0  |         return 0;  | 
662  |  |  | 
663  | 0  |     if (token_len_ > SIZE_MAX)  | 
664  | 0  |         return 0;  | 
665  |  |  | 
666  | 0  |     *token      = PACKET_data(pkt);  | 
667  | 0  |     *token_len  = (size_t)token_len_;  | 
668  |  | 
  | 
669  | 0  |     if (!PACKET_forward(pkt, (size_t)token_len_))  | 
670  | 0  |         return 0;  | 
671  |  |  | 
672  | 0  |     return 1;  | 
673  | 0  | }  | 
674  |  |  | 
675  |  | int ossl_quic_wire_decode_frame_stream(PACKET *pkt,  | 
676  |  |                                        int nodata,  | 
677  |  |                                        OSSL_QUIC_FRAME_STREAM *f)  | 
678  | 0  | { | 
679  | 0  |     uint64_t frame_type;  | 
680  |  |  | 
681  |  |     /* This call matches all STREAM values (low 3 bits are masked). */  | 
682  | 0  |     if (!expect_frame_header_mask(pkt, OSSL_QUIC_FRAME_TYPE_STREAM,  | 
683  | 0  |                                   OSSL_QUIC_FRAME_FLAG_STREAM_MASK,  | 
684  | 0  |                                   &frame_type)  | 
685  | 0  |             || !PACKET_get_quic_vlint(pkt, &f->stream_id))  | 
686  | 0  |         return 0;  | 
687  |  |  | 
688  | 0  |     if ((frame_type & OSSL_QUIC_FRAME_FLAG_STREAM_OFF) != 0) { | 
689  | 0  |         if (!PACKET_get_quic_vlint(pkt, &f->offset))  | 
690  | 0  |             return 0;  | 
691  | 0  |     } else { | 
692  | 0  |         f->offset = 0;  | 
693  | 0  |     }  | 
694  |  |  | 
695  | 0  |     f->has_explicit_len = ((frame_type & OSSL_QUIC_FRAME_FLAG_STREAM_LEN) != 0);  | 
696  | 0  |     f->is_fin           = ((frame_type & OSSL_QUIC_FRAME_FLAG_STREAM_FIN) != 0);  | 
697  |  | 
  | 
698  | 0  |     if (f->has_explicit_len) { | 
699  | 0  |         if (!PACKET_get_quic_vlint(pkt, &f->len))  | 
700  | 0  |             return 0;  | 
701  | 0  |     } else { | 
702  | 0  |         if (nodata)  | 
703  | 0  |             f->len = 0;  | 
704  | 0  |         else  | 
705  | 0  |             f->len = PACKET_remaining(pkt);  | 
706  | 0  |     }  | 
707  |  |  | 
708  |  |     /*  | 
709  |  |      * RFC 9000 s. 19.8: "The largest offset delivered on a stream -- the sum of  | 
710  |  |      * the offset and data length -- cannot exceed 2**62 - 1, as it is not  | 
711  |  |      * possible to provide flow control credit for that data."  | 
712  |  |      */  | 
713  | 0  |     if (f->offset + f->len > (((uint64_t)1) << 62) - 1)  | 
714  | 0  |         return 0;  | 
715  |  |  | 
716  | 0  |     if (nodata) { | 
717  | 0  |         f->data = NULL;  | 
718  | 0  |     } else { | 
719  | 0  |         f->data = PACKET_data(pkt);  | 
720  |  | 
  | 
721  | 0  |         if (f->len > SIZE_MAX /* sizeof(uint64_t) > sizeof(size_t)? */  | 
722  | 0  |             || !PACKET_forward(pkt, (size_t)f->len))  | 
723  | 0  |             return 0;  | 
724  | 0  |     }  | 
725  |  |  | 
726  | 0  |     return 1;  | 
727  | 0  | }  | 
728  |  |  | 
729  |  | int ossl_quic_wire_decode_frame_max_data(PACKET *pkt,  | 
730  |  |                                          uint64_t *max_data)  | 
731  | 0  | { | 
732  | 0  |     if (!expect_frame_header(pkt, OSSL_QUIC_FRAME_TYPE_MAX_DATA)  | 
733  | 0  |             || !PACKET_get_quic_vlint(pkt, max_data))  | 
734  | 0  |         return 0;  | 
735  |  |  | 
736  | 0  |     return 1;  | 
737  | 0  | }  | 
738  |  |  | 
739  |  | int ossl_quic_wire_decode_frame_max_stream_data(PACKET *pkt,  | 
740  |  |                                                 uint64_t *stream_id,  | 
741  |  |                                                 uint64_t *max_stream_data)  | 
742  | 0  | { | 
743  | 0  |     if (!expect_frame_header(pkt, OSSL_QUIC_FRAME_TYPE_MAX_STREAM_DATA)  | 
744  | 0  |             || !PACKET_get_quic_vlint(pkt, stream_id)  | 
745  | 0  |             || !PACKET_get_quic_vlint(pkt, max_stream_data))  | 
746  | 0  |         return 0;  | 
747  |  |  | 
748  | 0  |     return 1;  | 
749  | 0  | }  | 
750  |  |  | 
751  |  | int ossl_quic_wire_decode_frame_max_streams(PACKET *pkt,  | 
752  |  |                                             uint64_t *max_streams)  | 
753  | 0  | { | 
754  |  |     /* This call matches both MAX_STREAMS_BIDI and MAX_STREAMS_UNI. */  | 
755  | 0  |     if (!expect_frame_header_mask(pkt, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI,  | 
756  | 0  |                                   1, NULL)  | 
757  | 0  |             || !PACKET_get_quic_vlint(pkt, max_streams))  | 
758  | 0  |         return 0;  | 
759  |  |  | 
760  | 0  |     return 1;  | 
761  | 0  | }  | 
762  |  |  | 
763  |  | int ossl_quic_wire_decode_frame_data_blocked(PACKET *pkt,  | 
764  |  |                                              uint64_t *max_data)  | 
765  | 0  | { | 
766  | 0  |     if (!expect_frame_header(pkt, OSSL_QUIC_FRAME_TYPE_DATA_BLOCKED)  | 
767  | 0  |             || !PACKET_get_quic_vlint(pkt, max_data))  | 
768  | 0  |         return 0;  | 
769  |  |  | 
770  | 0  |     return 1;  | 
771  | 0  | }  | 
772  |  |  | 
773  |  | int ossl_quic_wire_decode_frame_stream_data_blocked(PACKET *pkt,  | 
774  |  |                                                     uint64_t *stream_id,  | 
775  |  |                                                     uint64_t *max_stream_data)  | 
776  | 0  | { | 
777  | 0  |     if (!expect_frame_header(pkt, OSSL_QUIC_FRAME_TYPE_STREAM_DATA_BLOCKED)  | 
778  | 0  |             || !PACKET_get_quic_vlint(pkt, stream_id)  | 
779  | 0  |             || !PACKET_get_quic_vlint(pkt, max_stream_data))  | 
780  | 0  |         return 0;  | 
781  |  |  | 
782  | 0  |     return 1;  | 
783  | 0  | }  | 
784  |  |  | 
785  |  | int ossl_quic_wire_decode_frame_streams_blocked(PACKET *pkt,  | 
786  |  |                                                 uint64_t *max_streams)  | 
787  | 0  | { | 
788  |  |     /* This call matches both STREAMS_BLOCKED_BIDI and STREAMS_BLOCKED_UNI. */  | 
789  | 0  |     if (!expect_frame_header_mask(pkt, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_BIDI,  | 
790  | 0  |                                   1, NULL)  | 
791  | 0  |             || !PACKET_get_quic_vlint(pkt, max_streams))  | 
792  | 0  |         return 0;  | 
793  |  |  | 
794  | 0  |     return 1;  | 
795  | 0  | }  | 
796  |  |  | 
797  |  | int ossl_quic_wire_decode_frame_new_conn_id(PACKET *pkt,  | 
798  |  |                                             OSSL_QUIC_FRAME_NEW_CONN_ID *f)  | 
799  | 0  | { | 
800  | 0  |     unsigned int len;  | 
801  |  | 
  | 
802  | 0  |     if (!expect_frame_header(pkt, OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID)  | 
803  | 0  |             || !PACKET_get_quic_vlint(pkt, &f->seq_num)  | 
804  | 0  |             || !PACKET_get_quic_vlint(pkt, &f->retire_prior_to)  | 
805  | 0  |             || f->seq_num < f->retire_prior_to  | 
806  | 0  |             || !PACKET_get_1(pkt, &len)  | 
807  | 0  |             || len < 1  | 
808  | 0  |             || len > QUIC_MAX_CONN_ID_LEN)  | 
809  | 0  |         return 0;  | 
810  |  |  | 
811  | 0  |     f->conn_id.id_len = (unsigned char)len;  | 
812  | 0  |     if (!PACKET_copy_bytes(pkt, f->conn_id.id, len))  | 
813  | 0  |         return 0;  | 
814  |  |  | 
815  |  |     /* Clear unused bytes to allow consistent memcmp. */  | 
816  | 0  |     if (len < QUIC_MAX_CONN_ID_LEN)  | 
817  | 0  |         memset(f->conn_id.id + len, 0, QUIC_MAX_CONN_ID_LEN - len);  | 
818  |  | 
  | 
819  | 0  |     if (!PACKET_copy_bytes(pkt, f->stateless_reset.token,  | 
820  | 0  |                            sizeof(f->stateless_reset.token)))  | 
821  | 0  |         return 0;  | 
822  |  |  | 
823  | 0  |     return 1;  | 
824  | 0  | }  | 
825  |  |  | 
826  |  | int ossl_quic_wire_decode_frame_retire_conn_id(PACKET *pkt,  | 
827  |  |                                                uint64_t *seq_num)  | 
828  | 0  | { | 
829  | 0  |     if (!expect_frame_header(pkt, OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID)  | 
830  | 0  |             || !PACKET_get_quic_vlint(pkt, seq_num))  | 
831  | 0  |         return 0;  | 
832  |  |  | 
833  | 0  |     return 1;  | 
834  | 0  | }  | 
835  |  |  | 
836  |  | int ossl_quic_wire_decode_frame_path_challenge(PACKET *pkt,  | 
837  |  |                                                uint64_t *data)  | 
838  | 0  | { | 
839  | 0  |     if (!expect_frame_header(pkt, OSSL_QUIC_FRAME_TYPE_PATH_CHALLENGE)  | 
840  | 0  |             || !PACKET_get_net_8(pkt, data))  | 
841  | 0  |         return 0;  | 
842  |  |  | 
843  | 0  |     return 1;  | 
844  | 0  | }  | 
845  |  |  | 
846  |  | int ossl_quic_wire_decode_frame_path_response(PACKET *pkt,  | 
847  |  |                                               uint64_t *data)  | 
848  | 0  | { | 
849  | 0  |     if (!expect_frame_header(pkt, OSSL_QUIC_FRAME_TYPE_PATH_RESPONSE)  | 
850  | 0  |             || !PACKET_get_net_8(pkt, data))  | 
851  | 0  |         return 0;  | 
852  |  |  | 
853  | 0  |     return 1;  | 
854  | 0  | }  | 
855  |  |  | 
856  |  | int ossl_quic_wire_decode_frame_conn_close(PACKET *pkt,  | 
857  |  |                                            OSSL_QUIC_FRAME_CONN_CLOSE *f)  | 
858  | 0  | { | 
859  | 0  |     uint64_t frame_type, reason_len;  | 
860  |  |  | 
861  |  |     /* This call matches both CONN_CLOSE_TRANSPORT and CONN_CLOSE_APP. */  | 
862  | 0  |     if (!expect_frame_header_mask(pkt, OSSL_QUIC_FRAME_TYPE_CONN_CLOSE_TRANSPORT,  | 
863  | 0  |                                   1, &frame_type)  | 
864  | 0  |             || !PACKET_get_quic_vlint(pkt, &f->error_code))  | 
865  | 0  |         return 0;  | 
866  |  |  | 
867  | 0  |     f->is_app = ((frame_type & 1) != 0);  | 
868  |  | 
  | 
869  | 0  |     if (!f->is_app) { | 
870  | 0  |         if (!PACKET_get_quic_vlint(pkt, &f->frame_type))  | 
871  | 0  |             return 0;  | 
872  | 0  |     } else { | 
873  | 0  |         f->frame_type = 0;  | 
874  | 0  |     }  | 
875  |  |  | 
876  | 0  |     if (!PACKET_get_quic_vlint(pkt, &reason_len)  | 
877  | 0  |             || reason_len > SIZE_MAX)  | 
878  | 0  |         return 0;  | 
879  |  |  | 
880  | 0  |     if (!PACKET_get_bytes(pkt, (const unsigned char **)&f->reason,  | 
881  | 0  |                           (size_t)reason_len))  | 
882  | 0  |         return 0;  | 
883  |  |  | 
884  | 0  |     f->reason_len = (size_t)reason_len;  | 
885  | 0  |     return 1;  | 
886  | 0  | }  | 
887  |  |  | 
888  |  | size_t ossl_quic_wire_decode_padding(PACKET *pkt)  | 
889  | 0  | { | 
890  | 0  |     const unsigned char *start = PACKET_data(pkt), *end = PACKET_end(pkt),  | 
891  | 0  |                         *p = start;  | 
892  |  | 
  | 
893  | 0  |     while (p < end && *p == 0)  | 
894  | 0  |         ++p;  | 
895  |  | 
  | 
896  | 0  |     if (!PACKET_forward(pkt, p - start))  | 
897  | 0  |         return 0;  | 
898  |  |  | 
899  | 0  |     return p - start;  | 
900  | 0  | }  | 
901  |  |  | 
902  |  | int ossl_quic_wire_decode_frame_ping(PACKET *pkt)  | 
903  | 0  | { | 
904  | 0  |     return expect_frame_header(pkt, OSSL_QUIC_FRAME_TYPE_PING);  | 
905  | 0  | }  | 
906  |  |  | 
907  |  | int ossl_quic_wire_decode_frame_handshake_done(PACKET *pkt)  | 
908  | 0  | { | 
909  | 0  |     return expect_frame_header(pkt, OSSL_QUIC_FRAME_TYPE_HANDSHAKE_DONE);  | 
910  | 0  | }  | 
911  |  |  | 
912  |  | int ossl_quic_wire_peek_transport_param(PACKET *pkt, uint64_t *id)  | 
913  | 0  | { | 
914  | 0  |     return PACKET_peek_quic_vlint(pkt, id);  | 
915  | 0  | }  | 
916  |  |  | 
917  |  | const unsigned char *ossl_quic_wire_decode_transport_param_bytes(PACKET *pkt,  | 
918  |  |                                                                  uint64_t *id,  | 
919  |  |                                                                  size_t *len)  | 
920  | 0  | { | 
921  | 0  |     uint64_t len_;  | 
922  | 0  |     const unsigned char *b = NULL;  | 
923  | 0  |     uint64_t id_;  | 
924  |  | 
  | 
925  | 0  |     if (!PACKET_get_quic_vlint(pkt, &id_)  | 
926  | 0  |             || !PACKET_get_quic_vlint(pkt, &len_))  | 
927  | 0  |         return NULL;  | 
928  |  |  | 
929  | 0  |     if (len_ > SIZE_MAX  | 
930  | 0  |             || !PACKET_get_bytes(pkt, (const unsigned char **)&b, (size_t)len_))  | 
931  | 0  |         return NULL;  | 
932  |  |  | 
933  | 0  |     *len = (size_t)len_;  | 
934  | 0  |     if (id != NULL)  | 
935  | 0  |         *id = id_;  | 
936  | 0  |     return b;  | 
937  | 0  | }  | 
938  |  |  | 
939  |  | int ossl_quic_wire_decode_transport_param_int(PACKET *pkt,  | 
940  |  |                                               uint64_t *id,  | 
941  |  |                                               uint64_t *value)  | 
942  | 0  | { | 
943  | 0  |     PACKET sub;  | 
944  |  | 
  | 
945  | 0  |     sub.curr = ossl_quic_wire_decode_transport_param_bytes(pkt,  | 
946  | 0  |                                                            id, &sub.remaining);  | 
947  | 0  |     if (sub.curr == NULL)  | 
948  | 0  |         return 0;  | 
949  |  |  | 
950  | 0  |     if (!PACKET_get_quic_vlint(&sub, value))  | 
951  | 0  |         return 0;  | 
952  |  |  | 
953  | 0  |     if (PACKET_remaining(&sub) > 0)  | 
954  | 0  |         return 0;  | 
955  |  |  | 
956  | 0  |    return 1;  | 
957  | 0  | }  | 
958  |  |  | 
959  |  | int ossl_quic_wire_decode_transport_param_cid(PACKET *pkt,  | 
960  |  |                                               uint64_t *id,  | 
961  |  |                                               QUIC_CONN_ID *cid)  | 
962  | 0  | { | 
963  | 0  |     const unsigned char *body;  | 
964  | 0  |     size_t len = 0;  | 
965  |  | 
  | 
966  | 0  |     body = ossl_quic_wire_decode_transport_param_bytes(pkt, id, &len);  | 
967  | 0  |     if (body == NULL || len > QUIC_MAX_CONN_ID_LEN)  | 
968  | 0  |         return 0;  | 
969  |  |  | 
970  | 0  |     cid->id_len = (unsigned char)len;  | 
971  | 0  |     memcpy(cid->id, body, cid->id_len);  | 
972  | 0  |     return 1;  | 
973  | 0  | }  | 
974  |  |  | 
975  |  | int ossl_quic_wire_decode_transport_param_preferred_addr(PACKET *pkt,  | 
976  |  |                                                          QUIC_PREFERRED_ADDR *p)  | 
977  | 0  | { | 
978  | 0  |     const unsigned char *body;  | 
979  | 0  |     uint64_t id;  | 
980  | 0  |     size_t len = 0;  | 
981  | 0  |     PACKET pkt2;  | 
982  | 0  |     unsigned int ipv4_port, ipv6_port, cidl;  | 
983  |  | 
  | 
984  | 0  |     body = ossl_quic_wire_decode_transport_param_bytes(pkt, &id, &len);  | 
985  | 0  |     if (body == NULL  | 
986  | 0  |         || len < QUIC_MIN_ENCODED_PREFERRED_ADDR_LEN  | 
987  | 0  |         || len > QUIC_MAX_ENCODED_PREFERRED_ADDR_LEN  | 
988  | 0  |         || id != QUIC_TPARAM_PREFERRED_ADDR)  | 
989  | 0  |         return 0;  | 
990  |  |  | 
991  | 0  |     if (!PACKET_buf_init(&pkt2, body, len))  | 
992  | 0  |         return 0;  | 
993  |  |  | 
994  | 0  |     if (!PACKET_copy_bytes(&pkt2, p->ipv4, sizeof(p->ipv4))  | 
995  | 0  |         || !PACKET_get_net_2(&pkt2, &ipv4_port)  | 
996  | 0  |         || !PACKET_copy_bytes(&pkt2, p->ipv6, sizeof(p->ipv6))  | 
997  | 0  |         || !PACKET_get_net_2(&pkt2, &ipv6_port)  | 
998  | 0  |         || !PACKET_get_1(&pkt2, &cidl)  | 
999  | 0  |         || cidl > QUIC_MAX_CONN_ID_LEN  | 
1000  | 0  |         || !PACKET_copy_bytes(&pkt2, p->cid.id, cidl)  | 
1001  | 0  |         || !PACKET_copy_bytes(&pkt2, p->stateless_reset.token,  | 
1002  | 0  |                               sizeof(p->stateless_reset.token)))  | 
1003  | 0  |         return 0;  | 
1004  |  |  | 
1005  | 0  |     p->ipv4_port    = (uint16_t)ipv4_port;  | 
1006  | 0  |     p->ipv6_port    = (uint16_t)ipv6_port;  | 
1007  | 0  |     p->cid.id_len   = (unsigned char)cidl;  | 
1008  | 0  |     return 1;  | 
1009  | 0  | }  | 
1010  |  |  | 
1011  |  | const char *  | 
1012  |  | ossl_quic_frame_type_to_string(uint64_t frame_type)  | 
1013  | 0  | { | 
1014  | 0  |     switch (frame_type) { | 
1015  | 0  | #define X(name) case OSSL_QUIC_FRAME_TYPE_##name: return #name;  | 
1016  | 0  |     X(PADDING)  | 
1017  | 0  |     X(PING)  | 
1018  | 0  |     X(ACK_WITHOUT_ECN)  | 
1019  | 0  |     X(ACK_WITH_ECN)  | 
1020  | 0  |     X(RESET_STREAM)  | 
1021  | 0  |     X(STOP_SENDING)  | 
1022  | 0  |     X(CRYPTO)  | 
1023  | 0  |     X(NEW_TOKEN)  | 
1024  | 0  |     X(MAX_DATA)  | 
1025  | 0  |     X(MAX_STREAM_DATA)  | 
1026  | 0  |     X(MAX_STREAMS_BIDI)  | 
1027  | 0  |     X(MAX_STREAMS_UNI)  | 
1028  | 0  |     X(DATA_BLOCKED)  | 
1029  | 0  |     X(STREAM_DATA_BLOCKED)  | 
1030  | 0  |     X(STREAMS_BLOCKED_BIDI)  | 
1031  | 0  |     X(STREAMS_BLOCKED_UNI)  | 
1032  | 0  |     X(NEW_CONN_ID)  | 
1033  | 0  |     X(RETIRE_CONN_ID)  | 
1034  | 0  |     X(PATH_CHALLENGE)  | 
1035  | 0  |     X(PATH_RESPONSE)  | 
1036  | 0  |     X(CONN_CLOSE_TRANSPORT)  | 
1037  | 0  |     X(CONN_CLOSE_APP)  | 
1038  | 0  |     X(HANDSHAKE_DONE)  | 
1039  | 0  |     X(STREAM)  | 
1040  | 0  |     X(STREAM_FIN)  | 
1041  | 0  |     X(STREAM_LEN)  | 
1042  | 0  |     X(STREAM_LEN_FIN)  | 
1043  | 0  |     X(STREAM_OFF)  | 
1044  | 0  |     X(STREAM_OFF_FIN)  | 
1045  | 0  |     X(STREAM_OFF_LEN)  | 
1046  | 0  |     X(STREAM_OFF_LEN_FIN)  | 
1047  | 0  | #undef X  | 
1048  | 0  |     default:  | 
1049  | 0  |         return NULL;  | 
1050  | 0  |     }  | 
1051  | 0  | }  | 
1052  |  |  | 
1053  |  | const char *ossl_quic_err_to_string(uint64_t error_code)  | 
1054  | 0  | { | 
1055  | 0  |     switch (error_code) { | 
1056  | 0  | #define X(name) case OSSL_QUIC_ERR_##name: return #name;  | 
1057  | 0  |     X(NO_ERROR)  | 
1058  | 0  |     X(INTERNAL_ERROR)  | 
1059  | 0  |     X(CONNECTION_REFUSED)  | 
1060  | 0  |     X(FLOW_CONTROL_ERROR)  | 
1061  | 0  |     X(STREAM_LIMIT_ERROR)  | 
1062  | 0  |     X(STREAM_STATE_ERROR)  | 
1063  | 0  |     X(FINAL_SIZE_ERROR)  | 
1064  | 0  |     X(FRAME_ENCODING_ERROR)  | 
1065  | 0  |     X(TRANSPORT_PARAMETER_ERROR)  | 
1066  | 0  |     X(CONNECTION_ID_LIMIT_ERROR)  | 
1067  | 0  |     X(PROTOCOL_VIOLATION)  | 
1068  | 0  |     X(INVALID_TOKEN)  | 
1069  | 0  |     X(APPLICATION_ERROR)  | 
1070  | 0  |     X(CRYPTO_BUFFER_EXCEEDED)  | 
1071  | 0  |     X(KEY_UPDATE_ERROR)  | 
1072  | 0  |     X(AEAD_LIMIT_REACHED)  | 
1073  | 0  |     X(NO_VIABLE_PATH)  | 
1074  | 0  | #undef X  | 
1075  | 0  |     default:  | 
1076  | 0  |         return NULL;  | 
1077  | 0  |     }  | 
1078  | 0  | }  |