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