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