blob: f96b17b2d3c3130e8317f56fb316b4384f449bcf [file] [log] [blame]
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +01001/*
2 * QPACK decompressor
3 *
Willy Tarreau3dfb7da2022-03-02 22:33:39 +01004 * Copyright 2021 HAProxy Technologies, Frederic Lecaille <flecaille@haproxy.com>
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +01005 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation, version 2.1
9 * exclusively.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include <inttypes.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25
26#include <import/ist.h>
27#include <haproxy/buf.h>
28#include <haproxy/chunk.h>
29#include <haproxy/h3.h>
Amaury Denoyelle26aa3992022-08-16 17:42:47 +020030#include <haproxy/mux_quic.h>
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +010031#include <haproxy/qpack-t.h>
32#include <haproxy/qpack-dec.h>
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +020033#include <haproxy/qpack-tbl.h>
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +010034#include <haproxy/hpack-huff.h>
35#include <haproxy/hpack-tbl.h>
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +020036#include <haproxy/http-hdr.h>
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +010037#include <haproxy/tools.h>
38
Amaury Denoyelled96361b2022-03-25 14:56:51 +010039#if defined(DEBUG_QPACK)
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +010040#define qpack_debug_printf fprintf
41#define qpack_debug_hexdump debug_hexdump
42#else
43#define qpack_debug_printf(...) do { } while (0)
44#define qpack_debug_hexdump(...) do { } while (0)
45#endif
46
47/* Encoded field line bitmask */
48#define QPACK_EFL_BITMASK 0xf0
49#define QPACK_LFL_WPBNM 0x00 // Literal field line with post-base name reference
50#define QPACK_IFL_WPBI 0x10 // Indexed field line with post-based index
51#define QPACK_LFL_WLN_BIT 0x20 // Literal field line with literal name
52#define QPACK_LFL_WNR_BIT 0x40 // Literal field line with name reference
53#define QPACK_IFL_BIT 0x80 // Indexed field line
54
55/* reads a varint from <raw>'s lowest <b> bits and <len> bytes max (raw included).
56 * returns the 64-bit value on success after updating buf and len_in. Forces
57 * len_in to (uint64_t)-1 on truncated input.
58 * Note that this function is similar to the one used for HPACK (except that is supports
59 * up to 62-bits integers).
60 */
61static uint64_t qpack_get_varint(const unsigned char **buf, uint64_t *len_in, int b)
62{
63 uint64_t ret = 0;
64 int len = *len_in;
65 const uint8_t *raw = *buf;
66 uint8_t shift = 0;
67
68 len--;
Frédéric Lécaille68424852022-02-02 14:56:23 +010069 ret = *raw++ & ((1ULL << b) - 1);
70 if (ret != (uint64_t)((1ULL << b) - 1))
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +010071 goto end;
72
73 while (len && (*raw & 128)) {
74 ret += ((uint64_t)*raw++ & 127) << shift;
75 shift += 7;
76 len--;
77 }
78
79 /* last 7 bits */
80 if (!len)
81 goto too_short;
82
83 len--;
84 ret += ((uint64_t)*raw++ & 127) << shift;
85
86 end:
87 *buf = raw;
88 *len_in = len;
89 return ret;
90
91 too_short:
92 *len_in = (uint64_t)-1;
93 return 0;
94}
95
Amaury Denoyelle5869cb62022-05-31 15:21:27 +020096/* Decode an encoder stream.
97 *
98 * Returns 0 on success else non-zero.
99 */
Amaury Denoyelle26aa3992022-08-16 17:42:47 +0200100int qpack_decode_enc(struct buffer *buf, int fin, void *ctx)
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100101{
Amaury Denoyelle26aa3992022-08-16 17:42:47 +0200102 struct qcs *qcs = ctx;
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100103 size_t len;
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100104 unsigned char inst;
105
Amaury Denoyelle26aa3992022-08-16 17:42:47 +0200106 /* RFC 9204 4.2. Encoder and Decoder Streams
107 *
108 * The sender MUST NOT close either of these streams, and the receiver
109 * MUST NOT request that the sender close either of these streams.
110 * Closure of either unidirectional stream type MUST be treated as a
111 * connection error of type H3_CLOSED_CRITICAL_STREAM.
112 */
113 if (fin) {
Amaury Denoyelle58721f22023-05-09 18:01:09 +0200114 qcc_set_error(qcs->qcc, H3_CLOSED_CRITICAL_STREAM, 1);
Amaury Denoyelle26aa3992022-08-16 17:42:47 +0200115 return -1;
116 }
117
Amaury Denoyelle53eef462022-06-14 16:34:32 +0200118 len = b_data(buf);
119 qpack_debug_hexdump(stderr, "[QPACK-DEC-ENC] ", b_head(buf), 0, len);
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100120
121 if (!len) {
122 qpack_debug_printf(stderr, "[QPACK-DEC-ENC] empty stream\n");
123 return 0;
124 }
125
Amaury Denoyelle53eef462022-06-14 16:34:32 +0200126 inst = (unsigned char)*b_head(buf) & QPACK_ENC_INST_BITMASK;
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100127 if (inst == QPACK_ENC_INST_DUP) {
128 /* Duplicate */
129 }
130 else if (inst & QPACK_ENC_INST_IWNR_BIT) {
131 /* Insert With Name Reference */
132 }
133 else if (inst & QPACK_ENC_INST_IWLN_BIT) {
134 /* Insert with literal name */
135 }
136 else if (inst & QPACK_ENC_INST_SDTC_BIT) {
137 /* Set dynamic table capacity */
Amaury Denoyellec95106f2024-02-14 18:23:12 +0100138 int capacity = *b_head(buf) & 0x1f;
139
140 /* RFC 9204 4.3.1. Set Dynamic Table Capacity
141 *
142 * The decoder MUST treat a new dynamic table capacity
143 * value that exceeds this limit as a connection error of type
144 * QPACK_ENCODER_STREAM_ERROR.
145 */
146 if (capacity) {
147 qcc_set_error(qcs->qcc, QPACK_ENCODER_STREAM_ERROR, 1);
148 return -1;
149 }
150
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100151 }
152
Amaury Denoyelle5869cb62022-05-31 15:21:27 +0200153 return 0;
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100154}
155
Amaury Denoyelle5869cb62022-05-31 15:21:27 +0200156/* Decode an decoder stream.
157 *
158 * Returns 0 on success else non-zero.
159 */
Amaury Denoyelle26aa3992022-08-16 17:42:47 +0200160int qpack_decode_dec(struct buffer *buf, int fin, void *ctx)
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100161{
Amaury Denoyelle26aa3992022-08-16 17:42:47 +0200162 struct qcs *qcs = ctx;
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100163 size_t len;
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100164 unsigned char inst;
165
Amaury Denoyelle26aa3992022-08-16 17:42:47 +0200166 /* RFC 9204 4.2. Encoder and Decoder Streams
167 *
168 * The sender MUST NOT close either of these streams, and the receiver
169 * MUST NOT request that the sender close either of these streams.
170 * Closure of either unidirectional stream type MUST be treated as a
171 * connection error of type H3_CLOSED_CRITICAL_STREAM.
172 */
173 if (fin) {
Amaury Denoyelle58721f22023-05-09 18:01:09 +0200174 qcc_set_error(qcs->qcc, H3_CLOSED_CRITICAL_STREAM, 1);
Amaury Denoyelle26aa3992022-08-16 17:42:47 +0200175 return -1;
176 }
177
Amaury Denoyelle53eef462022-06-14 16:34:32 +0200178 len = b_data(buf);
179 qpack_debug_hexdump(stderr, "[QPACK-DEC-DEC] ", b_head(buf), 0, len);
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100180
181 if (!len) {
182 qpack_debug_printf(stderr, "[QPACK-DEC-DEC] empty stream\n");
183 return 0;
184 }
185
Amaury Denoyelle53eef462022-06-14 16:34:32 +0200186 inst = (unsigned char)*b_head(buf) & QPACK_DEC_INST_BITMASK;
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100187 if (inst == QPACK_DEC_INST_ICINC) {
188 /* Insert count increment */
Amaury Denoyelleb840e602024-02-14 16:59:24 +0100189
190 /* RFC 9204 4.4.3. Insert Count Increment
191 *
192 * An encoder that receives an Increment field equal to zero, or one
193 * that increases the Known Received Count beyond what the encoder has
194 * sent, MUST treat this as a connection error of type
195 * QPACK_DECODER_STREAM_ERROR.
196 */
197
198 /* For the moment haproxy does not emit dynamic table insertion. */
199 qcc_set_error(qcs->qcc, QPACK_DECODER_STREAM_ERROR, 1);
200 return -1;
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100201 }
202 else if (inst & QPACK_DEC_INST_SACK) {
203 /* Section Acknowledgment */
204 }
205 else if (inst & QPACK_DEC_INST_SCCL) {
206 /* Stream cancellation */
207 }
208
Amaury Denoyelle5869cb62022-05-31 15:21:27 +0200209 return 0;
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100210}
211
212/* Decode a field section prefix made of <enc_ric> and <db> two varints.
213 * Also set the 'S' sign bit for <db>.
214 * Return a negative error if failed, 0 if not.
215 */
216static int qpack_decode_fs_pfx(uint64_t *enc_ric, uint64_t *db, int *sign_bit,
Frédéric Lécaille628e89c2022-06-24 12:13:53 +0200217 const unsigned char **raw, uint64_t *len)
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100218{
219 *enc_ric = qpack_get_varint(raw, len, 8);
220 if (*len == (uint64_t)-1)
221 return -QPACK_ERR_RIC;
222
223 *sign_bit = **raw & 0x8;
224 *db = qpack_get_varint(raw, len, 7);
225 if (*len == (uint64_t)-1)
226 return -QPACK_ERR_DB;
227
228 return 0;
229}
230
Amaury Denoyellec5d31ed2022-06-14 17:34:53 +0200231/* Decode a field section from the <raw> buffer of <len> bytes. Each parsed
Amaury Denoyelle60ef19f2022-06-14 17:38:36 +0200232 * header is inserted into <list> of <list_size> entries max and uses <tmp> as
233 * a storage for some elements pointing into it. An end marker is inserted at
234 * the end of the list with empty strings as name/value.
Amaury Denoyellec5d31ed2022-06-14 17:34:53 +0200235 *
Amaury Denoyelleb666c6b2022-06-14 17:17:07 +0200236 * Returns the number of headers inserted into list excluding the end marker.
237 * In case of error, a negative code QPACK_ERR_* is returned.
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100238 */
Frédéric Lécaille628e89c2022-06-24 12:13:53 +0200239int qpack_decode_fs(const unsigned char *raw, uint64_t len, struct buffer *tmp,
Amaury Denoyelle60ef19f2022-06-14 17:38:36 +0200240 struct http_hdr *list, int list_size)
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100241{
Amaury Denoyelleb666c6b2022-06-14 17:17:07 +0200242 struct ist name, value;
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100243 uint64_t enc_ric, db;
244 int s;
245 unsigned int efl_type;
246 int ret;
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200247 int hdr_idx = 0;
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100248
249 qpack_debug_hexdump(stderr, "[QPACK-DEC-FS] ", (const char *)raw, 0, len);
250
Amaury Denoyellec5d31ed2022-06-14 17:34:53 +0200251 /* parse field section prefix */
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100252 ret = qpack_decode_fs_pfx(&enc_ric, &db, &s, &raw, &len);
253 if (ret < 0) {
254 qpack_debug_printf(stderr, "##ERR@%d(%d)\n", __LINE__, ret);
255 goto out;
256 }
257
258 chunk_reset(tmp);
259 qpack_debug_printf(stderr, "enc_ric: %llu db: %llu s=%d\n",
260 (unsigned long long)enc_ric, (unsigned long long)db, !!s);
261 /* Decode field lines */
262 while (len) {
Amaury Denoyelle60ef19f2022-06-14 17:38:36 +0200263 if (hdr_idx >= list_size) {
264 qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__);
265 ret = -QPACK_ERR_TOO_LARGE;
266 goto out;
267 }
268
Amaury Denoyellec5d31ed2022-06-14 17:34:53 +0200269 /* parse field line representation */
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100270 efl_type = *raw & QPACK_EFL_BITMASK;
271 qpack_debug_printf(stderr, "efl_type=0x%02x\n", efl_type);
Amaury Denoyellec5d31ed2022-06-14 17:34:53 +0200272
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100273 if (efl_type == QPACK_LFL_WPBNM) {
Amaury Denoyelle28d3c242022-06-14 16:36:15 +0200274 /* Literal field line with post-base name reference
Amaury Denoyellea7a4c802022-06-30 09:30:23 +0200275 * TODO adjust this when dynamic table support is implemented.
Amaury Denoyelle28d3c242022-06-14 16:36:15 +0200276 */
277#if 0
Amaury Denoyelle18a10d02022-03-25 15:11:38 +0100278 uint64_t index __maybe_unused, length;
Amaury Denoyelled96361b2022-03-25 14:56:51 +0100279 unsigned int n __maybe_unused, h __maybe_unused;
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100280
281 qpack_debug_printf(stderr, "literal field line with post-base name reference:");
282 n = *raw & 0x08;
283 index = qpack_get_varint(&raw, &len, 3);
284 if (len == (uint64_t)-1) {
285 qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__);
286 ret = -QPACK_ERR_TRUNCATED;
287 goto out;
288 }
289
290 qpack_debug_printf(stderr, " n=%d index=%llu", !!n, (unsigned long long)index);
291 h = *raw & 0x80;
292 length = qpack_get_varint(&raw, &len, 7);
293 if (len == (uint64_t)-1) {
294 qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__);
295 ret = -QPACK_ERR_TRUNCATED;
296 goto out;
297 }
298
299 qpack_debug_printf(stderr, " h=%d length=%llu", !!h, (unsigned long long)length);
Frédéric Lécaillee629cfd2021-12-15 14:16:16 +0100300
301 if (len < length) {
302 qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__);
303 ret = -QPACK_ERR_TRUNCATED;
304 goto out;
305 }
306
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100307 raw += length;
308 len -= length;
Amaury Denoyelle28d3c242022-06-14 16:36:15 +0200309#endif
Amaury Denoyellea7a4c802022-06-30 09:30:23 +0200310
311 /* RFC9204 2.2.3 Invalid References
312 *
313 * If the decoder encounters a reference in a field line representation
314 * to a dynamic table entry that has already been evicted or that has an
315 * absolute index greater than or equal to the declared Required Insert
316 * Count (Section 4.5.1), it MUST treat this as a connection error of
317 * type QPACK_DECOMPRESSION_FAILED.
318 */
Amaury Denoyellec3c4d1b2024-05-13 16:01:08 +0200319 return -QPACK_ERR_DECOMP;
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200320 }
321 else if (efl_type == QPACK_IFL_WPBI) {
Amaury Denoyelle28d3c242022-06-14 16:36:15 +0200322 /* Indexed field line with post-base index
Amaury Denoyellea7a4c802022-06-30 09:30:23 +0200323 * TODO adjust this when dynamic table support is implemented.
Amaury Denoyelle28d3c242022-06-14 16:36:15 +0200324 */
325#if 0
Amaury Denoyelle18a10d02022-03-25 15:11:38 +0100326 uint64_t index __maybe_unused;
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100327
328 qpack_debug_printf(stderr, "indexed field line with post-base index:");
329 index = qpack_get_varint(&raw, &len, 4);
330 if (len == (uint64_t)-1) {
331 qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__);
332 ret = -QPACK_ERR_TRUNCATED;
333 goto out;
334 }
335
336 qpack_debug_printf(stderr, " index=%llu", (unsigned long long)index);
Amaury Denoyelle28d3c242022-06-14 16:36:15 +0200337#endif
Amaury Denoyellea7a4c802022-06-30 09:30:23 +0200338
339 /* RFC9204 2.2.3 Invalid References
340 *
341 * If the decoder encounters a reference in a field line representation
342 * to a dynamic table entry that has already been evicted or that has an
343 * absolute index greater than or equal to the declared Required Insert
344 * Count (Section 4.5.1), it MUST treat this as a connection error of
345 * type QPACK_DECOMPRESSION_FAILED.
346 */
Amaury Denoyellec3c4d1b2024-05-13 16:01:08 +0200347 return -QPACK_ERR_DECOMP;
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200348 }
349 else if (efl_type & QPACK_IFL_BIT) {
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100350 /* Indexed field line */
351 uint64_t index;
Amaury Denoyelledebaa042022-06-20 15:47:46 +0200352 unsigned int static_tbl;
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100353
354 qpack_debug_printf(stderr, "indexed field line:");
Amaury Denoyelledebaa042022-06-20 15:47:46 +0200355 static_tbl = efl_type & 0x40;
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100356 index = qpack_get_varint(&raw, &len, 6);
357 if (len == (uint64_t)-1) {
358 qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__);
359 ret = -QPACK_ERR_TRUNCATED;
360 goto out;
361 }
362
Willy Tarreauf41dfc22023-03-17 16:40:09 +0100363 if (static_tbl && index < QPACK_SHT_SIZE) {
Amaury Denoyelleb666c6b2022-06-14 17:17:07 +0200364 name = qpack_sht[index].n;
365 value = qpack_sht[index].v;
366 }
Amaury Denoyelledebaa042022-06-20 15:47:46 +0200367 else {
Amaury Denoyellea7a4c802022-06-30 09:30:23 +0200368 /* RFC9204 2.2.3 Invalid References
Amaury Denoyelledebaa042022-06-20 15:47:46 +0200369 *
Amaury Denoyellea7a4c802022-06-30 09:30:23 +0200370 * If the decoder encounters a reference in a field line representation
371 * to a dynamic table entry that has already been evicted or that has an
372 * absolute index greater than or equal to the declared Required Insert
373 * Count (Section 4.5.1), it MUST treat this as a connection error of
374 * type QPACK_DECOMPRESSION_FAILED.
375 *
376 * TODO adjust this when dynamic table support is implemented.
Amaury Denoyelledebaa042022-06-20 15:47:46 +0200377 */
Amaury Denoyellec3c4d1b2024-05-13 16:01:08 +0200378 return -QPACK_ERR_DECOMP;
Amaury Denoyelledebaa042022-06-20 15:47:46 +0200379 }
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200380
Amaury Denoyelle055de232022-06-30 09:28:50 +0200381 qpack_debug_printf(stderr, " t=%d index=%llu", !!static_tbl, (unsigned long long)index);
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200382 }
383 else if (efl_type & QPACK_LFL_WNR_BIT) {
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100384 /* Literal field line with name reference */
385 uint64_t index, length;
Amaury Denoyelle46e992d2022-06-30 09:31:24 +0200386 unsigned int static_tbl, n __maybe_unused, h;
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100387
388 qpack_debug_printf(stderr, "Literal field line with name reference:");
389 n = efl_type & 0x20;
Amaury Denoyelle46e992d2022-06-30 09:31:24 +0200390 static_tbl = efl_type & 0x10;
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100391 index = qpack_get_varint(&raw, &len, 4);
392 if (len == (uint64_t)-1) {
393 qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__);
394 ret = -QPACK_ERR_TRUNCATED;
395 goto out;
396 }
397
Willy Tarreauf41dfc22023-03-17 16:40:09 +0100398 if (static_tbl && index < QPACK_SHT_SIZE) {
Amaury Denoyelleb666c6b2022-06-14 17:17:07 +0200399 name = qpack_sht[index].n;
Amaury Denoyelle46e992d2022-06-30 09:31:24 +0200400 }
401 else {
Amaury Denoyellea7a4c802022-06-30 09:30:23 +0200402 /* RFC9204 2.2.3 Invalid References
Amaury Denoyelle46e992d2022-06-30 09:31:24 +0200403 *
Amaury Denoyellea7a4c802022-06-30 09:30:23 +0200404 * If the decoder encounters a reference in a field line representation
405 * to a dynamic table entry that has already been evicted or that has an
406 * absolute index greater than or equal to the declared Required Insert
407 * Count (Section 4.5.1), it MUST treat this as a connection error of
408 * type QPACK_DECOMPRESSION_FAILED.
409 *
410 * TODO adjust this when dynamic table support is implemented.
Amaury Denoyelle46e992d2022-06-30 09:31:24 +0200411 */
Amaury Denoyellec3c4d1b2024-05-13 16:01:08 +0200412 return -QPACK_ERR_DECOMP;
Amaury Denoyelle46e992d2022-06-30 09:31:24 +0200413 }
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200414
Amaury Denoyelle46e992d2022-06-30 09:31:24 +0200415 qpack_debug_printf(stderr, " n=%d t=%d index=%llu", !!n, !!static_tbl, (unsigned long long)index);
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100416 h = *raw & 0x80;
417 length = qpack_get_varint(&raw, &len, 7);
418 if (len == (uint64_t)-1) {
419 qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__);
420 ret = -QPACK_ERR_TRUNCATED;
421 goto out;
422 }
423
424 qpack_debug_printf(stderr, " h=%d length=%llu", !!h, (unsigned long long)length);
425 if (h) {
426 char *trash;
427 int nlen;
428
429 trash = chunk_newstr(tmp);
430 if (!trash) {
431 qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__);
Amaury Denoyellec3c4d1b2024-05-13 16:01:08 +0200432 ret = -QPACK_ERR_TOO_LARGE;
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100433 goto out;
434 }
435 nlen = huff_dec(raw, length, trash, tmp->size - tmp->data);
436 if (nlen == (uint32_t)-1) {
437 qpack_debug_printf(stderr, " can't decode huffman.\n");
438 ret = -QPACK_ERR_HUFFMAN;
439 goto out;
440 }
441
442 qpack_debug_printf(stderr, " [name huff %d->%d '%s']", (int)length, (int)nlen, trash);
Amaury Denoyelle9c8c4fa2021-09-30 17:14:55 +0200443 /* makes an ist from tmp storage */
444 b_add(tmp, nlen);
Amaury Denoyelleb666c6b2022-06-14 17:17:07 +0200445 value = ist2(trash, nlen);
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100446 }
Amaury Denoyelle7d3aea52021-11-24 16:04:03 +0100447 else {
Amaury Denoyelleb666c6b2022-06-14 17:17:07 +0200448 value = ist2(raw, length);
Amaury Denoyelle7d3aea52021-11-24 16:04:03 +0100449 }
450
Frédéric Lécaillee629cfd2021-12-15 14:16:16 +0100451 if (len < length) {
452 qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__);
453 ret = -QPACK_ERR_TRUNCATED;
454 goto out;
455 }
456
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100457 raw += length;
458 len -= length;
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200459 }
460 else if (efl_type & QPACK_LFL_WLN_BIT) {
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100461 /* Literal field line with literal name */
Amaury Denoyelle4bcaf692022-06-14 16:34:55 +0200462 unsigned int n __maybe_unused, hname, hvalue;
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100463 uint64_t name_len, value_len;
464
465 qpack_debug_printf(stderr, "Literal field line with literal name:");
466 n = *raw & 0x10;
467 hname = *raw & 0x08;
468 name_len = qpack_get_varint(&raw, &len, 3);
469 if (len == (uint64_t)-1) {
470 qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__);
471 ret = -QPACK_ERR_TRUNCATED;
472 goto out;
473 }
474
Amaury Denoyelleab9cec72022-02-14 14:45:10 +0100475 qpack_debug_printf(stderr, " n=%d hname=%d name_len=%llu", !!n, !!hname, (unsigned long long)name_len);
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100476 /* Name string */
Frédéric Lécaillee629cfd2021-12-15 14:16:16 +0100477
478 if (len < name_len) {
479 qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__);
480 ret = -QPACK_ERR_TRUNCATED;
481 goto out;
482 }
483
Amaury Denoyelle4bcaf692022-06-14 16:34:55 +0200484 if (hname) {
485 char *trash;
486 int nlen;
487
488 trash = chunk_newstr(tmp);
489 if (!trash) {
490 qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__);
Amaury Denoyellec3c4d1b2024-05-13 16:01:08 +0200491 ret = -QPACK_ERR_TOO_LARGE;
Amaury Denoyelle4bcaf692022-06-14 16:34:55 +0200492 goto out;
493 }
494 nlen = huff_dec(raw, name_len, trash, tmp->size - tmp->data);
495 if (nlen == (uint32_t)-1) {
496 qpack_debug_printf(stderr, " can't decode huffman.\n");
497 ret = -QPACK_ERR_HUFFMAN;
498 goto out;
499 }
500
501 qpack_debug_printf(stderr, " [name huff %d->%d '%s']", (int)name_len, (int)nlen, trash);
502 /* makes an ist from tmp storage */
503 b_add(tmp, nlen);
Amaury Denoyelleb666c6b2022-06-14 17:17:07 +0200504 name = ist2(trash, nlen);
Amaury Denoyelle4bcaf692022-06-14 16:34:55 +0200505 }
506 else {
Amaury Denoyelleb666c6b2022-06-14 17:17:07 +0200507 name = ist2(raw, name_len);
Amaury Denoyelle4bcaf692022-06-14 16:34:55 +0200508 }
509
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100510 raw += name_len;
511 len -= name_len;
Amaury Denoyelleb666c6b2022-06-14 17:17:07 +0200512
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100513 hvalue = *raw & 0x80;
514 value_len = qpack_get_varint(&raw, &len, 7);
515 if (len == (uint64_t)-1) {
516 qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__);
517 ret = -QPACK_ERR_TRUNCATED;
518 goto out;
519 }
520
521 qpack_debug_printf(stderr, " hvalue=%d value_len=%llu", !!hvalue, (unsigned long long)value_len);
522
Frédéric Lécaillee629cfd2021-12-15 14:16:16 +0100523 if (len < value_len) {
524 qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__);
525 ret = -QPACK_ERR_TRUNCATED;
526 goto out;
527 }
528
Amaury Denoyelle4bcaf692022-06-14 16:34:55 +0200529 if (hvalue) {
530 char *trash;
531 int nlen;
532
533 trash = chunk_newstr(tmp);
534 if (!trash) {
535 qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__);
Amaury Denoyellec3c4d1b2024-05-13 16:01:08 +0200536 ret = -QPACK_ERR_TOO_LARGE;
Amaury Denoyelle4bcaf692022-06-14 16:34:55 +0200537 goto out;
538 }
539 nlen = huff_dec(raw, value_len, trash, tmp->size - tmp->data);
540 if (nlen == (uint32_t)-1) {
541 qpack_debug_printf(stderr, " can't decode huffman.\n");
542 ret = -QPACK_ERR_HUFFMAN;
543 goto out;
544 }
545
546 qpack_debug_printf(stderr, " [name huff %d->%d '%s']", (int)value_len, (int)nlen, trash);
547 /* makes an ist from tmp storage */
548 b_add(tmp, nlen);
Amaury Denoyelleb666c6b2022-06-14 17:17:07 +0200549 value = ist2(trash, nlen);
Amaury Denoyelle4bcaf692022-06-14 16:34:55 +0200550 }
551 else {
Amaury Denoyelleb666c6b2022-06-14 17:17:07 +0200552 value = ist2(raw, value_len);
Amaury Denoyelle4bcaf692022-06-14 16:34:55 +0200553 }
554
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100555 raw += value_len;
556 len -= value_len;
557 }
Amaury Denoyelleb666c6b2022-06-14 17:17:07 +0200558
Willy Tarreaua8598a22023-02-09 21:36:54 +0100559 /* We must not accept empty header names (forbidden by the spec and used
560 * as a list termination).
561 */
562 if (!name.len) {
563 qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__);
Amaury Denoyellec3c4d1b2024-05-13 16:01:08 +0200564 ret = -QPACK_ERR_DECOMP;
Willy Tarreaua8598a22023-02-09 21:36:54 +0100565 goto out;
566 }
567
Amaury Denoyelleb666c6b2022-06-14 17:17:07 +0200568 list[hdr_idx].n = name;
569 list[hdr_idx].v = value;
570 ++hdr_idx;
571
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100572 qpack_debug_printf(stderr, "\n");
573 }
574
Amaury Denoyelle60ef19f2022-06-14 17:38:36 +0200575 if (hdr_idx >= list_size) {
576 qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__);
577 ret = -QPACK_ERR_TOO_LARGE;
578 goto out;
579 }
580
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200581 /* put an end marker */
582 list[hdr_idx].n = list[hdr_idx].v = IST_NULL;
Amaury Denoyelleb666c6b2022-06-14 17:17:07 +0200583 ret = hdr_idx;
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200584
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100585 out:
586 qpack_debug_printf(stderr, "-- done: ret=%d\n", ret);
587 return ret;
588}
Amaury Denoyellec3c4d1b2024-05-13 16:01:08 +0200589
590/* Convert return value from qpack_decode_fs() to a standard error code usable
591 * in CONNECTION_CLOSE or -1 for an internal error.
592 */
593int qpack_err_decode(const int value)
594{
595 return (value == -QPACK_ERR_DECOMP) ? QPACK_DECOMPRESSION_FAILED : -1;
596}