blob: 166e1ae2aa79ca0548d30260367cbab76189ecee [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>
30#include <haproxy/qpack-t.h>
31#include <haproxy/qpack-dec.h>
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +020032#include <haproxy/qpack-tbl.h>
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +010033#include <haproxy/hpack-huff.h>
34#include <haproxy/hpack-tbl.h>
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +020035#include <haproxy/http-hdr.h>
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +010036#include <haproxy/tools.h>
37
Amaury Denoyelled96361b2022-03-25 14:56:51 +010038#if defined(DEBUG_QPACK)
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +010039#define qpack_debug_printf fprintf
40#define qpack_debug_hexdump debug_hexdump
41#else
42#define qpack_debug_printf(...) do { } while (0)
43#define qpack_debug_hexdump(...) do { } while (0)
44#endif
45
46/* Encoded field line bitmask */
47#define QPACK_EFL_BITMASK 0xf0
48#define QPACK_LFL_WPBNM 0x00 // Literal field line with post-base name reference
49#define QPACK_IFL_WPBI 0x10 // Indexed field line with post-based index
50#define QPACK_LFL_WLN_BIT 0x20 // Literal field line with literal name
51#define QPACK_LFL_WNR_BIT 0x40 // Literal field line with name reference
52#define QPACK_IFL_BIT 0x80 // Indexed field line
53
54/* reads a varint from <raw>'s lowest <b> bits and <len> bytes max (raw included).
55 * returns the 64-bit value on success after updating buf and len_in. Forces
56 * len_in to (uint64_t)-1 on truncated input.
57 * Note that this function is similar to the one used for HPACK (except that is supports
58 * up to 62-bits integers).
59 */
60static uint64_t qpack_get_varint(const unsigned char **buf, uint64_t *len_in, int b)
61{
62 uint64_t ret = 0;
63 int len = *len_in;
64 const uint8_t *raw = *buf;
65 uint8_t shift = 0;
66
67 len--;
Frédéric Lécaille68424852022-02-02 14:56:23 +010068 ret = *raw++ & ((1ULL << b) - 1);
69 if (ret != (uint64_t)((1ULL << b) - 1))
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +010070 goto end;
71
72 while (len && (*raw & 128)) {
73 ret += ((uint64_t)*raw++ & 127) << shift;
74 shift += 7;
75 len--;
76 }
77
78 /* last 7 bits */
79 if (!len)
80 goto too_short;
81
82 len--;
83 ret += ((uint64_t)*raw++ & 127) << shift;
84
85 end:
86 *buf = raw;
87 *len_in = len;
88 return ret;
89
90 too_short:
91 *len_in = (uint64_t)-1;
92 return 0;
93}
94
95/* Decode an encoder stream */
96int qpack_decode_enc(struct h3_uqs *h3_uqs, void *ctx)
97{
98 size_t len;
99 struct buffer *rxbuf;
100 unsigned char inst;
101
102 rxbuf = &h3_uqs->qcs->rx.buf;
103 len = b_data(rxbuf);
104 qpack_debug_hexdump(stderr, "[QPACK-DEC-ENC] ", b_head(rxbuf), 0, len);
105
106 if (!len) {
107 qpack_debug_printf(stderr, "[QPACK-DEC-ENC] empty stream\n");
108 return 0;
109 }
110
111 inst = (unsigned char)*b_head(rxbuf) & QPACK_ENC_INST_BITMASK;
112 if (inst == QPACK_ENC_INST_DUP) {
113 /* Duplicate */
114 }
115 else if (inst & QPACK_ENC_INST_IWNR_BIT) {
116 /* Insert With Name Reference */
117 }
118 else if (inst & QPACK_ENC_INST_IWLN_BIT) {
119 /* Insert with literal name */
120 }
121 else if (inst & QPACK_ENC_INST_SDTC_BIT) {
122 /* Set dynamic table capacity */
123 }
124
125 return 1;
126}
127
128/* Decode an decoder stream */
129int qpack_decode_dec(struct h3_uqs *h3_uqs, void *ctx)
130{
131 size_t len;
132 struct buffer *rxbuf;
133 unsigned char inst;
134
135 rxbuf = &h3_uqs->qcs->rx.buf;
136 len = b_data(rxbuf);
137 qpack_debug_hexdump(stderr, "[QPACK-DEC-DEC] ", b_head(rxbuf), 0, len);
138
139 if (!len) {
140 qpack_debug_printf(stderr, "[QPACK-DEC-DEC] empty stream\n");
141 return 0;
142 }
143
144 inst = (unsigned char)*b_head(rxbuf) & QPACK_DEC_INST_BITMASK;
145 if (inst == QPACK_DEC_INST_ICINC) {
146 /* Insert count increment */
147 }
148 else if (inst & QPACK_DEC_INST_SACK) {
149 /* Section Acknowledgment */
150 }
151 else if (inst & QPACK_DEC_INST_SCCL) {
152 /* Stream cancellation */
153 }
154
155 return 1;
156}
157
158/* Decode a field section prefix made of <enc_ric> and <db> two varints.
159 * Also set the 'S' sign bit for <db>.
160 * Return a negative error if failed, 0 if not.
161 */
162static int qpack_decode_fs_pfx(uint64_t *enc_ric, uint64_t *db, int *sign_bit,
163 const unsigned char **raw, size_t *len)
164{
165 *enc_ric = qpack_get_varint(raw, len, 8);
166 if (*len == (uint64_t)-1)
167 return -QPACK_ERR_RIC;
168
169 *sign_bit = **raw & 0x8;
170 *db = qpack_get_varint(raw, len, 7);
171 if (*len == (uint64_t)-1)
172 return -QPACK_ERR_DB;
173
174 return 0;
175}
176
177/* Decode a field section from <len> bytes length <raw> buffer.
178 * Produces the output into <tmp> buffer.
179 */
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200180int qpack_decode_fs(const unsigned char *raw, size_t len, struct buffer *tmp,
181 struct http_hdr *list)
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100182{
183 uint64_t enc_ric, db;
184 int s;
185 unsigned int efl_type;
186 int ret;
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200187 int hdr_idx = 0;
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100188
189 qpack_debug_hexdump(stderr, "[QPACK-DEC-FS] ", (const char *)raw, 0, len);
190
191 ret = qpack_decode_fs_pfx(&enc_ric, &db, &s, &raw, &len);
192 if (ret < 0) {
193 qpack_debug_printf(stderr, "##ERR@%d(%d)\n", __LINE__, ret);
194 goto out;
195 }
196
197 chunk_reset(tmp);
198 qpack_debug_printf(stderr, "enc_ric: %llu db: %llu s=%d\n",
199 (unsigned long long)enc_ric, (unsigned long long)db, !!s);
200 /* Decode field lines */
201 while (len) {
202 qpack_debug_hexdump(stderr, "raw ", (const char *)raw, 0, len);
203 efl_type = *raw & QPACK_EFL_BITMASK;
204 qpack_debug_printf(stderr, "efl_type=0x%02x\n", efl_type);
205 if (efl_type == QPACK_LFL_WPBNM) {
206 /* Literal field line with post-base name reference */
Amaury Denoyelle18a10d02022-03-25 15:11:38 +0100207 uint64_t index __maybe_unused, length;
Amaury Denoyelled96361b2022-03-25 14:56:51 +0100208 unsigned int n __maybe_unused, h __maybe_unused;
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100209
210 qpack_debug_printf(stderr, "literal field line with post-base name reference:");
211 n = *raw & 0x08;
212 index = qpack_get_varint(&raw, &len, 3);
213 if (len == (uint64_t)-1) {
214 qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__);
215 ret = -QPACK_ERR_TRUNCATED;
216 goto out;
217 }
218
219 qpack_debug_printf(stderr, " n=%d index=%llu", !!n, (unsigned long long)index);
220 h = *raw & 0x80;
221 length = qpack_get_varint(&raw, &len, 7);
222 if (len == (uint64_t)-1) {
223 qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__);
224 ret = -QPACK_ERR_TRUNCATED;
225 goto out;
226 }
227
228 qpack_debug_printf(stderr, " h=%d length=%llu", !!h, (unsigned long long)length);
Frédéric Lécaillee629cfd2021-12-15 14:16:16 +0100229
230 if (len < length) {
231 qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__);
232 ret = -QPACK_ERR_TRUNCATED;
233 goto out;
234 }
235
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100236 /* XXX Value string XXX */
237 raw += length;
238 len -= length;
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200239 }
240 else if (efl_type == QPACK_IFL_WPBI) {
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100241 /* Indexed field line with post-base index */
Amaury Denoyelle18a10d02022-03-25 15:11:38 +0100242 uint64_t index __maybe_unused;
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100243
244 qpack_debug_printf(stderr, "indexed field line with post-base index:");
245 index = qpack_get_varint(&raw, &len, 4);
246 if (len == (uint64_t)-1) {
247 qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__);
248 ret = -QPACK_ERR_TRUNCATED;
249 goto out;
250 }
251
252 qpack_debug_printf(stderr, " index=%llu", (unsigned long long)index);
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200253 }
254 else if (efl_type & QPACK_IFL_BIT) {
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100255 /* Indexed field line */
256 uint64_t index;
257 unsigned int t;
258
259 qpack_debug_printf(stderr, "indexed field line:");
260 t = efl_type & 0x40;
261 index = qpack_get_varint(&raw, &len, 6);
262 if (len == (uint64_t)-1) {
263 qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__);
264 ret = -QPACK_ERR_TRUNCATED;
265 goto out;
266 }
267
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200268 if (t)
269 list[hdr_idx++] = qpack_sht[index];
270
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100271 qpack_debug_printf(stderr, " t=%d index=%llu", !!t, (unsigned long long)index);
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200272 }
273 else if (efl_type & QPACK_LFL_WNR_BIT) {
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100274 /* Literal field line with name reference */
275 uint64_t index, length;
Amaury Denoyelle18a10d02022-03-25 15:11:38 +0100276 unsigned int t, n __maybe_unused, h;
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100277
278 qpack_debug_printf(stderr, "Literal field line with name reference:");
279 n = efl_type & 0x20;
280 t = efl_type & 0x10;
281 index = qpack_get_varint(&raw, &len, 4);
282 if (len == (uint64_t)-1) {
283 qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__);
284 ret = -QPACK_ERR_TRUNCATED;
285 goto out;
286 }
287
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200288 if (t)
289 list[hdr_idx] = qpack_sht[index];
290
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100291 qpack_debug_printf(stderr, " n=%d t=%d index=%llu", !!n, !!t, (unsigned long long)index);
292 h = *raw & 0x80;
293 length = qpack_get_varint(&raw, &len, 7);
294 if (len == (uint64_t)-1) {
295 qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__);
296 ret = -QPACK_ERR_TRUNCATED;
297 goto out;
298 }
299
300 qpack_debug_printf(stderr, " h=%d length=%llu", !!h, (unsigned long long)length);
301 if (h) {
302 char *trash;
303 int nlen;
304
305 trash = chunk_newstr(tmp);
306 if (!trash) {
307 qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__);
308 ret = -QPACK_DECOMPRESSION_FAILED;
309 goto out;
310 }
311 nlen = huff_dec(raw, length, trash, tmp->size - tmp->data);
312 if (nlen == (uint32_t)-1) {
313 qpack_debug_printf(stderr, " can't decode huffman.\n");
314 ret = -QPACK_ERR_HUFFMAN;
315 goto out;
316 }
317
318 qpack_debug_printf(stderr, " [name huff %d->%d '%s']", (int)length, (int)nlen, trash);
Amaury Denoyelle9c8c4fa2021-09-30 17:14:55 +0200319 /* makes an ist from tmp storage */
320 b_add(tmp, nlen);
321 list[hdr_idx].v = ist2(trash, nlen);
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100322 }
Amaury Denoyelle7d3aea52021-11-24 16:04:03 +0100323 else {
324 list[hdr_idx].v = ist2(raw, length);
325 }
326
Frédéric Lécaillee629cfd2021-12-15 14:16:16 +0100327 if (len < length) {
328 qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__);
329 ret = -QPACK_ERR_TRUNCATED;
330 goto out;
331 }
332
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100333 raw += length;
334 len -= length;
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200335 ++hdr_idx;
336 }
337 else if (efl_type & QPACK_LFL_WLN_BIT) {
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100338 /* Literal field line with literal name */
Amaury Denoyelle18a10d02022-03-25 15:11:38 +0100339 unsigned int n __maybe_unused, hname __maybe_unused, hvalue __maybe_unused;
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100340 uint64_t name_len, value_len;
341
342 qpack_debug_printf(stderr, "Literal field line with literal name:");
343 n = *raw & 0x10;
344 hname = *raw & 0x08;
345 name_len = qpack_get_varint(&raw, &len, 3);
346 if (len == (uint64_t)-1) {
347 qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__);
348 ret = -QPACK_ERR_TRUNCATED;
349 goto out;
350 }
351
Amaury Denoyelleab9cec72022-02-14 14:45:10 +0100352 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 +0100353 /* Name string */
Frédéric Lécaillee629cfd2021-12-15 14:16:16 +0100354
355 if (len < name_len) {
356 qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__);
357 ret = -QPACK_ERR_TRUNCATED;
358 goto out;
359 }
360
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100361 raw += name_len;
362 len -= name_len;
363 hvalue = *raw & 0x80;
364 value_len = qpack_get_varint(&raw, &len, 7);
365 if (len == (uint64_t)-1) {
366 qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__);
367 ret = -QPACK_ERR_TRUNCATED;
368 goto out;
369 }
370
371 qpack_debug_printf(stderr, " hvalue=%d value_len=%llu", !!hvalue, (unsigned long long)value_len);
372
Frédéric Lécaillee629cfd2021-12-15 14:16:16 +0100373 if (len < value_len) {
374 qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__);
375 ret = -QPACK_ERR_TRUNCATED;
376 goto out;
377 }
378
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100379 /* XXX Value string XXX */
380 raw += value_len;
381 len -= value_len;
382 }
383 qpack_debug_printf(stderr, "\n");
384 }
385
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200386 /* put an end marker */
387 list[hdr_idx].n = list[hdr_idx].v = IST_NULL;
388
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100389 out:
390 qpack_debug_printf(stderr, "-- done: ret=%d\n", ret);
391 return ret;
392}