Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 1 | /* |
| 2 | * QPACK decompressor |
| 3 | * |
Willy Tarreau | 3dfb7da | 2022-03-02 22:33:39 +0100 | [diff] [blame] | 4 | * Copyright 2021 HAProxy Technologies, Frederic Lecaille <flecaille@haproxy.com> |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation, version 2.1 |
| 9 | * exclusively. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | */ |
| 20 | |
| 21 | #include <inttypes.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | |
| 26 | #include <import/ist.h> |
| 27 | #include <haproxy/buf.h> |
| 28 | #include <haproxy/chunk.h> |
| 29 | #include <haproxy/h3.h> |
| 30 | #include <haproxy/qpack-t.h> |
| 31 | #include <haproxy/qpack-dec.h> |
Amaury Denoyelle | fd7cdc3 | 2021-08-24 15:13:20 +0200 | [diff] [blame] | 32 | #include <haproxy/qpack-tbl.h> |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 33 | #include <haproxy/hpack-huff.h> |
| 34 | #include <haproxy/hpack-tbl.h> |
Amaury Denoyelle | fd7cdc3 | 2021-08-24 15:13:20 +0200 | [diff] [blame] | 35 | #include <haproxy/http-hdr.h> |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 36 | #include <haproxy/tools.h> |
| 37 | |
Amaury Denoyelle | d96361b | 2022-03-25 14:56:51 +0100 | [diff] [blame] | 38 | #if defined(DEBUG_QPACK) |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 39 | #define qpack_debug_printf fprintf |
| 40 | #define qpack_debug_hexdump debug_hexdump |
| 41 | #else |
| 42 | #define qpack_debug_printf(...) do { } while (0) |
| 43 | #define qpack_debug_hexdump(...) do { } while (0) |
| 44 | #endif |
| 45 | |
| 46 | /* Encoded field line bitmask */ |
| 47 | #define QPACK_EFL_BITMASK 0xf0 |
| 48 | #define QPACK_LFL_WPBNM 0x00 // Literal field line with post-base name reference |
| 49 | #define QPACK_IFL_WPBI 0x10 // Indexed field line with post-based index |
| 50 | #define QPACK_LFL_WLN_BIT 0x20 // Literal field line with literal name |
| 51 | #define QPACK_LFL_WNR_BIT 0x40 // Literal field line with name reference |
| 52 | #define QPACK_IFL_BIT 0x80 // Indexed field line |
| 53 | |
| 54 | /* reads a varint from <raw>'s lowest <b> bits and <len> bytes max (raw included). |
| 55 | * returns the 64-bit value on success after updating buf and len_in. Forces |
| 56 | * len_in to (uint64_t)-1 on truncated input. |
| 57 | * Note that this function is similar to the one used for HPACK (except that is supports |
| 58 | * up to 62-bits integers). |
| 59 | */ |
| 60 | static uint64_t qpack_get_varint(const unsigned char **buf, uint64_t *len_in, int b) |
| 61 | { |
| 62 | uint64_t ret = 0; |
| 63 | int len = *len_in; |
| 64 | const uint8_t *raw = *buf; |
| 65 | uint8_t shift = 0; |
| 66 | |
| 67 | len--; |
Frédéric Lécaille | 6842485 | 2022-02-02 14:56:23 +0100 | [diff] [blame] | 68 | ret = *raw++ & ((1ULL << b) - 1); |
| 69 | if (ret != (uint64_t)((1ULL << b) - 1)) |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 70 | goto end; |
| 71 | |
| 72 | while (len && (*raw & 128)) { |
| 73 | ret += ((uint64_t)*raw++ & 127) << shift; |
| 74 | shift += 7; |
| 75 | len--; |
| 76 | } |
| 77 | |
| 78 | /* last 7 bits */ |
| 79 | if (!len) |
| 80 | goto too_short; |
| 81 | |
| 82 | len--; |
| 83 | ret += ((uint64_t)*raw++ & 127) << shift; |
| 84 | |
| 85 | end: |
| 86 | *buf = raw; |
| 87 | *len_in = len; |
| 88 | return ret; |
| 89 | |
| 90 | too_short: |
| 91 | *len_in = (uint64_t)-1; |
| 92 | return 0; |
| 93 | } |
| 94 | |
Amaury Denoyelle | 5869cb6 | 2022-05-31 15:21:27 +0200 | [diff] [blame] | 95 | /* Decode an encoder stream. |
| 96 | * |
| 97 | * Returns 0 on success else non-zero. |
| 98 | */ |
Amaury Denoyelle | 53eef46 | 2022-06-14 16:34:32 +0200 | [diff] [blame] | 99 | int qpack_decode_enc(struct buffer *buf, void *ctx) |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 100 | { |
| 101 | size_t len; |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 102 | unsigned char inst; |
| 103 | |
Amaury Denoyelle | 53eef46 | 2022-06-14 16:34:32 +0200 | [diff] [blame] | 104 | len = b_data(buf); |
| 105 | qpack_debug_hexdump(stderr, "[QPACK-DEC-ENC] ", b_head(buf), 0, len); |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 106 | |
| 107 | if (!len) { |
| 108 | qpack_debug_printf(stderr, "[QPACK-DEC-ENC] empty stream\n"); |
| 109 | return 0; |
| 110 | } |
| 111 | |
Amaury Denoyelle | 53eef46 | 2022-06-14 16:34:32 +0200 | [diff] [blame] | 112 | inst = (unsigned char)*b_head(buf) & QPACK_ENC_INST_BITMASK; |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 113 | if (inst == QPACK_ENC_INST_DUP) { |
| 114 | /* Duplicate */ |
| 115 | } |
| 116 | else if (inst & QPACK_ENC_INST_IWNR_BIT) { |
| 117 | /* Insert With Name Reference */ |
| 118 | } |
| 119 | else if (inst & QPACK_ENC_INST_IWLN_BIT) { |
| 120 | /* Insert with literal name */ |
| 121 | } |
| 122 | else if (inst & QPACK_ENC_INST_SDTC_BIT) { |
| 123 | /* Set dynamic table capacity */ |
| 124 | } |
| 125 | |
Amaury Denoyelle | 5869cb6 | 2022-05-31 15:21:27 +0200 | [diff] [blame] | 126 | return 0; |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 127 | } |
| 128 | |
Amaury Denoyelle | 5869cb6 | 2022-05-31 15:21:27 +0200 | [diff] [blame] | 129 | /* Decode an decoder stream. |
| 130 | * |
| 131 | * Returns 0 on success else non-zero. |
| 132 | */ |
Amaury Denoyelle | 53eef46 | 2022-06-14 16:34:32 +0200 | [diff] [blame] | 133 | int qpack_decode_dec(struct buffer *buf, void *ctx) |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 134 | { |
| 135 | size_t len; |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 136 | unsigned char inst; |
| 137 | |
Amaury Denoyelle | 53eef46 | 2022-06-14 16:34:32 +0200 | [diff] [blame] | 138 | len = b_data(buf); |
| 139 | qpack_debug_hexdump(stderr, "[QPACK-DEC-DEC] ", b_head(buf), 0, len); |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 140 | |
| 141 | if (!len) { |
| 142 | qpack_debug_printf(stderr, "[QPACK-DEC-DEC] empty stream\n"); |
| 143 | return 0; |
| 144 | } |
| 145 | |
Amaury Denoyelle | 53eef46 | 2022-06-14 16:34:32 +0200 | [diff] [blame] | 146 | inst = (unsigned char)*b_head(buf) & QPACK_DEC_INST_BITMASK; |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 147 | if (inst == QPACK_DEC_INST_ICINC) { |
| 148 | /* Insert count increment */ |
| 149 | } |
| 150 | else if (inst & QPACK_DEC_INST_SACK) { |
| 151 | /* Section Acknowledgment */ |
| 152 | } |
| 153 | else if (inst & QPACK_DEC_INST_SCCL) { |
| 154 | /* Stream cancellation */ |
| 155 | } |
| 156 | |
Amaury Denoyelle | 5869cb6 | 2022-05-31 15:21:27 +0200 | [diff] [blame] | 157 | return 0; |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | /* Decode a field section prefix made of <enc_ric> and <db> two varints. |
| 161 | * Also set the 'S' sign bit for <db>. |
| 162 | * Return a negative error if failed, 0 if not. |
| 163 | */ |
| 164 | static int qpack_decode_fs_pfx(uint64_t *enc_ric, uint64_t *db, int *sign_bit, |
Frédéric Lécaille | 628e89c | 2022-06-24 12:13:53 +0200 | [diff] [blame] | 165 | const unsigned char **raw, uint64_t *len) |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 166 | { |
| 167 | *enc_ric = qpack_get_varint(raw, len, 8); |
| 168 | if (*len == (uint64_t)-1) |
| 169 | return -QPACK_ERR_RIC; |
| 170 | |
| 171 | *sign_bit = **raw & 0x8; |
| 172 | *db = qpack_get_varint(raw, len, 7); |
| 173 | if (*len == (uint64_t)-1) |
| 174 | return -QPACK_ERR_DB; |
| 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
Amaury Denoyelle | c5d31ed | 2022-06-14 17:34:53 +0200 | [diff] [blame] | 179 | /* Decode a field section from the <raw> buffer of <len> bytes. Each parsed |
Amaury Denoyelle | 60ef19f | 2022-06-14 17:38:36 +0200 | [diff] [blame] | 180 | * header is inserted into <list> of <list_size> entries max and uses <tmp> as |
| 181 | * a storage for some elements pointing into it. An end marker is inserted at |
| 182 | * the end of the list with empty strings as name/value. |
Amaury Denoyelle | c5d31ed | 2022-06-14 17:34:53 +0200 | [diff] [blame] | 183 | * |
Amaury Denoyelle | b666c6b | 2022-06-14 17:17:07 +0200 | [diff] [blame] | 184 | * Returns the number of headers inserted into list excluding the end marker. |
| 185 | * In case of error, a negative code QPACK_ERR_* is returned. |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 186 | */ |
Frédéric Lécaille | 628e89c | 2022-06-24 12:13:53 +0200 | [diff] [blame] | 187 | int qpack_decode_fs(const unsigned char *raw, uint64_t len, struct buffer *tmp, |
Amaury Denoyelle | 60ef19f | 2022-06-14 17:38:36 +0200 | [diff] [blame] | 188 | struct http_hdr *list, int list_size) |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 189 | { |
Amaury Denoyelle | b666c6b | 2022-06-14 17:17:07 +0200 | [diff] [blame] | 190 | struct ist name, value; |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 191 | uint64_t enc_ric, db; |
| 192 | int s; |
| 193 | unsigned int efl_type; |
| 194 | int ret; |
Amaury Denoyelle | fd7cdc3 | 2021-08-24 15:13:20 +0200 | [diff] [blame] | 195 | int hdr_idx = 0; |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 196 | |
| 197 | qpack_debug_hexdump(stderr, "[QPACK-DEC-FS] ", (const char *)raw, 0, len); |
| 198 | |
Amaury Denoyelle | c5d31ed | 2022-06-14 17:34:53 +0200 | [diff] [blame] | 199 | /* parse field section prefix */ |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 200 | ret = qpack_decode_fs_pfx(&enc_ric, &db, &s, &raw, &len); |
| 201 | if (ret < 0) { |
| 202 | qpack_debug_printf(stderr, "##ERR@%d(%d)\n", __LINE__, ret); |
| 203 | goto out; |
| 204 | } |
| 205 | |
| 206 | chunk_reset(tmp); |
| 207 | qpack_debug_printf(stderr, "enc_ric: %llu db: %llu s=%d\n", |
| 208 | (unsigned long long)enc_ric, (unsigned long long)db, !!s); |
| 209 | /* Decode field lines */ |
| 210 | while (len) { |
Amaury Denoyelle | 60ef19f | 2022-06-14 17:38:36 +0200 | [diff] [blame] | 211 | if (hdr_idx >= list_size) { |
| 212 | qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__); |
| 213 | ret = -QPACK_ERR_TOO_LARGE; |
| 214 | goto out; |
| 215 | } |
| 216 | |
Amaury Denoyelle | c5d31ed | 2022-06-14 17:34:53 +0200 | [diff] [blame] | 217 | /* parse field line representation */ |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 218 | efl_type = *raw & QPACK_EFL_BITMASK; |
| 219 | qpack_debug_printf(stderr, "efl_type=0x%02x\n", efl_type); |
Amaury Denoyelle | c5d31ed | 2022-06-14 17:34:53 +0200 | [diff] [blame] | 220 | |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 221 | if (efl_type == QPACK_LFL_WPBNM) { |
Amaury Denoyelle | 28d3c24 | 2022-06-14 16:36:15 +0200 | [diff] [blame] | 222 | /* Literal field line with post-base name reference |
Amaury Denoyelle | a7a4c80 | 2022-06-30 09:30:23 +0200 | [diff] [blame] | 223 | * TODO adjust this when dynamic table support is implemented. |
Amaury Denoyelle | 28d3c24 | 2022-06-14 16:36:15 +0200 | [diff] [blame] | 224 | */ |
| 225 | #if 0 |
Amaury Denoyelle | 18a10d0 | 2022-03-25 15:11:38 +0100 | [diff] [blame] | 226 | uint64_t index __maybe_unused, length; |
Amaury Denoyelle | d96361b | 2022-03-25 14:56:51 +0100 | [diff] [blame] | 227 | unsigned int n __maybe_unused, h __maybe_unused; |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 228 | |
| 229 | qpack_debug_printf(stderr, "literal field line with post-base name reference:"); |
| 230 | n = *raw & 0x08; |
| 231 | index = qpack_get_varint(&raw, &len, 3); |
| 232 | if (len == (uint64_t)-1) { |
| 233 | qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__); |
| 234 | ret = -QPACK_ERR_TRUNCATED; |
| 235 | goto out; |
| 236 | } |
| 237 | |
| 238 | qpack_debug_printf(stderr, " n=%d index=%llu", !!n, (unsigned long long)index); |
| 239 | h = *raw & 0x80; |
| 240 | length = qpack_get_varint(&raw, &len, 7); |
| 241 | if (len == (uint64_t)-1) { |
| 242 | qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__); |
| 243 | ret = -QPACK_ERR_TRUNCATED; |
| 244 | goto out; |
| 245 | } |
| 246 | |
| 247 | qpack_debug_printf(stderr, " h=%d length=%llu", !!h, (unsigned long long)length); |
Frédéric Lécaille | e629cfd | 2021-12-15 14:16:16 +0100 | [diff] [blame] | 248 | |
| 249 | if (len < length) { |
| 250 | qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__); |
| 251 | ret = -QPACK_ERR_TRUNCATED; |
| 252 | goto out; |
| 253 | } |
| 254 | |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 255 | raw += length; |
| 256 | len -= length; |
Amaury Denoyelle | 28d3c24 | 2022-06-14 16:36:15 +0200 | [diff] [blame] | 257 | #endif |
Amaury Denoyelle | a7a4c80 | 2022-06-30 09:30:23 +0200 | [diff] [blame] | 258 | |
| 259 | /* RFC9204 2.2.3 Invalid References |
| 260 | * |
| 261 | * If the decoder encounters a reference in a field line representation |
| 262 | * to a dynamic table entry that has already been evicted or that has an |
| 263 | * absolute index greater than or equal to the declared Required Insert |
| 264 | * Count (Section 4.5.1), it MUST treat this as a connection error of |
| 265 | * type QPACK_DECOMPRESSION_FAILED. |
| 266 | */ |
| 267 | return -QPACK_DECOMPRESSION_FAILED; |
Amaury Denoyelle | fd7cdc3 | 2021-08-24 15:13:20 +0200 | [diff] [blame] | 268 | } |
| 269 | else if (efl_type == QPACK_IFL_WPBI) { |
Amaury Denoyelle | 28d3c24 | 2022-06-14 16:36:15 +0200 | [diff] [blame] | 270 | /* Indexed field line with post-base index |
Amaury Denoyelle | a7a4c80 | 2022-06-30 09:30:23 +0200 | [diff] [blame] | 271 | * TODO adjust this when dynamic table support is implemented. |
Amaury Denoyelle | 28d3c24 | 2022-06-14 16:36:15 +0200 | [diff] [blame] | 272 | */ |
| 273 | #if 0 |
Amaury Denoyelle | 18a10d0 | 2022-03-25 15:11:38 +0100 | [diff] [blame] | 274 | uint64_t index __maybe_unused; |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 275 | |
| 276 | qpack_debug_printf(stderr, "indexed field line with post-base index:"); |
| 277 | index = qpack_get_varint(&raw, &len, 4); |
| 278 | if (len == (uint64_t)-1) { |
| 279 | qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__); |
| 280 | ret = -QPACK_ERR_TRUNCATED; |
| 281 | goto out; |
| 282 | } |
| 283 | |
| 284 | qpack_debug_printf(stderr, " index=%llu", (unsigned long long)index); |
Amaury Denoyelle | 28d3c24 | 2022-06-14 16:36:15 +0200 | [diff] [blame] | 285 | #endif |
Amaury Denoyelle | a7a4c80 | 2022-06-30 09:30:23 +0200 | [diff] [blame] | 286 | |
| 287 | /* RFC9204 2.2.3 Invalid References |
| 288 | * |
| 289 | * If the decoder encounters a reference in a field line representation |
| 290 | * to a dynamic table entry that has already been evicted or that has an |
| 291 | * absolute index greater than or equal to the declared Required Insert |
| 292 | * Count (Section 4.5.1), it MUST treat this as a connection error of |
| 293 | * type QPACK_DECOMPRESSION_FAILED. |
| 294 | */ |
| 295 | return -QPACK_DECOMPRESSION_FAILED; |
Amaury Denoyelle | fd7cdc3 | 2021-08-24 15:13:20 +0200 | [diff] [blame] | 296 | } |
| 297 | else if (efl_type & QPACK_IFL_BIT) { |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 298 | /* Indexed field line */ |
| 299 | uint64_t index; |
Amaury Denoyelle | debaa04 | 2022-06-20 15:47:46 +0200 | [diff] [blame] | 300 | unsigned int static_tbl; |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 301 | |
| 302 | qpack_debug_printf(stderr, "indexed field line:"); |
Amaury Denoyelle | debaa04 | 2022-06-20 15:47:46 +0200 | [diff] [blame] | 303 | static_tbl = efl_type & 0x40; |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 304 | index = qpack_get_varint(&raw, &len, 6); |
| 305 | if (len == (uint64_t)-1) { |
| 306 | qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__); |
| 307 | ret = -QPACK_ERR_TRUNCATED; |
| 308 | goto out; |
| 309 | } |
| 310 | |
Amaury Denoyelle | debaa04 | 2022-06-20 15:47:46 +0200 | [diff] [blame] | 311 | if (static_tbl) { |
Amaury Denoyelle | b666c6b | 2022-06-14 17:17:07 +0200 | [diff] [blame] | 312 | name = qpack_sht[index].n; |
| 313 | value = qpack_sht[index].v; |
| 314 | } |
Amaury Denoyelle | debaa04 | 2022-06-20 15:47:46 +0200 | [diff] [blame] | 315 | else { |
Amaury Denoyelle | a7a4c80 | 2022-06-30 09:30:23 +0200 | [diff] [blame] | 316 | /* RFC9204 2.2.3 Invalid References |
Amaury Denoyelle | debaa04 | 2022-06-20 15:47:46 +0200 | [diff] [blame] | 317 | * |
Amaury Denoyelle | a7a4c80 | 2022-06-30 09:30:23 +0200 | [diff] [blame] | 318 | * If the decoder encounters a reference in a field line representation |
| 319 | * to a dynamic table entry that has already been evicted or that has an |
| 320 | * absolute index greater than or equal to the declared Required Insert |
| 321 | * Count (Section 4.5.1), it MUST treat this as a connection error of |
| 322 | * type QPACK_DECOMPRESSION_FAILED. |
| 323 | * |
| 324 | * TODO adjust this when dynamic table support is implemented. |
Amaury Denoyelle | debaa04 | 2022-06-20 15:47:46 +0200 | [diff] [blame] | 325 | */ |
Amaury Denoyelle | a7a4c80 | 2022-06-30 09:30:23 +0200 | [diff] [blame] | 326 | return -QPACK_DECOMPRESSION_FAILED; |
Amaury Denoyelle | debaa04 | 2022-06-20 15:47:46 +0200 | [diff] [blame] | 327 | } |
Amaury Denoyelle | fd7cdc3 | 2021-08-24 15:13:20 +0200 | [diff] [blame] | 328 | |
Amaury Denoyelle | 055de23 | 2022-06-30 09:28:50 +0200 | [diff] [blame] | 329 | qpack_debug_printf(stderr, " t=%d index=%llu", !!static_tbl, (unsigned long long)index); |
Amaury Denoyelle | fd7cdc3 | 2021-08-24 15:13:20 +0200 | [diff] [blame] | 330 | } |
| 331 | else if (efl_type & QPACK_LFL_WNR_BIT) { |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 332 | /* Literal field line with name reference */ |
| 333 | uint64_t index, length; |
Amaury Denoyelle | 46e992d | 2022-06-30 09:31:24 +0200 | [diff] [blame] | 334 | unsigned int static_tbl, n __maybe_unused, h; |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 335 | |
| 336 | qpack_debug_printf(stderr, "Literal field line with name reference:"); |
| 337 | n = efl_type & 0x20; |
Amaury Denoyelle | 46e992d | 2022-06-30 09:31:24 +0200 | [diff] [blame] | 338 | static_tbl = efl_type & 0x10; |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 339 | index = qpack_get_varint(&raw, &len, 4); |
| 340 | if (len == (uint64_t)-1) { |
| 341 | qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__); |
| 342 | ret = -QPACK_ERR_TRUNCATED; |
| 343 | goto out; |
| 344 | } |
| 345 | |
Amaury Denoyelle | 46e992d | 2022-06-30 09:31:24 +0200 | [diff] [blame] | 346 | if (static_tbl) { |
Amaury Denoyelle | b666c6b | 2022-06-14 17:17:07 +0200 | [diff] [blame] | 347 | name = qpack_sht[index].n; |
Amaury Denoyelle | 46e992d | 2022-06-30 09:31:24 +0200 | [diff] [blame] | 348 | } |
| 349 | else { |
Amaury Denoyelle | a7a4c80 | 2022-06-30 09:30:23 +0200 | [diff] [blame] | 350 | /* RFC9204 2.2.3 Invalid References |
Amaury Denoyelle | 46e992d | 2022-06-30 09:31:24 +0200 | [diff] [blame] | 351 | * |
Amaury Denoyelle | a7a4c80 | 2022-06-30 09:30:23 +0200 | [diff] [blame] | 352 | * If the decoder encounters a reference in a field line representation |
| 353 | * to a dynamic table entry that has already been evicted or that has an |
| 354 | * absolute index greater than or equal to the declared Required Insert |
| 355 | * Count (Section 4.5.1), it MUST treat this as a connection error of |
| 356 | * type QPACK_DECOMPRESSION_FAILED. |
| 357 | * |
| 358 | * TODO adjust this when dynamic table support is implemented. |
Amaury Denoyelle | 46e992d | 2022-06-30 09:31:24 +0200 | [diff] [blame] | 359 | */ |
Amaury Denoyelle | a7a4c80 | 2022-06-30 09:30:23 +0200 | [diff] [blame] | 360 | return -QPACK_DECOMPRESSION_FAILED; |
Amaury Denoyelle | 46e992d | 2022-06-30 09:31:24 +0200 | [diff] [blame] | 361 | } |
Amaury Denoyelle | fd7cdc3 | 2021-08-24 15:13:20 +0200 | [diff] [blame] | 362 | |
Amaury Denoyelle | 46e992d | 2022-06-30 09:31:24 +0200 | [diff] [blame] | 363 | qpack_debug_printf(stderr, " n=%d t=%d index=%llu", !!n, !!static_tbl, (unsigned long long)index); |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 364 | h = *raw & 0x80; |
| 365 | length = qpack_get_varint(&raw, &len, 7); |
| 366 | if (len == (uint64_t)-1) { |
| 367 | qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__); |
| 368 | ret = -QPACK_ERR_TRUNCATED; |
| 369 | goto out; |
| 370 | } |
| 371 | |
| 372 | qpack_debug_printf(stderr, " h=%d length=%llu", !!h, (unsigned long long)length); |
| 373 | if (h) { |
| 374 | char *trash; |
| 375 | int nlen; |
| 376 | |
| 377 | trash = chunk_newstr(tmp); |
| 378 | if (!trash) { |
| 379 | qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__); |
| 380 | ret = -QPACK_DECOMPRESSION_FAILED; |
| 381 | goto out; |
| 382 | } |
| 383 | nlen = huff_dec(raw, length, trash, tmp->size - tmp->data); |
| 384 | if (nlen == (uint32_t)-1) { |
| 385 | qpack_debug_printf(stderr, " can't decode huffman.\n"); |
| 386 | ret = -QPACK_ERR_HUFFMAN; |
| 387 | goto out; |
| 388 | } |
| 389 | |
| 390 | qpack_debug_printf(stderr, " [name huff %d->%d '%s']", (int)length, (int)nlen, trash); |
Amaury Denoyelle | 9c8c4fa | 2021-09-30 17:14:55 +0200 | [diff] [blame] | 391 | /* makes an ist from tmp storage */ |
| 392 | b_add(tmp, nlen); |
Amaury Denoyelle | b666c6b | 2022-06-14 17:17:07 +0200 | [diff] [blame] | 393 | value = ist2(trash, nlen); |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 394 | } |
Amaury Denoyelle | 7d3aea5 | 2021-11-24 16:04:03 +0100 | [diff] [blame] | 395 | else { |
Amaury Denoyelle | b666c6b | 2022-06-14 17:17:07 +0200 | [diff] [blame] | 396 | value = ist2(raw, length); |
Amaury Denoyelle | 7d3aea5 | 2021-11-24 16:04:03 +0100 | [diff] [blame] | 397 | } |
| 398 | |
Frédéric Lécaille | e629cfd | 2021-12-15 14:16:16 +0100 | [diff] [blame] | 399 | if (len < length) { |
| 400 | qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__); |
| 401 | ret = -QPACK_ERR_TRUNCATED; |
| 402 | goto out; |
| 403 | } |
| 404 | |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 405 | raw += length; |
| 406 | len -= length; |
Amaury Denoyelle | fd7cdc3 | 2021-08-24 15:13:20 +0200 | [diff] [blame] | 407 | } |
| 408 | else if (efl_type & QPACK_LFL_WLN_BIT) { |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 409 | /* Literal field line with literal name */ |
Amaury Denoyelle | 4bcaf69 | 2022-06-14 16:34:55 +0200 | [diff] [blame] | 410 | unsigned int n __maybe_unused, hname, hvalue; |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 411 | uint64_t name_len, value_len; |
| 412 | |
| 413 | qpack_debug_printf(stderr, "Literal field line with literal name:"); |
| 414 | n = *raw & 0x10; |
| 415 | hname = *raw & 0x08; |
| 416 | name_len = qpack_get_varint(&raw, &len, 3); |
| 417 | if (len == (uint64_t)-1) { |
| 418 | qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__); |
| 419 | ret = -QPACK_ERR_TRUNCATED; |
| 420 | goto out; |
| 421 | } |
| 422 | |
Amaury Denoyelle | ab9cec7 | 2022-02-14 14:45:10 +0100 | [diff] [blame] | 423 | qpack_debug_printf(stderr, " n=%d hname=%d name_len=%llu", !!n, !!hname, (unsigned long long)name_len); |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 424 | /* Name string */ |
Frédéric Lécaille | e629cfd | 2021-12-15 14:16:16 +0100 | [diff] [blame] | 425 | |
| 426 | if (len < name_len) { |
| 427 | qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__); |
| 428 | ret = -QPACK_ERR_TRUNCATED; |
| 429 | goto out; |
| 430 | } |
| 431 | |
Amaury Denoyelle | 4bcaf69 | 2022-06-14 16:34:55 +0200 | [diff] [blame] | 432 | if (hname) { |
| 433 | char *trash; |
| 434 | int nlen; |
| 435 | |
| 436 | trash = chunk_newstr(tmp); |
| 437 | if (!trash) { |
| 438 | qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__); |
| 439 | ret = -QPACK_DECOMPRESSION_FAILED; |
| 440 | goto out; |
| 441 | } |
| 442 | nlen = huff_dec(raw, name_len, trash, tmp->size - tmp->data); |
| 443 | if (nlen == (uint32_t)-1) { |
| 444 | qpack_debug_printf(stderr, " can't decode huffman.\n"); |
| 445 | ret = -QPACK_ERR_HUFFMAN; |
| 446 | goto out; |
| 447 | } |
| 448 | |
| 449 | qpack_debug_printf(stderr, " [name huff %d->%d '%s']", (int)name_len, (int)nlen, trash); |
| 450 | /* makes an ist from tmp storage */ |
| 451 | b_add(tmp, nlen); |
Amaury Denoyelle | b666c6b | 2022-06-14 17:17:07 +0200 | [diff] [blame] | 452 | name = ist2(trash, nlen); |
Amaury Denoyelle | 4bcaf69 | 2022-06-14 16:34:55 +0200 | [diff] [blame] | 453 | } |
| 454 | else { |
Amaury Denoyelle | b666c6b | 2022-06-14 17:17:07 +0200 | [diff] [blame] | 455 | name = ist2(raw, name_len); |
Amaury Denoyelle | 4bcaf69 | 2022-06-14 16:34:55 +0200 | [diff] [blame] | 456 | } |
| 457 | |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 458 | raw += name_len; |
| 459 | len -= name_len; |
Amaury Denoyelle | b666c6b | 2022-06-14 17:17:07 +0200 | [diff] [blame] | 460 | |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 461 | hvalue = *raw & 0x80; |
| 462 | value_len = qpack_get_varint(&raw, &len, 7); |
| 463 | if (len == (uint64_t)-1) { |
| 464 | qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__); |
| 465 | ret = -QPACK_ERR_TRUNCATED; |
| 466 | goto out; |
| 467 | } |
| 468 | |
| 469 | qpack_debug_printf(stderr, " hvalue=%d value_len=%llu", !!hvalue, (unsigned long long)value_len); |
| 470 | |
Frédéric Lécaille | e629cfd | 2021-12-15 14:16:16 +0100 | [diff] [blame] | 471 | if (len < value_len) { |
| 472 | qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__); |
| 473 | ret = -QPACK_ERR_TRUNCATED; |
| 474 | goto out; |
| 475 | } |
| 476 | |
Amaury Denoyelle | 4bcaf69 | 2022-06-14 16:34:55 +0200 | [diff] [blame] | 477 | if (hvalue) { |
| 478 | char *trash; |
| 479 | int nlen; |
| 480 | |
| 481 | trash = chunk_newstr(tmp); |
| 482 | if (!trash) { |
| 483 | qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__); |
| 484 | ret = -QPACK_DECOMPRESSION_FAILED; |
| 485 | goto out; |
| 486 | } |
| 487 | nlen = huff_dec(raw, value_len, trash, tmp->size - tmp->data); |
| 488 | if (nlen == (uint32_t)-1) { |
| 489 | qpack_debug_printf(stderr, " can't decode huffman.\n"); |
| 490 | ret = -QPACK_ERR_HUFFMAN; |
| 491 | goto out; |
| 492 | } |
| 493 | |
| 494 | qpack_debug_printf(stderr, " [name huff %d->%d '%s']", (int)value_len, (int)nlen, trash); |
| 495 | /* makes an ist from tmp storage */ |
| 496 | b_add(tmp, nlen); |
Amaury Denoyelle | b666c6b | 2022-06-14 17:17:07 +0200 | [diff] [blame] | 497 | value = ist2(trash, nlen); |
Amaury Denoyelle | 4bcaf69 | 2022-06-14 16:34:55 +0200 | [diff] [blame] | 498 | } |
| 499 | else { |
Amaury Denoyelle | b666c6b | 2022-06-14 17:17:07 +0200 | [diff] [blame] | 500 | value = ist2(raw, value_len); |
Amaury Denoyelle | 4bcaf69 | 2022-06-14 16:34:55 +0200 | [diff] [blame] | 501 | } |
| 502 | |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 503 | raw += value_len; |
| 504 | len -= value_len; |
| 505 | } |
Amaury Denoyelle | b666c6b | 2022-06-14 17:17:07 +0200 | [diff] [blame] | 506 | |
| 507 | list[hdr_idx].n = name; |
| 508 | list[hdr_idx].v = value; |
| 509 | ++hdr_idx; |
| 510 | |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 511 | qpack_debug_printf(stderr, "\n"); |
| 512 | } |
| 513 | |
Amaury Denoyelle | 60ef19f | 2022-06-14 17:38:36 +0200 | [diff] [blame] | 514 | if (hdr_idx >= list_size) { |
| 515 | qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__); |
| 516 | ret = -QPACK_ERR_TOO_LARGE; |
| 517 | goto out; |
| 518 | } |
| 519 | |
Amaury Denoyelle | fd7cdc3 | 2021-08-24 15:13:20 +0200 | [diff] [blame] | 520 | /* put an end marker */ |
| 521 | list[hdr_idx].n = list[hdr_idx].v = IST_NULL; |
Amaury Denoyelle | b666c6b | 2022-06-14 17:17:07 +0200 | [diff] [blame] | 522 | ret = hdr_idx; |
Amaury Denoyelle | fd7cdc3 | 2021-08-24 15:13:20 +0200 | [diff] [blame] | 523 | |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 524 | out: |
| 525 | qpack_debug_printf(stderr, "-- done: ret=%d\n", ret); |
| 526 | return ret; |
| 527 | } |