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