Willy Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
Willy Tarreau | a1bd1fa | 2019-03-29 17:26:33 +0100 | [diff] [blame] | 28 | #include <inttypes.h> |
Willy Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 29 | #include <stdio.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <string.h> |
| 32 | |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 33 | #include <haproxy/tools.h> |
Willy Tarreau | be327fa | 2020-06-03 09:09:57 +0200 | [diff] [blame] | 34 | #include <haproxy/hpack-dec.h> |
| 35 | #include <haproxy/hpack-huff.h> |
| 36 | #include <haproxy/hpack-tbl.h> |
Willy Tarreau | c13ed53 | 2020-06-02 10:22:45 +0200 | [diff] [blame] | 37 | #include <haproxy/chunk.h> |
Willy Tarreau | bf07314 | 2020-06-03 12:04:01 +0200 | [diff] [blame] | 38 | #include <haproxy/h2.h> |
Willy Tarreau | eb6f701 | 2020-05-27 16:21:26 +0200 | [diff] [blame] | 39 | #include <import/ist.h> |
Willy Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 40 | |
| 41 | #include <types/global.h> |
| 42 | |
Willy Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 43 | |
| 44 | #if defined(DEBUG_HPACK) |
| 45 | #define hpack_debug_printf printf |
Willy Tarreau | c775f83 | 2017-12-30 13:17:37 +0100 | [diff] [blame] | 46 | #define hpack_debug_hexdump debug_hexdump |
Willy Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 47 | #else |
| 48 | #define hpack_debug_printf(...) do { } while (0) |
Willy Tarreau | c775f83 | 2017-12-30 13:17:37 +0100 | [diff] [blame] | 49 | #define hpack_debug_hexdump(...) do { } while (0) |
Willy Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 50 | #endif |
| 51 | |
| 52 | /* reads a varint from <raw>'s lowest <b> bits and <len> bytes max (raw included). |
| 53 | * returns the 32-bit value on success after updating raw_in and len_in. Forces |
| 54 | * len_in to (uint32_t)-1 on truncated input. |
| 55 | */ |
| 56 | static uint32_t get_var_int(const uint8_t **raw_in, uint32_t *len_in, int b) |
| 57 | { |
| 58 | uint32_t ret = 0; |
| 59 | int len = *len_in; |
| 60 | const uint8_t *raw = *raw_in; |
| 61 | uint8_t shift = 0; |
| 62 | |
| 63 | len--; |
| 64 | ret = *(raw++) & ((1 << b) - 1); |
| 65 | if (ret != (uint32_t)((1 << b) - 1)) |
| 66 | goto end; |
| 67 | |
Willy Tarreau | 077d366 | 2020-02-05 15:28:55 +0100 | [diff] [blame] | 68 | while (len && (*raw & 128)) { |
Willy Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 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 Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 90 | /* 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 Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 97 | */ |
| 98 | static 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 Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 108 | /* 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 Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 112 | */ |
Willy Tarreau | 7f2a44d | 2018-09-17 14:07:33 +0200 | [diff] [blame] | 113 | static inline struct ist hpack_alloc_string(struct buffer *store, uint32_t idx, |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 114 | struct ist in) |
Willy Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 115 | { |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 116 | struct ist out; |
Willy Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 117 | |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 118 | if (idx < HPACK_SHT_SIZE) |
| 119 | return in; |
Willy Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 120 | |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 121 | out.len = in.len; |
| 122 | out.ptr = chunk_newstr(store); |
| 123 | if (unlikely(!out.ptr)) |
| 124 | return out; |
Willy Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 125 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 126 | if (unlikely(store->data + out.len > store->size)) { |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 127 | out.ptr = NULL; |
| 128 | return out; |
Willy Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 129 | } |
| 130 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 131 | store->data += out.len; |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 132 | memcpy(out.ptr, in.ptr, out.len); |
| 133 | return out; |
Willy Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 134 | } |
| 135 | |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 136 | /* decode an HPACK frame starting at <raw> for <len> bytes, using the dynamic |
| 137 | * headers table <dht>, produces the output into list <list> of <list_size> |
| 138 | * entries max, and uses pre-allocated buffer <tmp> for temporary storage (some |
| 139 | * list elements will point to it). Some <list> name entries may be made of a |
| 140 | * NULL pointer and a len, in which case they will designate a pseudo header |
| 141 | * index according to the values returned by hpack_idx_to_phdr() above. The |
| 142 | * number of <list> entries used is returned on success, or <0 on failure, with |
| 143 | * the opposite one of the HPACK_ERR_* codes. A last element is always zeroed |
| 144 | * and is not counted in the number of returned entries. This way the caller |
| 145 | * can use list[].n.len == 0 as a marker for the end of list. |
Willy Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 146 | */ |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 147 | int hpack_decode_frame(struct hpack_dht *dht, const uint8_t *raw, uint32_t len, |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 148 | struct http_hdr *list, int list_size, |
| 149 | struct buffer *tmp) |
Willy Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 150 | { |
| 151 | uint32_t idx; |
| 152 | uint32_t nlen; |
| 153 | uint32_t vlen; |
| 154 | uint8_t huff; |
Willy Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 155 | struct ist name; |
| 156 | struct ist value; |
Willy Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 157 | int must_index; |
| 158 | int ret; |
Willy Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 159 | |
Willy Tarreau | c775f83 | 2017-12-30 13:17:37 +0100 | [diff] [blame] | 160 | hpack_debug_hexdump(stderr, "[HPACK-DEC] ", (const char *)raw, 0, len); |
| 161 | |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 162 | chunk_reset(tmp); |
| 163 | ret = 0; |
Willy Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 164 | while (len) { |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 165 | int __maybe_unused code = *raw; /* first byte, only for debugging */ |
Willy Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 166 | |
| 167 | must_index = 0; |
| 168 | if (*raw >= 0x80) { |
| 169 | /* indexed header field */ |
| 170 | if (*raw == 0x80) { |
| 171 | hpack_debug_printf("unhandled code 0x%02x (raw=%p, len=%d)\n", *raw, raw, len); |
| 172 | ret = -HPACK_ERR_UNKNOWN_OPCODE; |
| 173 | goto leave; |
| 174 | } |
| 175 | |
| 176 | hpack_debug_printf("%02x: p14: indexed header field : ", code); |
| 177 | |
| 178 | idx = get_var_int(&raw, &len, 7); |
| 179 | if (len == (uint32_t)-1) { // truncated |
Willy Tarreau | c775f83 | 2017-12-30 13:17:37 +0100 | [diff] [blame] | 180 | hpack_debug_printf("##ERR@%d##\n", __LINE__); |
Willy Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 181 | ret = -HPACK_ERR_TRUNCATED; |
| 182 | goto leave; |
| 183 | } |
| 184 | |
Willy Tarreau | c775f83 | 2017-12-30 13:17:37 +0100 | [diff] [blame] | 185 | hpack_debug_printf(" idx=%u ", idx); |
| 186 | |
Willy Tarreau | d85ba4e | 2017-12-03 12:12:17 +0100 | [diff] [blame] | 187 | if (!hpack_valid_idx(dht, idx)) { |
Willy Tarreau | c775f83 | 2017-12-30 13:17:37 +0100 | [diff] [blame] | 188 | hpack_debug_printf("##ERR@%d##\n", __LINE__); |
Willy Tarreau | d85ba4e | 2017-12-03 12:12:17 +0100 | [diff] [blame] | 189 | ret = -HPACK_ERR_TOO_LARGE; |
| 190 | goto leave; |
| 191 | } |
| 192 | |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 193 | value = hpack_alloc_string(tmp, idx, hpack_idx_to_value(dht, idx)); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 194 | if (!isttest(value)) { |
Willy Tarreau | c775f83 | 2017-12-30 13:17:37 +0100 | [diff] [blame] | 195 | hpack_debug_printf("##ERR@%d##\n", __LINE__); |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 196 | ret = -HPACK_ERR_TOO_LARGE; |
| 197 | goto leave; |
| 198 | } |
Willy Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 199 | |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 200 | /* here we don't index so we can always keep the pseudo header number */ |
| 201 | name = ist2(NULL, hpack_idx_to_phdr(idx)); |
Willy Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 202 | |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 203 | if (!name.len) { |
| 204 | name = hpack_alloc_string(tmp, idx, hpack_idx_to_name(dht, idx)); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 205 | if (!isttest(name)) { |
Willy Tarreau | c775f83 | 2017-12-30 13:17:37 +0100 | [diff] [blame] | 206 | hpack_debug_printf("##ERR@%d##\n", __LINE__); |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 207 | ret = -HPACK_ERR_TOO_LARGE; |
| 208 | goto leave; |
| 209 | } |
| 210 | } |
| 211 | /* <name> and <value> are now set and point to stable values */ |
Willy Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 212 | } |
| 213 | else if (*raw >= 0x20 && *raw <= 0x3f) { |
| 214 | /* max dyn table size change */ |
Willy Tarreau | c775f83 | 2017-12-30 13:17:37 +0100 | [diff] [blame] | 215 | hpack_debug_printf("%02x: p18: dynamic table size update : ", code); |
| 216 | |
Willy Tarreau | c611e66 | 2017-12-03 18:09:21 +0100 | [diff] [blame] | 217 | if (ret) { |
| 218 | /* 7541#4.2.1 : DHT size update must only be at the beginning */ |
Willy Tarreau | c775f83 | 2017-12-30 13:17:37 +0100 | [diff] [blame] | 219 | hpack_debug_printf("##ERR@%d##\n", __LINE__); |
Willy Tarreau | c611e66 | 2017-12-03 18:09:21 +0100 | [diff] [blame] | 220 | ret = -HPACK_ERR_TOO_LARGE; |
| 221 | goto leave; |
| 222 | } |
| 223 | |
Willy Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 224 | idx = get_var_int(&raw, &len, 5); |
| 225 | if (len == (uint32_t)-1) { // truncated |
Willy Tarreau | c775f83 | 2017-12-30 13:17:37 +0100 | [diff] [blame] | 226 | hpack_debug_printf("##ERR@%d##\n", __LINE__); |
Willy Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 227 | ret = -HPACK_ERR_TRUNCATED; |
| 228 | goto leave; |
| 229 | } |
Willy Tarreau | c775f83 | 2017-12-30 13:17:37 +0100 | [diff] [blame] | 230 | hpack_debug_printf(" new len=%u\n", idx); |
Willy Tarreau | 1e7d444 | 2019-01-24 10:47:10 +0100 | [diff] [blame] | 231 | |
| 232 | if (idx > dht->size) { |
| 233 | hpack_debug_printf("##ERR@%d##\n", __LINE__); |
| 234 | ret = -HPACK_ERR_INVALID_ARGUMENT; |
| 235 | goto leave; |
| 236 | } |
Willy Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 237 | continue; |
| 238 | } |
| 239 | else if (!(*raw & (*raw - 0x10))) { |
| 240 | /* 0x00, 0x10, and 0x40 (0x20 and 0x80 were already handled above) */ |
| 241 | |
| 242 | /* literal header field without/never/with incremental indexing -- literal name */ |
| 243 | if (*raw == 0x00) |
| 244 | hpack_debug_printf("%02x: p17: literal without indexing : ", code); |
| 245 | else if (*raw == 0x10) |
| 246 | hpack_debug_printf("%02x: p18: literal never indexed : ", code); |
| 247 | else if (*raw == 0x40) |
| 248 | hpack_debug_printf("%02x: p16: literal with indexing : ", code); |
| 249 | |
| 250 | if (*raw == 0x40) |
| 251 | must_index = 1; |
| 252 | |
| 253 | raw++; len--; |
| 254 | |
| 255 | /* retrieve name */ |
| 256 | if (!len) { // truncated |
Willy Tarreau | c775f83 | 2017-12-30 13:17:37 +0100 | [diff] [blame] | 257 | hpack_debug_printf("##ERR@%d##\n", __LINE__); |
Willy Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 258 | ret = -HPACK_ERR_TRUNCATED; |
| 259 | goto leave; |
| 260 | } |
| 261 | |
| 262 | huff = *raw & 0x80; |
| 263 | nlen = get_var_int(&raw, &len, 7); |
| 264 | if (len == (uint32_t)-1 || len < nlen) { // truncated |
Willy Tarreau | c775f83 | 2017-12-30 13:17:37 +0100 | [diff] [blame] | 265 | hpack_debug_printf("##ERR@%d## (truncated): nlen=%d len=%d\n", |
| 266 | __LINE__, (int)nlen, (int)len); |
Willy Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 267 | ret = -HPACK_ERR_TRUNCATED; |
| 268 | goto leave; |
| 269 | } |
| 270 | |
| 271 | name = ist2(raw, nlen); |
| 272 | |
| 273 | raw += nlen; |
| 274 | len -= nlen; |
Willy Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 275 | |
| 276 | if (huff) { |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 277 | char *ntrash = chunk_newstr(tmp); |
| 278 | if (!ntrash) { |
Willy Tarreau | c775f83 | 2017-12-30 13:17:37 +0100 | [diff] [blame] | 279 | hpack_debug_printf("##ERR@%d##\n", __LINE__); |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 280 | ret = -HPACK_ERR_TOO_LARGE; |
| 281 | goto leave; |
| 282 | } |
| 283 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 284 | nlen = huff_dec((const uint8_t *)name.ptr, name.len, ntrash, |
| 285 | tmp->size - tmp->data); |
Willy Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 286 | if (nlen == (uint32_t)-1) { |
| 287 | hpack_debug_printf("2: can't decode huffman.\n"); |
| 288 | ret = -HPACK_ERR_HUFFMAN; |
| 289 | goto leave; |
| 290 | } |
Willy Tarreau | c775f83 | 2017-12-30 13:17:37 +0100 | [diff] [blame] | 291 | hpack_debug_printf(" [name huff %d->%d] ", (int)name.len, (int)nlen); |
| 292 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 293 | tmp->data += nlen; // make room for the value |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 294 | name = ist2(ntrash, nlen); |
Willy Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | /* retrieve value */ |
| 298 | if (!len) { // truncated |
Willy Tarreau | c775f83 | 2017-12-30 13:17:37 +0100 | [diff] [blame] | 299 | hpack_debug_printf("##ERR@%d##\n", __LINE__); |
Willy Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 300 | ret = -HPACK_ERR_TRUNCATED; |
| 301 | goto leave; |
| 302 | } |
| 303 | |
| 304 | huff = *raw & 0x80; |
| 305 | vlen = get_var_int(&raw, &len, 7); |
| 306 | if (len == (uint32_t)-1 || len < vlen) { // truncated |
Willy Tarreau | c775f83 | 2017-12-30 13:17:37 +0100 | [diff] [blame] | 307 | hpack_debug_printf("##ERR@%d## : vlen=%d len=%d\n", |
| 308 | __LINE__, (int)vlen, (int)len); |
Willy Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 309 | ret = -HPACK_ERR_TRUNCATED; |
| 310 | goto leave; |
| 311 | } |
| 312 | |
| 313 | value = ist2(raw, vlen); |
| 314 | raw += vlen; |
| 315 | len -= vlen; |
| 316 | |
| 317 | if (huff) { |
| 318 | char *vtrash = chunk_newstr(tmp); |
| 319 | if (!vtrash) { |
Willy Tarreau | c775f83 | 2017-12-30 13:17:37 +0100 | [diff] [blame] | 320 | hpack_debug_printf("##ERR@%d##\n", __LINE__); |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 321 | ret = -HPACK_ERR_TOO_LARGE; |
Willy Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 322 | goto leave; |
| 323 | } |
| 324 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 325 | vlen = huff_dec((const uint8_t *)value.ptr, value.len, vtrash, |
| 326 | tmp->size - tmp->data); |
Willy Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 327 | if (vlen == (uint32_t)-1) { |
| 328 | hpack_debug_printf("3: can't decode huffman.\n"); |
| 329 | ret = -HPACK_ERR_HUFFMAN; |
| 330 | goto leave; |
| 331 | } |
Willy Tarreau | c775f83 | 2017-12-30 13:17:37 +0100 | [diff] [blame] | 332 | hpack_debug_printf(" [value huff %d->%d] ", (int)value.len, (int)vlen); |
| 333 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 334 | tmp->data += vlen; // make room for the value |
Willy Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 335 | value = ist2(vtrash, vlen); |
| 336 | } |
| 337 | |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 338 | /* <name> and <value> are correctly filled here */ |
Willy Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 339 | } |
| 340 | else { |
| 341 | /* 0x01..0x0f : literal header field without indexing -- indexed name */ |
| 342 | /* 0x11..0x1f : literal header field never indexed -- indexed name */ |
| 343 | /* 0x41..0x7f : literal header field with incremental indexing -- indexed name */ |
| 344 | |
| 345 | if (*raw <= 0x0f) |
| 346 | hpack_debug_printf("%02x: p16: literal without indexing -- indexed name : ", code); |
| 347 | else if (*raw >= 0x41) |
| 348 | hpack_debug_printf("%02x: p15: literal with indexing -- indexed name : ", code); |
| 349 | else |
| 350 | hpack_debug_printf("%02x: p16: literal never indexed -- indexed name : ", code); |
| 351 | |
| 352 | /* retrieve name index */ |
| 353 | if (*raw >= 0x41) { |
| 354 | must_index = 1; |
| 355 | idx = get_var_int(&raw, &len, 6); |
| 356 | } |
| 357 | else |
| 358 | idx = get_var_int(&raw, &len, 4); |
| 359 | |
Willy Tarreau | c775f83 | 2017-12-30 13:17:37 +0100 | [diff] [blame] | 360 | hpack_debug_printf(" idx=%u ", idx); |
| 361 | |
Willy Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 362 | if (len == (uint32_t)-1 || !len) { // truncated |
Willy Tarreau | c775f83 | 2017-12-30 13:17:37 +0100 | [diff] [blame] | 363 | hpack_debug_printf("##ERR@%d##\n", __LINE__); |
Willy Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 364 | ret = -HPACK_ERR_TRUNCATED; |
| 365 | goto leave; |
| 366 | } |
| 367 | |
Willy Tarreau | d85ba4e | 2017-12-03 12:12:17 +0100 | [diff] [blame] | 368 | if (!hpack_valid_idx(dht, idx)) { |
Willy Tarreau | c775f83 | 2017-12-30 13:17:37 +0100 | [diff] [blame] | 369 | hpack_debug_printf("##ERR@%d##\n", __LINE__); |
Willy Tarreau | d85ba4e | 2017-12-03 12:12:17 +0100 | [diff] [blame] | 370 | ret = -HPACK_ERR_TOO_LARGE; |
| 371 | goto leave; |
| 372 | } |
| 373 | |
Willy Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 374 | /* retrieve value */ |
| 375 | huff = *raw & 0x80; |
| 376 | vlen = get_var_int(&raw, &len, 7); |
| 377 | if (len == (uint32_t)-1 || len < vlen) { // truncated |
Willy Tarreau | c775f83 | 2017-12-30 13:17:37 +0100 | [diff] [blame] | 378 | hpack_debug_printf("##ERR@%d##\n", __LINE__); |
Willy Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 379 | ret = -HPACK_ERR_TRUNCATED; |
| 380 | goto leave; |
| 381 | } |
| 382 | |
| 383 | value = ist2(raw, vlen); |
| 384 | raw += vlen; |
| 385 | len -= vlen; |
| 386 | |
| 387 | if (huff) { |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 388 | char *vtrash = chunk_newstr(tmp); |
| 389 | if (!vtrash) { |
Willy Tarreau | c775f83 | 2017-12-30 13:17:37 +0100 | [diff] [blame] | 390 | hpack_debug_printf("##ERR@%d##\n", __LINE__); |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 391 | ret = -HPACK_ERR_TOO_LARGE; |
| 392 | goto leave; |
| 393 | } |
| 394 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 395 | vlen = huff_dec((const uint8_t *)value.ptr, value.len, vtrash, |
| 396 | tmp->size - tmp->data); |
Willy Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 397 | if (vlen == (uint32_t)-1) { |
Willy Tarreau | c775f83 | 2017-12-30 13:17:37 +0100 | [diff] [blame] | 398 | hpack_debug_printf("##ERR@%d## can't decode huffman : ilen=%d osize=%d\n", |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 399 | __LINE__, (int)value.len, |
| 400 | (int)(tmp->size - tmp->data)); |
Willy Tarreau | c775f83 | 2017-12-30 13:17:37 +0100 | [diff] [blame] | 401 | hpack_debug_hexdump(stderr, "[HUFFMAN] ", value.ptr, 0, value.len); |
Willy Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 402 | ret = -HPACK_ERR_HUFFMAN; |
| 403 | goto leave; |
| 404 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 405 | tmp->data += vlen; // make room for the value |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 406 | value = ist2(vtrash, vlen); |
Willy Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 407 | } |
| 408 | |
Tim Duesterhus | 241e29e | 2020-03-05 17:56:30 +0100 | [diff] [blame] | 409 | name = IST_NULL; |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 410 | if (!must_index) |
| 411 | name.len = hpack_idx_to_phdr(idx); |
Willy Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 412 | |
Willy Tarreau | bb39b49 | 2017-12-30 16:56:28 +0100 | [diff] [blame] | 413 | if (!name.len) { |
| 414 | name = hpack_alloc_string(tmp, idx, hpack_idx_to_name(dht, idx)); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 415 | if (!isttest(name)) { |
Willy Tarreau | c775f83 | 2017-12-30 13:17:37 +0100 | [diff] [blame] | 416 | hpack_debug_printf("##ERR@%d##\n", __LINE__); |
Willy Tarreau | bb39b49 | 2017-12-30 16:56:28 +0100 | [diff] [blame] | 417 | ret = -HPACK_ERR_TOO_LARGE; |
| 418 | goto leave; |
| 419 | } |
| 420 | } |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 421 | /* <name> and <value> are correctly filled here */ |
Willy Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 422 | } |
Willy Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 423 | |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 424 | /* here's what we have here : |
| 425 | * - name.len > 0 |
| 426 | * - value is filled with either const data or data allocated from tmp |
| 427 | * - name.ptr == NULL && !must_index : known pseudo-header #name.len |
| 428 | * - name.ptr != NULL || must_index : general header, unknown pseudo-header or index needed |
| 429 | */ |
| 430 | if (ret >= list_size) { |
Willy Tarreau | c775f83 | 2017-12-30 13:17:37 +0100 | [diff] [blame] | 431 | hpack_debug_printf("##ERR@%d##\n", __LINE__); |
Willy Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 432 | ret = -HPACK_ERR_TOO_LARGE; |
| 433 | goto leave; |
| 434 | } |
| 435 | |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 436 | list[ret].n = name; |
| 437 | list[ret].v = value; |
| 438 | ret++; |
Willy Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 439 | |
| 440 | if (must_index && hpack_dht_insert(dht, name, value) < 0) { |
| 441 | hpack_debug_printf("failed to find some room in the dynamic table\n"); |
| 442 | ret = -HPACK_ERR_DHT_INSERT_FAIL; |
| 443 | goto leave; |
| 444 | } |
| 445 | |
Willy Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 446 | hpack_debug_printf("\e[1;34m%s\e[0m: ", |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 447 | isttest(name) ? istpad(trash.area, name).ptr : h2_phdr_to_str(name.len)); |
Willy Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 448 | |
Willy Tarreau | c775f83 | 2017-12-30 13:17:37 +0100 | [diff] [blame] | 449 | hpack_debug_printf("\e[1;35m%s\e[0m [mustidx=%d, used=%d] [n=(%p,%d) v=(%p,%d)]\n", |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 450 | istpad(trash.area, value).ptr, must_index, |
| 451 | dht->used, |
Willy Tarreau | c775f83 | 2017-12-30 13:17:37 +0100 | [diff] [blame] | 452 | name.ptr, (int)name.len, value.ptr, (int)value.len); |
Willy Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 453 | } |
| 454 | |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 455 | if (ret >= list_size) { |
Willy Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 456 | ret = -HPACK_ERR_TOO_LARGE; |
| 457 | goto leave; |
| 458 | } |
| 459 | |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 460 | /* put an end marker */ |
Tim Duesterhus | 241e29e | 2020-03-05 17:56:30 +0100 | [diff] [blame] | 461 | list[ret].n = list[ret].v = IST_NULL; |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 462 | ret++; |
Willy Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 463 | |
Willy Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 464 | leave: |
Willy Tarreau | c775f83 | 2017-12-30 13:17:37 +0100 | [diff] [blame] | 465 | hpack_debug_printf("-- done: ret=%d list_size=%d --\n", (int)ret, (int)list_size); |
Willy Tarreau | 679790b | 2017-05-30 19:09:44 +0200 | [diff] [blame] | 466 | return ret; |
| 467 | } |