blob: eadb6deb4f7952c262f99e81242f659c7c27c3f7 [file] [log] [blame]
Willy Tarreau679790b2017-05-30 19:09:44 +02001/*
2 * HPACK decompressor (RFC7541)
3 *
4 * Copyright (C) 2014-2017 Willy Tarreau <willy@haproxy.org>
5 * Copyright (C) 2017 HAProxy Technologies
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
26 */
27
28#include <stdint.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32
33#include <common/hpack-dec.h>
34#include <common/hpack-huff.h>
35#include <common/hpack-tbl.h>
Willy Tarreau59a10fb2017-11-21 20:03:02 +010036#include <common/chunk.h>
Willy Tarreau679790b2017-05-30 19:09:44 +020037#include <common/ist.h>
38
39#include <types/global.h>
40
Willy Tarreau679790b2017-05-30 19:09:44 +020041
42#if defined(DEBUG_HPACK)
43#define hpack_debug_printf printf
44#else
45#define hpack_debug_printf(...) do { } while (0)
46#endif
47
48/* reads a varint from <raw>'s lowest <b> bits and <len> bytes max (raw included).
49 * returns the 32-bit value on success after updating raw_in and len_in. Forces
50 * len_in to (uint32_t)-1 on truncated input.
51 */
52static uint32_t get_var_int(const uint8_t **raw_in, uint32_t *len_in, int b)
53{
54 uint32_t ret = 0;
55 int len = *len_in;
56 const uint8_t *raw = *raw_in;
57 uint8_t shift = 0;
58
59 len--;
60 ret = *(raw++) & ((1 << b) - 1);
61 if (ret != (uint32_t)((1 << b) - 1))
62 goto end;
63
64 while (1) {
65 if (!len)
66 goto too_short;
67 if (!(*raw & 128))
68 break;
69 ret += ((uint32_t)(*raw++) & 127) << shift;
70 shift += 7;
71 len--;
72 }
73
74 /* last 7 bits */
75 if (!len)
76 goto too_short;
77 len--;
78 ret += ((uint32_t)(*raw++) & 127) << shift;
79
80 end:
81 *raw_in = raw;
82 *len_in = len;
83 return ret;
84
85 too_short:
86 *len_in = (uint32_t)-1;
87 return 0;
88}
89
Willy Tarreau59a10fb2017-11-21 20:03:02 +010090/* returns the pseudo-header <idx> corresponds to among the following values :
91 * - 0 = unknown, the header's string needs to be used instead
92 * - 1 = ":authority"
93 * - 2 = ":method"
94 * - 3 = ":path"
95 * - 4 = ":scheme"
96 * - 5 = ":status"
Willy Tarreau679790b2017-05-30 19:09:44 +020097 */
98static inline int hpack_idx_to_phdr(uint32_t idx)
99{
100 if (idx > 14)
101 return 0;
102
103 idx >>= 1;
104 idx <<= 2;
105 return (0x55554321U >> idx) & 0xF;
106}
107
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100108/* If <idx> designates a static header, returns <in>. Otherwise allocates some
109 * room from chunk <store> to duplicate <in> into it and returns the string
110 * allocated there. In case of allocation failure, returns a string whose
111 * pointer is NULL.
Willy Tarreau679790b2017-05-30 19:09:44 +0200112 */
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100113static inline struct ist hpack_alloc_string(struct chunk *store, int idx, struct ist in)
Willy Tarreau679790b2017-05-30 19:09:44 +0200114{
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100115 struct ist out;
Willy Tarreau679790b2017-05-30 19:09:44 +0200116
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100117 if (idx < HPACK_SHT_SIZE)
118 return in;
Willy Tarreau679790b2017-05-30 19:09:44 +0200119
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100120 out.len = in.len;
121 out.ptr = chunk_newstr(store);
122 if (unlikely(!out.ptr))
123 return out;
Willy Tarreau679790b2017-05-30 19:09:44 +0200124
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100125 if (unlikely(store->len + out.len > store->size)) {
126 out.ptr = NULL;
127 return out;
Willy Tarreau679790b2017-05-30 19:09:44 +0200128 }
129
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100130 store->len += out.len;
131 memcpy(out.ptr, in.ptr, out.len);
132 return out;
Willy Tarreau679790b2017-05-30 19:09:44 +0200133}
134
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100135/* decode an HPACK frame starting at <raw> for <len> bytes, using the dynamic
136 * headers table <dht>, produces the output into list <list> of <list_size>
137 * entries max, and uses pre-allocated buffer <tmp> for temporary storage (some
138 * list elements will point to it). Some <list> name entries may be made of a
139 * NULL pointer and a len, in which case they will designate a pseudo header
140 * index according to the values returned by hpack_idx_to_phdr() above. The
141 * number of <list> entries used is returned on success, or <0 on failure, with
142 * the opposite one of the HPACK_ERR_* codes. A last element is always zeroed
143 * and is not counted in the number of returned entries. This way the caller
144 * can use list[].n.len == 0 as a marker for the end of list.
Willy Tarreau679790b2017-05-30 19:09:44 +0200145 */
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100146int hpack_decode_frame(struct hpack_dht *dht, const uint8_t *raw, uint32_t len,
147 struct http_hdr *list, int list_size, struct chunk *tmp)
Willy Tarreau679790b2017-05-30 19:09:44 +0200148{
149 uint32_t idx;
150 uint32_t nlen;
151 uint32_t vlen;
152 uint8_t huff;
Willy Tarreau679790b2017-05-30 19:09:44 +0200153 struct ist name;
154 struct ist value;
Willy Tarreau679790b2017-05-30 19:09:44 +0200155 int must_index;
156 int ret;
Willy Tarreau679790b2017-05-30 19:09:44 +0200157
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100158 chunk_reset(tmp);
159 ret = 0;
Willy Tarreau679790b2017-05-30 19:09:44 +0200160 while (len) {
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100161 int __maybe_unused code = *raw; /* first byte, only for debugging */
Willy Tarreau679790b2017-05-30 19:09:44 +0200162
163 must_index = 0;
164 if (*raw >= 0x80) {
165 /* indexed header field */
166 if (*raw == 0x80) {
167 hpack_debug_printf("unhandled code 0x%02x (raw=%p, len=%d)\n", *raw, raw, len);
168 ret = -HPACK_ERR_UNKNOWN_OPCODE;
169 goto leave;
170 }
171
172 hpack_debug_printf("%02x: p14: indexed header field : ", code);
173
174 idx = get_var_int(&raw, &len, 7);
175 if (len == (uint32_t)-1) { // truncated
176 ret = -HPACK_ERR_TRUNCATED;
177 goto leave;
178 }
179
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100180 value = hpack_alloc_string(tmp, idx, hpack_idx_to_value(dht, idx));
181 if (!value.ptr) {
182 ret = -HPACK_ERR_TOO_LARGE;
183 goto leave;
184 }
Willy Tarreau679790b2017-05-30 19:09:44 +0200185
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100186 /* here we don't index so we can always keep the pseudo header number */
187 name = ist2(NULL, hpack_idx_to_phdr(idx));
Willy Tarreau679790b2017-05-30 19:09:44 +0200188
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100189 if (!name.len) {
190 name = hpack_alloc_string(tmp, idx, hpack_idx_to_name(dht, idx));
191 if (!name.ptr) {
192 ret = -HPACK_ERR_TOO_LARGE;
193 goto leave;
194 }
195 }
196 /* <name> and <value> are now set and point to stable values */
Willy Tarreau679790b2017-05-30 19:09:44 +0200197 }
198 else if (*raw >= 0x20 && *raw <= 0x3f) {
199 /* max dyn table size change */
200 idx = get_var_int(&raw, &len, 5);
201 if (len == (uint32_t)-1) { // truncated
202 ret = -HPACK_ERR_TRUNCATED;
203 goto leave;
204 }
205 continue;
206 }
207 else if (!(*raw & (*raw - 0x10))) {
208 /* 0x00, 0x10, and 0x40 (0x20 and 0x80 were already handled above) */
209
210 /* literal header field without/never/with incremental indexing -- literal name */
211 if (*raw == 0x00)
212 hpack_debug_printf("%02x: p17: literal without indexing : ", code);
213 else if (*raw == 0x10)
214 hpack_debug_printf("%02x: p18: literal never indexed : ", code);
215 else if (*raw == 0x40)
216 hpack_debug_printf("%02x: p16: literal with indexing : ", code);
217
218 if (*raw == 0x40)
219 must_index = 1;
220
221 raw++; len--;
222
223 /* retrieve name */
224 if (!len) { // truncated
225 ret = -HPACK_ERR_TRUNCATED;
226 goto leave;
227 }
228
229 huff = *raw & 0x80;
230 nlen = get_var_int(&raw, &len, 7);
231 if (len == (uint32_t)-1 || len < nlen) { // truncated
232 ret = -HPACK_ERR_TRUNCATED;
233 goto leave;
234 }
235
236 name = ist2(raw, nlen);
237
238 raw += nlen;
239 len -= nlen;
Willy Tarreau679790b2017-05-30 19:09:44 +0200240
241 if (huff) {
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100242 char *ntrash = chunk_newstr(tmp);
243 if (!ntrash) {
244 ret = -HPACK_ERR_TOO_LARGE;
245 goto leave;
246 }
247
248 nlen = huff_dec((const uint8_t *)name.ptr, name.len, ntrash, tmp->size - tmp->len);
Willy Tarreau679790b2017-05-30 19:09:44 +0200249 if (nlen == (uint32_t)-1) {
250 hpack_debug_printf("2: can't decode huffman.\n");
251 ret = -HPACK_ERR_HUFFMAN;
252 goto leave;
253 }
254 tmp->len += nlen; // make room for the value
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100255 name = ist2(ntrash, nlen);
Willy Tarreau679790b2017-05-30 19:09:44 +0200256 }
257
258 /* retrieve value */
259 if (!len) { // truncated
260 ret = -HPACK_ERR_TRUNCATED;
261 goto leave;
262 }
263
264 huff = *raw & 0x80;
265 vlen = get_var_int(&raw, &len, 7);
266 if (len == (uint32_t)-1 || len < vlen) { // truncated
267 ret = -HPACK_ERR_TRUNCATED;
268 goto leave;
269 }
270
271 value = ist2(raw, vlen);
272 raw += vlen;
273 len -= vlen;
274
275 if (huff) {
276 char *vtrash = chunk_newstr(tmp);
277 if (!vtrash) {
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100278 ret = -HPACK_ERR_TOO_LARGE;
Willy Tarreau679790b2017-05-30 19:09:44 +0200279 goto leave;
280 }
281
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100282 vlen = huff_dec((const uint8_t *)value.ptr, value.len, vtrash, tmp->size - tmp->len);
Willy Tarreau679790b2017-05-30 19:09:44 +0200283 if (vlen == (uint32_t)-1) {
284 hpack_debug_printf("3: can't decode huffman.\n");
285 ret = -HPACK_ERR_HUFFMAN;
286 goto leave;
287 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100288 tmp->len += vlen; // make room for the value
Willy Tarreau679790b2017-05-30 19:09:44 +0200289 value = ist2(vtrash, vlen);
290 }
291
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100292 /* <name> and <value> are correctly filled here */
Willy Tarreau679790b2017-05-30 19:09:44 +0200293 }
294 else {
295 /* 0x01..0x0f : literal header field without indexing -- indexed name */
296 /* 0x11..0x1f : literal header field never indexed -- indexed name */
297 /* 0x41..0x7f : literal header field with incremental indexing -- indexed name */
298
299 if (*raw <= 0x0f)
300 hpack_debug_printf("%02x: p16: literal without indexing -- indexed name : ", code);
301 else if (*raw >= 0x41)
302 hpack_debug_printf("%02x: p15: literal with indexing -- indexed name : ", code);
303 else
304 hpack_debug_printf("%02x: p16: literal never indexed -- indexed name : ", code);
305
306 /* retrieve name index */
307 if (*raw >= 0x41) {
308 must_index = 1;
309 idx = get_var_int(&raw, &len, 6);
310 }
311 else
312 idx = get_var_int(&raw, &len, 4);
313
314 if (len == (uint32_t)-1 || !len) { // truncated
315 ret = -HPACK_ERR_TRUNCATED;
316 goto leave;
317 }
318
319 /* retrieve value */
320 huff = *raw & 0x80;
321 vlen = get_var_int(&raw, &len, 7);
322 if (len == (uint32_t)-1 || len < vlen) { // truncated
323 ret = -HPACK_ERR_TRUNCATED;
324 goto leave;
325 }
326
327 value = ist2(raw, vlen);
328 raw += vlen;
329 len -= vlen;
330
331 if (huff) {
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100332 char *vtrash = chunk_newstr(tmp);
333 if (!vtrash) {
334 ret = -HPACK_ERR_TOO_LARGE;
335 goto leave;
336 }
337
338 vlen = huff_dec((const uint8_t *)value.ptr, value.len, vtrash, tmp->size - tmp->len);
Willy Tarreau679790b2017-05-30 19:09:44 +0200339 if (vlen == (uint32_t)-1) {
340 hpack_debug_printf("1: can't decode huffman.\n");
341 ret = -HPACK_ERR_HUFFMAN;
342 goto leave;
343 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100344 tmp->len += vlen; // make room for the value
345 value = ist2(vtrash, vlen);
Willy Tarreau679790b2017-05-30 19:09:44 +0200346 }
347
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100348 name = ist2(NULL, 0);
349 if (!must_index)
350 name.len = hpack_idx_to_phdr(idx);
Willy Tarreau679790b2017-05-30 19:09:44 +0200351
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100352 if (!name.len)
353 name = hpack_idx_to_name(dht, idx);
354 /* <name> and <value> are correctly filled here */
Willy Tarreau679790b2017-05-30 19:09:44 +0200355 }
Willy Tarreau679790b2017-05-30 19:09:44 +0200356
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100357 /* here's what we have here :
358 * - name.len > 0
359 * - value is filled with either const data or data allocated from tmp
360 * - name.ptr == NULL && !must_index : known pseudo-header #name.len
361 * - name.ptr != NULL || must_index : general header, unknown pseudo-header or index needed
362 */
363 if (ret >= list_size) {
Willy Tarreau679790b2017-05-30 19:09:44 +0200364 ret = -HPACK_ERR_TOO_LARGE;
365 goto leave;
366 }
367
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100368 list[ret].n = name;
369 list[ret].v = value;
370 ret++;
Willy Tarreau679790b2017-05-30 19:09:44 +0200371
372 if (must_index && hpack_dht_insert(dht, name, value) < 0) {
373 hpack_debug_printf("failed to find some room in the dynamic table\n");
374 ret = -HPACK_ERR_DHT_INSERT_FAIL;
375 goto leave;
376 }
377
Willy Tarreau679790b2017-05-30 19:09:44 +0200378 hpack_debug_printf("\e[1;34m%s\e[0m: ",
379 istpad(trash.str, name).ptr);
380
381 hpack_debug_printf("\e[1;35m%s\e[0m [idx=%d, used=%d]\n",
382 istpad(trash.str, value).ptr,
383 must_index, dht->used);
Willy Tarreau679790b2017-05-30 19:09:44 +0200384 }
385
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100386 if (ret >= list_size) {
Willy Tarreau679790b2017-05-30 19:09:44 +0200387 ret = -HPACK_ERR_TOO_LARGE;
388 goto leave;
389 }
390
Willy Tarreau59a10fb2017-11-21 20:03:02 +0100391 /* put an end marker */
392 list[ret].n = list[ret].v = ist2(NULL, 0);
393 ret++;
Willy Tarreau679790b2017-05-30 19:09:44 +0200394
Willy Tarreau679790b2017-05-30 19:09:44 +0200395 leave:
396 return ret;
397}