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) { |
Amaury Denoyelle | 58721f2 | 2023-05-09 18:01:09 +0200 | [diff] [blame] | 114 | qcc_set_error(qcs->qcc, H3_CLOSED_CRITICAL_STREAM, 1); |
Amaury Denoyelle | 26aa399 | 2022-08-16 17:42:47 +0200 | [diff] [blame] | 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 */ |
Amaury Denoyelle | c95106f | 2024-02-14 18:23:12 +0100 | [diff] [blame] | 138 | int capacity = *b_head(buf) & 0x1f; |
| 139 | |
| 140 | /* RFC 9204 4.3.1. Set Dynamic Table Capacity |
| 141 | * |
| 142 | * The decoder MUST treat a new dynamic table capacity |
| 143 | * value that exceeds this limit as a connection error of type |
| 144 | * QPACK_ENCODER_STREAM_ERROR. |
| 145 | */ |
| 146 | if (capacity) { |
| 147 | qcc_set_error(qcs->qcc, QPACK_ENCODER_STREAM_ERROR, 1); |
| 148 | return -1; |
| 149 | } |
| 150 | |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 151 | } |
| 152 | |
Amaury Denoyelle | 5869cb6 | 2022-05-31 15:21:27 +0200 | [diff] [blame] | 153 | return 0; |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 154 | } |
| 155 | |
Amaury Denoyelle | 5869cb6 | 2022-05-31 15:21:27 +0200 | [diff] [blame] | 156 | /* Decode an decoder stream. |
| 157 | * |
| 158 | * Returns 0 on success else non-zero. |
| 159 | */ |
Amaury Denoyelle | 26aa399 | 2022-08-16 17:42:47 +0200 | [diff] [blame] | 160 | 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] | 161 | { |
Amaury Denoyelle | 26aa399 | 2022-08-16 17:42:47 +0200 | [diff] [blame] | 162 | struct qcs *qcs = ctx; |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 163 | size_t len; |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 164 | unsigned char inst; |
| 165 | |
Amaury Denoyelle | 26aa399 | 2022-08-16 17:42:47 +0200 | [diff] [blame] | 166 | /* RFC 9204 4.2. Encoder and Decoder Streams |
| 167 | * |
| 168 | * The sender MUST NOT close either of these streams, and the receiver |
| 169 | * MUST NOT request that the sender close either of these streams. |
| 170 | * Closure of either unidirectional stream type MUST be treated as a |
| 171 | * connection error of type H3_CLOSED_CRITICAL_STREAM. |
| 172 | */ |
| 173 | if (fin) { |
Amaury Denoyelle | 58721f2 | 2023-05-09 18:01:09 +0200 | [diff] [blame] | 174 | qcc_set_error(qcs->qcc, H3_CLOSED_CRITICAL_STREAM, 1); |
Amaury Denoyelle | 26aa399 | 2022-08-16 17:42:47 +0200 | [diff] [blame] | 175 | return -1; |
| 176 | } |
| 177 | |
Amaury Denoyelle | 53eef46 | 2022-06-14 16:34:32 +0200 | [diff] [blame] | 178 | len = b_data(buf); |
| 179 | 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] | 180 | |
| 181 | if (!len) { |
| 182 | qpack_debug_printf(stderr, "[QPACK-DEC-DEC] empty stream\n"); |
| 183 | return 0; |
| 184 | } |
| 185 | |
Amaury Denoyelle | 53eef46 | 2022-06-14 16:34:32 +0200 | [diff] [blame] | 186 | 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] | 187 | if (inst == QPACK_DEC_INST_ICINC) { |
| 188 | /* Insert count increment */ |
Amaury Denoyelle | b840e60 | 2024-02-14 16:59:24 +0100 | [diff] [blame] | 189 | |
| 190 | /* RFC 9204 4.4.3. Insert Count Increment |
| 191 | * |
| 192 | * An encoder that receives an Increment field equal to zero, or one |
| 193 | * that increases the Known Received Count beyond what the encoder has |
| 194 | * sent, MUST treat this as a connection error of type |
| 195 | * QPACK_DECODER_STREAM_ERROR. |
| 196 | */ |
| 197 | |
| 198 | /* For the moment haproxy does not emit dynamic table insertion. */ |
| 199 | qcc_set_error(qcs->qcc, QPACK_DECODER_STREAM_ERROR, 1); |
| 200 | return -1; |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 201 | } |
| 202 | else if (inst & QPACK_DEC_INST_SACK) { |
| 203 | /* Section Acknowledgment */ |
| 204 | } |
| 205 | else if (inst & QPACK_DEC_INST_SCCL) { |
| 206 | /* Stream cancellation */ |
| 207 | } |
| 208 | |
Amaury Denoyelle | 5869cb6 | 2022-05-31 15:21:27 +0200 | [diff] [blame] | 209 | return 0; |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | /* Decode a field section prefix made of <enc_ric> and <db> two varints. |
| 213 | * Also set the 'S' sign bit for <db>. |
| 214 | * Return a negative error if failed, 0 if not. |
| 215 | */ |
| 216 | 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] | 217 | const unsigned char **raw, uint64_t *len) |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 218 | { |
| 219 | *enc_ric = qpack_get_varint(raw, len, 8); |
| 220 | if (*len == (uint64_t)-1) |
| 221 | return -QPACK_ERR_RIC; |
| 222 | |
| 223 | *sign_bit = **raw & 0x8; |
| 224 | *db = qpack_get_varint(raw, len, 7); |
| 225 | if (*len == (uint64_t)-1) |
| 226 | return -QPACK_ERR_DB; |
| 227 | |
| 228 | return 0; |
| 229 | } |
| 230 | |
Amaury Denoyelle | c5d31ed | 2022-06-14 17:34:53 +0200 | [diff] [blame] | 231 | /* 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] | 232 | * header is inserted into <list> of <list_size> entries max and uses <tmp> as |
| 233 | * a storage for some elements pointing into it. An end marker is inserted at |
| 234 | * the end of the list with empty strings as name/value. |
Amaury Denoyelle | c5d31ed | 2022-06-14 17:34:53 +0200 | [diff] [blame] | 235 | * |
Amaury Denoyelle | b666c6b | 2022-06-14 17:17:07 +0200 | [diff] [blame] | 236 | * Returns the number of headers inserted into list excluding the end marker. |
| 237 | * 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] | 238 | */ |
Frédéric Lécaille | 628e89c | 2022-06-24 12:13:53 +0200 | [diff] [blame] | 239 | 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] | 240 | struct http_hdr *list, int list_size) |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 241 | { |
Amaury Denoyelle | b666c6b | 2022-06-14 17:17:07 +0200 | [diff] [blame] | 242 | struct ist name, value; |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 243 | uint64_t enc_ric, db; |
| 244 | int s; |
| 245 | unsigned int efl_type; |
| 246 | int ret; |
Amaury Denoyelle | fd7cdc3 | 2021-08-24 15:13:20 +0200 | [diff] [blame] | 247 | int hdr_idx = 0; |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 248 | |
| 249 | qpack_debug_hexdump(stderr, "[QPACK-DEC-FS] ", (const char *)raw, 0, len); |
| 250 | |
Amaury Denoyelle | c5d31ed | 2022-06-14 17:34:53 +0200 | [diff] [blame] | 251 | /* parse field section prefix */ |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 252 | ret = qpack_decode_fs_pfx(&enc_ric, &db, &s, &raw, &len); |
| 253 | if (ret < 0) { |
| 254 | qpack_debug_printf(stderr, "##ERR@%d(%d)\n", __LINE__, ret); |
| 255 | goto out; |
| 256 | } |
| 257 | |
| 258 | chunk_reset(tmp); |
| 259 | qpack_debug_printf(stderr, "enc_ric: %llu db: %llu s=%d\n", |
| 260 | (unsigned long long)enc_ric, (unsigned long long)db, !!s); |
| 261 | /* Decode field lines */ |
| 262 | while (len) { |
Amaury Denoyelle | 60ef19f | 2022-06-14 17:38:36 +0200 | [diff] [blame] | 263 | if (hdr_idx >= list_size) { |
| 264 | qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__); |
| 265 | ret = -QPACK_ERR_TOO_LARGE; |
| 266 | goto out; |
| 267 | } |
| 268 | |
Amaury Denoyelle | c5d31ed | 2022-06-14 17:34:53 +0200 | [diff] [blame] | 269 | /* parse field line representation */ |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 270 | efl_type = *raw & QPACK_EFL_BITMASK; |
| 271 | qpack_debug_printf(stderr, "efl_type=0x%02x\n", efl_type); |
Amaury Denoyelle | c5d31ed | 2022-06-14 17:34:53 +0200 | [diff] [blame] | 272 | |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 273 | if (efl_type == QPACK_LFL_WPBNM) { |
Amaury Denoyelle | 28d3c24 | 2022-06-14 16:36:15 +0200 | [diff] [blame] | 274 | /* Literal field line with post-base name reference |
Amaury Denoyelle | a7a4c80 | 2022-06-30 09:30:23 +0200 | [diff] [blame] | 275 | * TODO adjust this when dynamic table support is implemented. |
Amaury Denoyelle | 28d3c24 | 2022-06-14 16:36:15 +0200 | [diff] [blame] | 276 | */ |
| 277 | #if 0 |
Amaury Denoyelle | 18a10d0 | 2022-03-25 15:11:38 +0100 | [diff] [blame] | 278 | uint64_t index __maybe_unused, length; |
Amaury Denoyelle | d96361b | 2022-03-25 14:56:51 +0100 | [diff] [blame] | 279 | unsigned int n __maybe_unused, h __maybe_unused; |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 280 | |
| 281 | qpack_debug_printf(stderr, "literal field line with post-base name reference:"); |
| 282 | n = *raw & 0x08; |
| 283 | index = qpack_get_varint(&raw, &len, 3); |
| 284 | if (len == (uint64_t)-1) { |
| 285 | qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__); |
| 286 | ret = -QPACK_ERR_TRUNCATED; |
| 287 | goto out; |
| 288 | } |
| 289 | |
| 290 | qpack_debug_printf(stderr, " n=%d index=%llu", !!n, (unsigned long long)index); |
| 291 | h = *raw & 0x80; |
| 292 | length = qpack_get_varint(&raw, &len, 7); |
| 293 | if (len == (uint64_t)-1) { |
| 294 | qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__); |
| 295 | ret = -QPACK_ERR_TRUNCATED; |
| 296 | goto out; |
| 297 | } |
| 298 | |
| 299 | 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] | 300 | |
| 301 | if (len < length) { |
| 302 | qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__); |
| 303 | ret = -QPACK_ERR_TRUNCATED; |
| 304 | goto out; |
| 305 | } |
| 306 | |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 307 | raw += length; |
| 308 | len -= length; |
Amaury Denoyelle | 28d3c24 | 2022-06-14 16:36:15 +0200 | [diff] [blame] | 309 | #endif |
Amaury Denoyelle | a7a4c80 | 2022-06-30 09:30:23 +0200 | [diff] [blame] | 310 | |
| 311 | /* RFC9204 2.2.3 Invalid References |
| 312 | * |
| 313 | * If the decoder encounters a reference in a field line representation |
| 314 | * to a dynamic table entry that has already been evicted or that has an |
| 315 | * absolute index greater than or equal to the declared Required Insert |
| 316 | * Count (Section 4.5.1), it MUST treat this as a connection error of |
| 317 | * type QPACK_DECOMPRESSION_FAILED. |
| 318 | */ |
Amaury Denoyelle | c3c4d1b | 2024-05-13 16:01:08 +0200 | [diff] [blame] | 319 | return -QPACK_ERR_DECOMP; |
Amaury Denoyelle | fd7cdc3 | 2021-08-24 15:13:20 +0200 | [diff] [blame] | 320 | } |
| 321 | else if (efl_type == QPACK_IFL_WPBI) { |
Amaury Denoyelle | 28d3c24 | 2022-06-14 16:36:15 +0200 | [diff] [blame] | 322 | /* Indexed field line with post-base index |
Amaury Denoyelle | a7a4c80 | 2022-06-30 09:30:23 +0200 | [diff] [blame] | 323 | * TODO adjust this when dynamic table support is implemented. |
Amaury Denoyelle | 28d3c24 | 2022-06-14 16:36:15 +0200 | [diff] [blame] | 324 | */ |
| 325 | #if 0 |
Amaury Denoyelle | 18a10d0 | 2022-03-25 15:11:38 +0100 | [diff] [blame] | 326 | uint64_t index __maybe_unused; |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 327 | |
| 328 | qpack_debug_printf(stderr, "indexed field line with post-base index:"); |
| 329 | index = qpack_get_varint(&raw, &len, 4); |
| 330 | if (len == (uint64_t)-1) { |
| 331 | qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__); |
| 332 | ret = -QPACK_ERR_TRUNCATED; |
| 333 | goto out; |
| 334 | } |
| 335 | |
| 336 | qpack_debug_printf(stderr, " index=%llu", (unsigned long long)index); |
Amaury Denoyelle | 28d3c24 | 2022-06-14 16:36:15 +0200 | [diff] [blame] | 337 | #endif |
Amaury Denoyelle | a7a4c80 | 2022-06-30 09:30:23 +0200 | [diff] [blame] | 338 | |
| 339 | /* RFC9204 2.2.3 Invalid References |
| 340 | * |
| 341 | * If the decoder encounters a reference in a field line representation |
| 342 | * to a dynamic table entry that has already been evicted or that has an |
| 343 | * absolute index greater than or equal to the declared Required Insert |
| 344 | * Count (Section 4.5.1), it MUST treat this as a connection error of |
| 345 | * type QPACK_DECOMPRESSION_FAILED. |
| 346 | */ |
Amaury Denoyelle | c3c4d1b | 2024-05-13 16:01:08 +0200 | [diff] [blame] | 347 | return -QPACK_ERR_DECOMP; |
Amaury Denoyelle | fd7cdc3 | 2021-08-24 15:13:20 +0200 | [diff] [blame] | 348 | } |
| 349 | else if (efl_type & QPACK_IFL_BIT) { |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 350 | /* Indexed field line */ |
| 351 | uint64_t index; |
Amaury Denoyelle | debaa04 | 2022-06-20 15:47:46 +0200 | [diff] [blame] | 352 | unsigned int static_tbl; |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 353 | |
| 354 | qpack_debug_printf(stderr, "indexed field line:"); |
Amaury Denoyelle | debaa04 | 2022-06-20 15:47:46 +0200 | [diff] [blame] | 355 | static_tbl = efl_type & 0x40; |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 356 | index = qpack_get_varint(&raw, &len, 6); |
| 357 | if (len == (uint64_t)-1) { |
| 358 | qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__); |
| 359 | ret = -QPACK_ERR_TRUNCATED; |
| 360 | goto out; |
| 361 | } |
| 362 | |
Willy Tarreau | f41dfc2 | 2023-03-17 16:40:09 +0100 | [diff] [blame] | 363 | if (static_tbl && index < QPACK_SHT_SIZE) { |
Amaury Denoyelle | b666c6b | 2022-06-14 17:17:07 +0200 | [diff] [blame] | 364 | name = qpack_sht[index].n; |
| 365 | value = qpack_sht[index].v; |
| 366 | } |
Amaury Denoyelle | debaa04 | 2022-06-20 15:47:46 +0200 | [diff] [blame] | 367 | else { |
Amaury Denoyelle | a7a4c80 | 2022-06-30 09:30:23 +0200 | [diff] [blame] | 368 | /* RFC9204 2.2.3 Invalid References |
Amaury Denoyelle | debaa04 | 2022-06-20 15:47:46 +0200 | [diff] [blame] | 369 | * |
Amaury Denoyelle | a7a4c80 | 2022-06-30 09:30:23 +0200 | [diff] [blame] | 370 | * If the decoder encounters a reference in a field line representation |
| 371 | * to a dynamic table entry that has already been evicted or that has an |
| 372 | * absolute index greater than or equal to the declared Required Insert |
| 373 | * Count (Section 4.5.1), it MUST treat this as a connection error of |
| 374 | * type QPACK_DECOMPRESSION_FAILED. |
| 375 | * |
| 376 | * TODO adjust this when dynamic table support is implemented. |
Amaury Denoyelle | debaa04 | 2022-06-20 15:47:46 +0200 | [diff] [blame] | 377 | */ |
Amaury Denoyelle | c3c4d1b | 2024-05-13 16:01:08 +0200 | [diff] [blame] | 378 | return -QPACK_ERR_DECOMP; |
Amaury Denoyelle | debaa04 | 2022-06-20 15:47:46 +0200 | [diff] [blame] | 379 | } |
Amaury Denoyelle | fd7cdc3 | 2021-08-24 15:13:20 +0200 | [diff] [blame] | 380 | |
Amaury Denoyelle | 055de23 | 2022-06-30 09:28:50 +0200 | [diff] [blame] | 381 | 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] | 382 | } |
| 383 | else if (efl_type & QPACK_LFL_WNR_BIT) { |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 384 | /* Literal field line with name reference */ |
| 385 | uint64_t index, length; |
Amaury Denoyelle | 46e992d | 2022-06-30 09:31:24 +0200 | [diff] [blame] | 386 | unsigned int static_tbl, n __maybe_unused, h; |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 387 | |
| 388 | qpack_debug_printf(stderr, "Literal field line with name reference:"); |
| 389 | n = efl_type & 0x20; |
Amaury Denoyelle | 46e992d | 2022-06-30 09:31:24 +0200 | [diff] [blame] | 390 | static_tbl = efl_type & 0x10; |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 391 | index = qpack_get_varint(&raw, &len, 4); |
| 392 | if (len == (uint64_t)-1) { |
| 393 | qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__); |
| 394 | ret = -QPACK_ERR_TRUNCATED; |
| 395 | goto out; |
| 396 | } |
| 397 | |
Willy Tarreau | f41dfc2 | 2023-03-17 16:40:09 +0100 | [diff] [blame] | 398 | if (static_tbl && index < QPACK_SHT_SIZE) { |
Amaury Denoyelle | b666c6b | 2022-06-14 17:17:07 +0200 | [diff] [blame] | 399 | name = qpack_sht[index].n; |
Amaury Denoyelle | 46e992d | 2022-06-30 09:31:24 +0200 | [diff] [blame] | 400 | } |
| 401 | else { |
Amaury Denoyelle | a7a4c80 | 2022-06-30 09:30:23 +0200 | [diff] [blame] | 402 | /* RFC9204 2.2.3 Invalid References |
Amaury Denoyelle | 46e992d | 2022-06-30 09:31:24 +0200 | [diff] [blame] | 403 | * |
Amaury Denoyelle | a7a4c80 | 2022-06-30 09:30:23 +0200 | [diff] [blame] | 404 | * If the decoder encounters a reference in a field line representation |
| 405 | * to a dynamic table entry that has already been evicted or that has an |
| 406 | * absolute index greater than or equal to the declared Required Insert |
| 407 | * Count (Section 4.5.1), it MUST treat this as a connection error of |
| 408 | * type QPACK_DECOMPRESSION_FAILED. |
| 409 | * |
| 410 | * TODO adjust this when dynamic table support is implemented. |
Amaury Denoyelle | 46e992d | 2022-06-30 09:31:24 +0200 | [diff] [blame] | 411 | */ |
Amaury Denoyelle | c3c4d1b | 2024-05-13 16:01:08 +0200 | [diff] [blame] | 412 | return -QPACK_ERR_DECOMP; |
Amaury Denoyelle | 46e992d | 2022-06-30 09:31:24 +0200 | [diff] [blame] | 413 | } |
Amaury Denoyelle | fd7cdc3 | 2021-08-24 15:13:20 +0200 | [diff] [blame] | 414 | |
Amaury Denoyelle | 46e992d | 2022-06-30 09:31:24 +0200 | [diff] [blame] | 415 | 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] | 416 | h = *raw & 0x80; |
| 417 | length = qpack_get_varint(&raw, &len, 7); |
| 418 | if (len == (uint64_t)-1) { |
| 419 | qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__); |
| 420 | ret = -QPACK_ERR_TRUNCATED; |
| 421 | goto out; |
| 422 | } |
| 423 | |
| 424 | qpack_debug_printf(stderr, " h=%d length=%llu", !!h, (unsigned long long)length); |
| 425 | if (h) { |
| 426 | char *trash; |
| 427 | int nlen; |
| 428 | |
| 429 | trash = chunk_newstr(tmp); |
| 430 | if (!trash) { |
| 431 | qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__); |
Amaury Denoyelle | c3c4d1b | 2024-05-13 16:01:08 +0200 | [diff] [blame] | 432 | ret = -QPACK_ERR_TOO_LARGE; |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 433 | goto out; |
| 434 | } |
| 435 | nlen = huff_dec(raw, length, trash, tmp->size - tmp->data); |
| 436 | if (nlen == (uint32_t)-1) { |
| 437 | qpack_debug_printf(stderr, " can't decode huffman.\n"); |
| 438 | ret = -QPACK_ERR_HUFFMAN; |
| 439 | goto out; |
| 440 | } |
| 441 | |
| 442 | 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] | 443 | /* makes an ist from tmp storage */ |
| 444 | b_add(tmp, nlen); |
Amaury Denoyelle | b666c6b | 2022-06-14 17:17:07 +0200 | [diff] [blame] | 445 | value = ist2(trash, nlen); |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 446 | } |
Amaury Denoyelle | 7d3aea5 | 2021-11-24 16:04:03 +0100 | [diff] [blame] | 447 | else { |
Amaury Denoyelle | b666c6b | 2022-06-14 17:17:07 +0200 | [diff] [blame] | 448 | value = ist2(raw, length); |
Amaury Denoyelle | 7d3aea5 | 2021-11-24 16:04:03 +0100 | [diff] [blame] | 449 | } |
| 450 | |
Frédéric Lécaille | e629cfd | 2021-12-15 14:16:16 +0100 | [diff] [blame] | 451 | if (len < length) { |
| 452 | qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__); |
| 453 | ret = -QPACK_ERR_TRUNCATED; |
| 454 | goto out; |
| 455 | } |
| 456 | |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 457 | raw += length; |
| 458 | len -= length; |
Amaury Denoyelle | fd7cdc3 | 2021-08-24 15:13:20 +0200 | [diff] [blame] | 459 | } |
| 460 | else if (efl_type & QPACK_LFL_WLN_BIT) { |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 461 | /* Literal field line with literal name */ |
Amaury Denoyelle | 4bcaf69 | 2022-06-14 16:34:55 +0200 | [diff] [blame] | 462 | unsigned int n __maybe_unused, hname, hvalue; |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 463 | uint64_t name_len, value_len; |
| 464 | |
| 465 | qpack_debug_printf(stderr, "Literal field line with literal name:"); |
| 466 | n = *raw & 0x10; |
| 467 | hname = *raw & 0x08; |
| 468 | name_len = qpack_get_varint(&raw, &len, 3); |
| 469 | if (len == (uint64_t)-1) { |
| 470 | qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__); |
| 471 | ret = -QPACK_ERR_TRUNCATED; |
| 472 | goto out; |
| 473 | } |
| 474 | |
Amaury Denoyelle | ab9cec7 | 2022-02-14 14:45:10 +0100 | [diff] [blame] | 475 | 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] | 476 | /* Name string */ |
Frédéric Lécaille | e629cfd | 2021-12-15 14:16:16 +0100 | [diff] [blame] | 477 | |
| 478 | if (len < name_len) { |
| 479 | qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__); |
| 480 | ret = -QPACK_ERR_TRUNCATED; |
| 481 | goto out; |
| 482 | } |
| 483 | |
Amaury Denoyelle | 4bcaf69 | 2022-06-14 16:34:55 +0200 | [diff] [blame] | 484 | if (hname) { |
| 485 | char *trash; |
| 486 | int nlen; |
| 487 | |
| 488 | trash = chunk_newstr(tmp); |
| 489 | if (!trash) { |
| 490 | qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__); |
Amaury Denoyelle | c3c4d1b | 2024-05-13 16:01:08 +0200 | [diff] [blame] | 491 | ret = -QPACK_ERR_TOO_LARGE; |
Amaury Denoyelle | 4bcaf69 | 2022-06-14 16:34:55 +0200 | [diff] [blame] | 492 | goto out; |
| 493 | } |
| 494 | nlen = huff_dec(raw, name_len, trash, tmp->size - tmp->data); |
| 495 | if (nlen == (uint32_t)-1) { |
| 496 | qpack_debug_printf(stderr, " can't decode huffman.\n"); |
| 497 | ret = -QPACK_ERR_HUFFMAN; |
| 498 | goto out; |
| 499 | } |
| 500 | |
| 501 | qpack_debug_printf(stderr, " [name huff %d->%d '%s']", (int)name_len, (int)nlen, trash); |
| 502 | /* makes an ist from tmp storage */ |
| 503 | b_add(tmp, nlen); |
Amaury Denoyelle | b666c6b | 2022-06-14 17:17:07 +0200 | [diff] [blame] | 504 | name = ist2(trash, nlen); |
Amaury Denoyelle | 4bcaf69 | 2022-06-14 16:34:55 +0200 | [diff] [blame] | 505 | } |
| 506 | else { |
Amaury Denoyelle | b666c6b | 2022-06-14 17:17:07 +0200 | [diff] [blame] | 507 | name = ist2(raw, name_len); |
Amaury Denoyelle | 4bcaf69 | 2022-06-14 16:34:55 +0200 | [diff] [blame] | 508 | } |
| 509 | |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 510 | raw += name_len; |
| 511 | len -= name_len; |
Amaury Denoyelle | b666c6b | 2022-06-14 17:17:07 +0200 | [diff] [blame] | 512 | |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 513 | hvalue = *raw & 0x80; |
| 514 | value_len = qpack_get_varint(&raw, &len, 7); |
| 515 | if (len == (uint64_t)-1) { |
| 516 | qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__); |
| 517 | ret = -QPACK_ERR_TRUNCATED; |
| 518 | goto out; |
| 519 | } |
| 520 | |
| 521 | qpack_debug_printf(stderr, " hvalue=%d value_len=%llu", !!hvalue, (unsigned long long)value_len); |
| 522 | |
Frédéric Lécaille | e629cfd | 2021-12-15 14:16:16 +0100 | [diff] [blame] | 523 | if (len < value_len) { |
| 524 | qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__); |
| 525 | ret = -QPACK_ERR_TRUNCATED; |
| 526 | goto out; |
| 527 | } |
| 528 | |
Amaury Denoyelle | 4bcaf69 | 2022-06-14 16:34:55 +0200 | [diff] [blame] | 529 | if (hvalue) { |
| 530 | char *trash; |
| 531 | int nlen; |
| 532 | |
| 533 | trash = chunk_newstr(tmp); |
| 534 | if (!trash) { |
| 535 | qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__); |
Amaury Denoyelle | c3c4d1b | 2024-05-13 16:01:08 +0200 | [diff] [blame] | 536 | ret = -QPACK_ERR_TOO_LARGE; |
Amaury Denoyelle | 4bcaf69 | 2022-06-14 16:34:55 +0200 | [diff] [blame] | 537 | goto out; |
| 538 | } |
| 539 | nlen = huff_dec(raw, value_len, trash, tmp->size - tmp->data); |
| 540 | if (nlen == (uint32_t)-1) { |
| 541 | qpack_debug_printf(stderr, " can't decode huffman.\n"); |
| 542 | ret = -QPACK_ERR_HUFFMAN; |
| 543 | goto out; |
| 544 | } |
| 545 | |
| 546 | qpack_debug_printf(stderr, " [name huff %d->%d '%s']", (int)value_len, (int)nlen, trash); |
| 547 | /* makes an ist from tmp storage */ |
| 548 | b_add(tmp, nlen); |
Amaury Denoyelle | b666c6b | 2022-06-14 17:17:07 +0200 | [diff] [blame] | 549 | value = ist2(trash, nlen); |
Amaury Denoyelle | 4bcaf69 | 2022-06-14 16:34:55 +0200 | [diff] [blame] | 550 | } |
| 551 | else { |
Amaury Denoyelle | b666c6b | 2022-06-14 17:17:07 +0200 | [diff] [blame] | 552 | value = ist2(raw, value_len); |
Amaury Denoyelle | 4bcaf69 | 2022-06-14 16:34:55 +0200 | [diff] [blame] | 553 | } |
| 554 | |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 555 | raw += value_len; |
| 556 | len -= value_len; |
| 557 | } |
Amaury Denoyelle | b666c6b | 2022-06-14 17:17:07 +0200 | [diff] [blame] | 558 | |
Willy Tarreau | a8598a2 | 2023-02-09 21:36:54 +0100 | [diff] [blame] | 559 | /* We must not accept empty header names (forbidden by the spec and used |
| 560 | * as a list termination). |
| 561 | */ |
| 562 | if (!name.len) { |
| 563 | qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__); |
Amaury Denoyelle | c3c4d1b | 2024-05-13 16:01:08 +0200 | [diff] [blame] | 564 | ret = -QPACK_ERR_DECOMP; |
Willy Tarreau | a8598a2 | 2023-02-09 21:36:54 +0100 | [diff] [blame] | 565 | goto out; |
| 566 | } |
| 567 | |
Amaury Denoyelle | b666c6b | 2022-06-14 17:17:07 +0200 | [diff] [blame] | 568 | list[hdr_idx].n = name; |
| 569 | list[hdr_idx].v = value; |
| 570 | ++hdr_idx; |
| 571 | |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 572 | qpack_debug_printf(stderr, "\n"); |
| 573 | } |
| 574 | |
Amaury Denoyelle | 60ef19f | 2022-06-14 17:38:36 +0200 | [diff] [blame] | 575 | if (hdr_idx >= list_size) { |
| 576 | qpack_debug_printf(stderr, "##ERR@%d\n", __LINE__); |
| 577 | ret = -QPACK_ERR_TOO_LARGE; |
| 578 | goto out; |
| 579 | } |
| 580 | |
Amaury Denoyelle | fd7cdc3 | 2021-08-24 15:13:20 +0200 | [diff] [blame] | 581 | /* put an end marker */ |
| 582 | list[hdr_idx].n = list[hdr_idx].v = IST_NULL; |
Amaury Denoyelle | b666c6b | 2022-06-14 17:17:07 +0200 | [diff] [blame] | 583 | ret = hdr_idx; |
Amaury Denoyelle | fd7cdc3 | 2021-08-24 15:13:20 +0200 | [diff] [blame] | 584 | |
Frédéric Lécaille | b4672fb | 2021-03-03 16:13:10 +0100 | [diff] [blame] | 585 | out: |
| 586 | qpack_debug_printf(stderr, "-- done: ret=%d\n", ret); |
| 587 | return ret; |
| 588 | } |
Amaury Denoyelle | c3c4d1b | 2024-05-13 16:01:08 +0200 | [diff] [blame] | 589 | |
| 590 | /* Convert return value from qpack_decode_fs() to a standard error code usable |
| 591 | * in CONNECTION_CLOSE or -1 for an internal error. |
| 592 | */ |
| 593 | int qpack_err_decode(const int value) |
| 594 | { |
| 595 | return (value == -QPACK_ERR_DECOMP) ? QPACK_DECOMPRESSION_FAILED : -1; |
| 596 | } |