blob: c5e42a0f5da4591f4a076fd2a35cd77489607022 [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 Denoyelle3db98e92022-05-13 15:41:04 +020030#include <haproxy/ncbuf.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
96/* Decode an encoder stream */
Amaury Denoyelle6b923942022-05-23 14:25:53 +020097int qpack_decode_enc(struct qcs *qcs, void *ctx)
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +010098{
99 size_t len;
Amaury Denoyelle3db98e92022-05-13 15:41:04 +0200100 struct ncbuf *rxbuf;
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100101 unsigned char inst;
102
Amaury Denoyelle6b923942022-05-23 14:25:53 +0200103 rxbuf = &qcs->rx.ncbuf;
Amaury Denoyelle3db98e92022-05-13 15:41:04 +0200104 len = ncb_data(rxbuf, 0);
105 qpack_debug_hexdump(stderr, "[QPACK-DEC-ENC] ", ncb_head(rxbuf), 0, len);
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100106
107 if (!len) {
108 qpack_debug_printf(stderr, "[QPACK-DEC-ENC] empty stream\n");
109 return 0;
110 }
111
Amaury Denoyelle3db98e92022-05-13 15:41:04 +0200112 inst = (unsigned char)*ncb_head(rxbuf) & QPACK_ENC_INST_BITMASK;
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100113 if (inst == QPACK_ENC_INST_DUP) {
114 /* Duplicate */
115 }
116 else if (inst & QPACK_ENC_INST_IWNR_BIT) {
117 /* Insert With Name Reference */
118 }
119 else if (inst & QPACK_ENC_INST_IWLN_BIT) {
120 /* Insert with literal name */
121 }
122 else if (inst & QPACK_ENC_INST_SDTC_BIT) {
123 /* Set dynamic table capacity */
124 }
125
126 return 1;
127}
128
129/* Decode an decoder stream */
Amaury Denoyelle6b923942022-05-23 14:25:53 +0200130int qpack_decode_dec(struct qcs *qcs, void *ctx)
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100131{
132 size_t len;
Amaury Denoyelle3db98e92022-05-13 15:41:04 +0200133 struct ncbuf *rxbuf;
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100134 unsigned char inst;
135
Amaury Denoyelle6b923942022-05-23 14:25:53 +0200136 rxbuf = &qcs->rx.ncbuf;
Amaury Denoyelle3db98e92022-05-13 15:41:04 +0200137 len = ncb_data(rxbuf, 0);
138 qpack_debug_hexdump(stderr, "[QPACK-DEC-DEC] ", ncb_head(rxbuf), 0, len);
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100139
140 if (!len) {
141 qpack_debug_printf(stderr, "[QPACK-DEC-DEC] empty stream\n");
142 return 0;
143 }
144
Amaury Denoyelle3db98e92022-05-13 15:41:04 +0200145 inst = (unsigned char)*ncb_head(rxbuf) & QPACK_DEC_INST_BITMASK;
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100146 if (inst == QPACK_DEC_INST_ICINC) {
147 /* Insert count increment */
148 }
149 else if (inst & QPACK_DEC_INST_SACK) {
150 /* Section Acknowledgment */
151 }
152 else if (inst & QPACK_DEC_INST_SCCL) {
153 /* Stream cancellation */
154 }
155
156 return 1;
157}
158
159/* Decode a field section prefix made of <enc_ric> and <db> two varints.
160 * Also set the 'S' sign bit for <db>.
161 * Return a negative error if failed, 0 if not.
162 */
163static int qpack_decode_fs_pfx(uint64_t *enc_ric, uint64_t *db, int *sign_bit,
164 const unsigned char **raw, size_t *len)
165{
166 *enc_ric = qpack_get_varint(raw, len, 8);
167 if (*len == (uint64_t)-1)
168 return -QPACK_ERR_RIC;
169
170 *sign_bit = **raw & 0x8;
171 *db = qpack_get_varint(raw, len, 7);
172 if (*len == (uint64_t)-1)
173 return -QPACK_ERR_DB;
174
175 return 0;
176}
177
178/* Decode a field section from <len> bytes length <raw> buffer.
179 * Produces the output into <tmp> buffer.
180 */
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200181int qpack_decode_fs(const unsigned char *raw, size_t len, struct buffer *tmp,
182 struct http_hdr *list)
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100183{
184 uint64_t enc_ric, db;
185 int s;
186 unsigned int efl_type;
187 int ret;
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200188 int hdr_idx = 0;
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100189
190 qpack_debug_hexdump(stderr, "[QPACK-DEC-FS] ", (const char *)raw, 0, len);
191
192 ret = qpack_decode_fs_pfx(&enc_ric, &db, &s, &raw, &len);
193 if (ret < 0) {
194 qpack_debug_printf(stderr, "##ERR@%d(%d)\n", __LINE__, ret);
195 goto out;
196 }
197
198 chunk_reset(tmp);
199 qpack_debug_printf(stderr, "enc_ric: %llu db: %llu s=%d\n",
200 (unsigned long long)enc_ric, (unsigned long long)db, !!s);
201 /* Decode field lines */
202 while (len) {
203 qpack_debug_hexdump(stderr, "raw ", (const char *)raw, 0, len);
204 efl_type = *raw & QPACK_EFL_BITMASK;
205 qpack_debug_printf(stderr, "efl_type=0x%02x\n", efl_type);
206 if (efl_type == QPACK_LFL_WPBNM) {
207 /* Literal field line with post-base name reference */
Amaury Denoyelle18a10d02022-03-25 15:11:38 +0100208 uint64_t index __maybe_unused, length;
Amaury Denoyelled96361b2022-03-25 14:56:51 +0100209 unsigned int n __maybe_unused, h __maybe_unused;
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100210
211 qpack_debug_printf(stderr, "literal field line with post-base name reference:");
212 n = *raw & 0x08;
213 index = qpack_get_varint(&raw, &len, 3);
214 if (len == (uint64_t)-1) {
215 qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__);
216 ret = -QPACK_ERR_TRUNCATED;
217 goto out;
218 }
219
220 qpack_debug_printf(stderr, " n=%d index=%llu", !!n, (unsigned long long)index);
221 h = *raw & 0x80;
222 length = qpack_get_varint(&raw, &len, 7);
223 if (len == (uint64_t)-1) {
224 qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__);
225 ret = -QPACK_ERR_TRUNCATED;
226 goto out;
227 }
228
229 qpack_debug_printf(stderr, " h=%d length=%llu", !!h, (unsigned long long)length);
Frédéric Lécaillee629cfd2021-12-15 14:16:16 +0100230
231 if (len < length) {
232 qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__);
233 ret = -QPACK_ERR_TRUNCATED;
234 goto out;
235 }
236
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100237 /* XXX Value string XXX */
238 raw += length;
239 len -= length;
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200240 }
241 else if (efl_type == QPACK_IFL_WPBI) {
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100242 /* Indexed field line with post-base index */
Amaury Denoyelle18a10d02022-03-25 15:11:38 +0100243 uint64_t index __maybe_unused;
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100244
245 qpack_debug_printf(stderr, "indexed field line with post-base index:");
246 index = qpack_get_varint(&raw, &len, 4);
247 if (len == (uint64_t)-1) {
248 qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__);
249 ret = -QPACK_ERR_TRUNCATED;
250 goto out;
251 }
252
253 qpack_debug_printf(stderr, " index=%llu", (unsigned long long)index);
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200254 }
255 else if (efl_type & QPACK_IFL_BIT) {
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100256 /* Indexed field line */
257 uint64_t index;
258 unsigned int t;
259
260 qpack_debug_printf(stderr, "indexed field line:");
261 t = efl_type & 0x40;
262 index = qpack_get_varint(&raw, &len, 6);
263 if (len == (uint64_t)-1) {
264 qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__);
265 ret = -QPACK_ERR_TRUNCATED;
266 goto out;
267 }
268
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200269 if (t)
270 list[hdr_idx++] = qpack_sht[index];
271
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100272 qpack_debug_printf(stderr, " t=%d index=%llu", !!t, (unsigned long long)index);
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200273 }
274 else if (efl_type & QPACK_LFL_WNR_BIT) {
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100275 /* Literal field line with name reference */
276 uint64_t index, length;
Amaury Denoyelle18a10d02022-03-25 15:11:38 +0100277 unsigned int t, n __maybe_unused, h;
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100278
279 qpack_debug_printf(stderr, "Literal field line with name reference:");
280 n = efl_type & 0x20;
281 t = efl_type & 0x10;
282 index = qpack_get_varint(&raw, &len, 4);
283 if (len == (uint64_t)-1) {
284 qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__);
285 ret = -QPACK_ERR_TRUNCATED;
286 goto out;
287 }
288
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200289 if (t)
290 list[hdr_idx] = qpack_sht[index];
291
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100292 qpack_debug_printf(stderr, " n=%d t=%d index=%llu", !!n, !!t, (unsigned long long)index);
293 h = *raw & 0x80;
294 length = qpack_get_varint(&raw, &len, 7);
295 if (len == (uint64_t)-1) {
296 qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__);
297 ret = -QPACK_ERR_TRUNCATED;
298 goto out;
299 }
300
301 qpack_debug_printf(stderr, " h=%d length=%llu", !!h, (unsigned long long)length);
302 if (h) {
303 char *trash;
304 int nlen;
305
306 trash = chunk_newstr(tmp);
307 if (!trash) {
308 qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__);
309 ret = -QPACK_DECOMPRESSION_FAILED;
310 goto out;
311 }
312 nlen = huff_dec(raw, length, trash, tmp->size - tmp->data);
313 if (nlen == (uint32_t)-1) {
314 qpack_debug_printf(stderr, " can't decode huffman.\n");
315 ret = -QPACK_ERR_HUFFMAN;
316 goto out;
317 }
318
319 qpack_debug_printf(stderr, " [name huff %d->%d '%s']", (int)length, (int)nlen, trash);
Amaury Denoyelle9c8c4fa2021-09-30 17:14:55 +0200320 /* makes an ist from tmp storage */
321 b_add(tmp, nlen);
322 list[hdr_idx].v = ist2(trash, nlen);
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100323 }
Amaury Denoyelle7d3aea52021-11-24 16:04:03 +0100324 else {
325 list[hdr_idx].v = ist2(raw, length);
326 }
327
Frédéric Lécaillee629cfd2021-12-15 14:16:16 +0100328 if (len < length) {
329 qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__);
330 ret = -QPACK_ERR_TRUNCATED;
331 goto out;
332 }
333
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100334 raw += length;
335 len -= length;
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200336 ++hdr_idx;
337 }
338 else if (efl_type & QPACK_LFL_WLN_BIT) {
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100339 /* Literal field line with literal name */
Amaury Denoyelle18a10d02022-03-25 15:11:38 +0100340 unsigned int n __maybe_unused, hname __maybe_unused, hvalue __maybe_unused;
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100341 uint64_t name_len, value_len;
342
343 qpack_debug_printf(stderr, "Literal field line with literal name:");
344 n = *raw & 0x10;
345 hname = *raw & 0x08;
346 name_len = qpack_get_varint(&raw, &len, 3);
347 if (len == (uint64_t)-1) {
348 qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__);
349 ret = -QPACK_ERR_TRUNCATED;
350 goto out;
351 }
352
Amaury Denoyelleab9cec72022-02-14 14:45:10 +0100353 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 +0100354 /* Name string */
Frédéric Lécaillee629cfd2021-12-15 14:16:16 +0100355
356 if (len < name_len) {
357 qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__);
358 ret = -QPACK_ERR_TRUNCATED;
359 goto out;
360 }
361
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100362 raw += name_len;
363 len -= name_len;
364 hvalue = *raw & 0x80;
365 value_len = qpack_get_varint(&raw, &len, 7);
366 if (len == (uint64_t)-1) {
367 qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__);
368 ret = -QPACK_ERR_TRUNCATED;
369 goto out;
370 }
371
372 qpack_debug_printf(stderr, " hvalue=%d value_len=%llu", !!hvalue, (unsigned long long)value_len);
373
Frédéric Lécaillee629cfd2021-12-15 14:16:16 +0100374 if (len < value_len) {
375 qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__);
376 ret = -QPACK_ERR_TRUNCATED;
377 goto out;
378 }
379
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100380 /* XXX Value string XXX */
381 raw += value_len;
382 len -= value_len;
383 }
384 qpack_debug_printf(stderr, "\n");
385 }
386
Amaury Denoyellefd7cdc32021-08-24 15:13:20 +0200387 /* put an end marker */
388 list[hdr_idx].n = list[hdr_idx].v = IST_NULL;
389
Frédéric Lécailleb4672fb2021-03-03 16:13:10 +0100390 out:
391 qpack_debug_printf(stderr, "-- done: ret=%d\n", ret);
392 return ret;
393}