| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#include <aws/http/private/hpack.h> |
| 6 |
|
| 7 |
#define HPACK_LOGF(level, decoder, text, ...) \ |
| 8 |
AWS_LOGF_##level(AWS_LS_HTTP_DECODER, "id=%p [HPACK]: " text, (decoder)->log_id, __VA_ARGS__) |
| 9 |
#define HPACK_LOG(level, decoder, text) HPACK_LOGF(level, decoder, "%s", text) |
| 10 |
|
| 11 |
struct aws_huffman_symbol_coder *hpack_get_coder(void); |
| 12 |
|
| 13 |
|
| 14 |
const size_t s_hpack_decoder_scratch_initial_size = 512; |
| 15 |
|
| 16 |
void aws_hpack_decoder_init(struct aws_hpack_decoder *decoder, struct aws_allocator *allocator, const void *log_id) { |
| 17 |
AWS_ZERO_STRUCT(*decoder); |
| 18 |
decoder->log_id = log_id; |
| 19 |
|
| 20 |
aws_huffman_decoder_init(&decoder->huffman_decoder, hpack_get_coder()); |
| 21 |
aws_huffman_decoder_allow_growth(&decoder->huffman_decoder, true); |
| 22 |
|
| 23 |
aws_hpack_context_init(&decoder->context, allocator, AWS_LS_HTTP_DECODER, log_id); |
| 24 |
|
| 25 |
aws_byte_buf_init(&decoder->progress_entry.scratch, allocator, s_hpack_decoder_scratch_initial_size); |
| 26 |
|
| 27 |
decoder->dynamic_table_protocol_max_size_setting = aws_hpack_get_dynamic_table_max_size(&decoder->context); |
| 28 |
} |
| 29 |
|
| 30 |
void aws_hpack_decoder_clean_up(struct aws_hpack_decoder *decoder) { |
| 31 |
aws_hpack_context_clean_up(&decoder->context); |
| 32 |
aws_byte_buf_clean_up(&decoder->progress_entry.scratch); |
| 33 |
AWS_ZERO_STRUCT(*decoder); |
| 34 |
} |
| 35 |
|
| 36 |
static const struct aws_http_header *s_get_header_u64(const struct aws_hpack_decoder *decoder, uint64_t index) { |
| 37 |
if (index > SIZE_MAX) { |
| 38 |
HPACK_LOG(ERROR, decoder, "Header index is absurdly large"); |
| 39 |
aws_raise_error(AWS_ERROR_INVALID_INDEX); |
| 40 |
return NULL; |
| 41 |
} |
| 42 |
|
| 43 |
return aws_hpack_get_header(&decoder->context, (size_t)index); |
| 44 |
} |
| 45 |
|
| 46 |
void aws_hpack_decoder_update_max_table_size(struct aws_hpack_decoder *decoder, uint32_t setting_max_size) { |
| 47 |
decoder->dynamic_table_protocol_max_size_setting = setting_max_size; |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
static uint8_t s_masked_right_bits_u8(uint8_t num_masked_bits) { |
| 53 |
AWS_ASSERT(num_masked_bits <= 8); |
| 54 |
const uint8_t cut_bits = 8 - num_masked_bits; |
| 55 |
return UINT8_MAX >> cut_bits; |
| 56 |
} |
| 57 |
|
| 58 |
int aws_hpack_decode_integer( |
| 59 |
struct aws_hpack_decoder *decoder, |
| 60 |
struct aws_byte_cursor *to_decode, |
| 61 |
uint8_t prefix_size, |
| 62 |
uint64_t *integer, |
| 63 |
bool *complete) { |
| 64 |
|
| 65 |
AWS_PRECONDITION(decoder); |
| 66 |
AWS_PRECONDITION(to_decode); |
| 67 |
AWS_PRECONDITION(prefix_size <= 8); |
| 68 |
AWS_PRECONDITION(integer); |
| 69 |
|
| 70 |
const uint8_t prefix_mask = s_masked_right_bits_u8(prefix_size); |
| 71 |
|
| 72 |
struct hpack_progress_integer *progress = &decoder->progress_integer; |
| 73 |
|
| 74 |
while (to_decode->len) { |
| 75 |
switch (progress->state) { |
| 76 |
case HPACK_INTEGER_STATE_INIT: { |
| 77 |
|
| 78 |
uint8_t byte = 0; |
| 79 |
bool succ = aws_byte_cursor_read_u8(to_decode, &byte); |
| 80 |
AWS_FATAL_ASSERT(succ); |
| 81 |
|
| 82 |
|
| 83 |
byte &= prefix_mask; |
| 84 |
|
| 85 |
|
| 86 |
*integer = byte; |
| 87 |
|
| 88 |
if (byte != prefix_mask) { |
| 89 |
goto handle_complete; |
| 90 |
} |
| 91 |
|
| 92 |
progress->state = HPACK_INTEGER_STATE_VALUE; |
| 93 |
} break; |
| 94 |
|
| 95 |
case HPACK_INTEGER_STATE_VALUE: { |
| 96 |
uint8_t byte = 0; |
| 97 |
bool succ = aws_byte_cursor_read_u8(to_decode, &byte); |
| 98 |
AWS_FATAL_ASSERT(succ); |
| 99 |
|
| 100 |
uint64_t new_byte_value = (uint64_t)(byte & 127) << progress->bit_count; |
| 101 |
if (*integer + new_byte_value < *integer) { |
| 102 |
return aws_raise_error(AWS_ERROR_OVERFLOW_DETECTED); |
| 103 |
} |
| 104 |
*integer += new_byte_value; |
| 105 |
|
| 106 |
|
| 107 |
if ((byte & 128) == 0) { |
| 108 |
goto handle_complete; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
progress->bit_count += 7; |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
if (progress->bit_count > 64 - 7) { |
| 117 |
return aws_raise_error(AWS_ERROR_OVERFLOW_DETECTED); |
| 118 |
} |
| 119 |
} break; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
*complete = false; |
| 125 |
return AWS_OP_SUCCESS; |
| 126 |
|
| 127 |
handle_complete: |
| 128 |
AWS_ZERO_STRUCT(decoder->progress_integer); |
| 129 |
*complete = true; |
| 130 |
return AWS_OP_SUCCESS; |
| 131 |
} |
| 132 |
|
| 133 |
int aws_hpack_decode_string( |
| 134 |
struct aws_hpack_decoder *decoder, |
| 135 |
struct aws_byte_cursor *to_decode, |
| 136 |
struct aws_byte_buf *output, |
| 137 |
bool *complete) { |
| 138 |
|
| 139 |
AWS_PRECONDITION(decoder); |
| 140 |
AWS_PRECONDITION(to_decode); |
| 141 |
AWS_PRECONDITION(output); |
| 142 |
AWS_PRECONDITION(complete); |
| 143 |
|
| 144 |
struct hpack_progress_string *progress = &decoder->progress_string; |
| 145 |
|
| 146 |
while (to_decode->len) { |
| 147 |
switch (progress->state) { |
| 148 |
case HPACK_STRING_STATE_INIT: { |
| 149 |
|
| 150 |
progress->state = HPACK_STRING_STATE_LENGTH; |
| 151 |
progress->use_huffman = *to_decode->ptr >> 7; |
| 152 |
aws_huffman_decoder_reset(&decoder->huffman_decoder); |
| 153 |
|
| 154 |
} |
| 155 |
|
| 156 |
case HPACK_STRING_STATE_LENGTH: { |
| 157 |
bool length_complete = false; |
| 158 |
if (aws_hpack_decode_integer(decoder, to_decode, 7, &progress->length, &length_complete)) { |
| 159 |
return AWS_OP_ERR; |
| 160 |
} |
| 161 |
|
| 162 |
if (!length_complete) { |
| 163 |
goto handle_ongoing; |
| 164 |
} |
| 165 |
|
| 166 |
if (progress->length == 0) { |
| 167 |
goto handle_complete; |
| 168 |
} |
| 169 |
|
| 170 |
if (progress->length > SIZE_MAX) { |
| 171 |
return aws_raise_error(AWS_ERROR_OVERFLOW_DETECTED); |
| 172 |
} |
| 173 |
|
| 174 |
progress->state = HPACK_STRING_STATE_VALUE; |
| 175 |
} break; |
| 176 |
|
| 177 |
case HPACK_STRING_STATE_VALUE: { |
| 178 |
|
| 179 |
size_t to_process = aws_min_size((size_t)progress->length, to_decode->len); |
| 180 |
progress->length -= to_process; |
| 181 |
|
| 182 |
struct aws_byte_cursor chunk = aws_byte_cursor_advance(to_decode, to_process); |
| 183 |
|
| 184 |
if (progress->use_huffman) { |
| 185 |
if (aws_huffman_decode(&decoder->huffman_decoder, &chunk, output)) { |
| 186 |
HPACK_LOGF(ERROR, decoder, "Error from Huffman decoder: %s", aws_error_name(aws_last_error())); |
| 187 |
return AWS_OP_ERR; |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
if (chunk.len != 0) { |
| 193 |
HPACK_LOG(ERROR, decoder, "Huffman encoded end-of-string symbol is illegal"); |
| 194 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 195 |
} |
| 196 |
} else { |
| 197 |
if (aws_byte_buf_append_dynamic(output, &chunk)) { |
| 198 |
return AWS_OP_ERR; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
if (progress->length == 0) { |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
goto handle_complete; |
| 211 |
} |
| 212 |
} break; |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
handle_ongoing: |
| 217 |
|
| 218 |
AWS_ASSERT(to_decode->len == 0); |
| 219 |
*complete = false; |
| 220 |
return AWS_OP_SUCCESS; |
| 221 |
|
| 222 |
handle_complete: |
| 223 |
AWS_ASSERT(decoder->progress_string.length == 0); |
| 224 |
AWS_ZERO_STRUCT(decoder->progress_string); |
| 225 |
*complete = true; |
| 226 |
return AWS_OP_SUCCESS; |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
int aws_hpack_decode( |
| 231 |
struct aws_hpack_decoder *decoder, |
| 232 |
struct aws_byte_cursor *to_decode, |
| 233 |
struct aws_hpack_decode_result *result) { |
| 234 |
|
| 235 |
AWS_PRECONDITION(decoder); |
| 236 |
AWS_PRECONDITION(to_decode); |
| 237 |
AWS_PRECONDITION(result); |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
while (to_decode->len) { |
| 242 |
switch (decoder->progress_entry.state) { |
| 243 |
|
| 244 |
case HPACK_ENTRY_STATE_INIT: { |
| 245 |
|
| 246 |
AWS_ZERO_STRUCT(decoder->progress_entry.u); |
| 247 |
decoder->progress_entry.scratch.len = 0; |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
uint8_t first_byte = to_decode->ptr[0]; |
| 256 |
if (first_byte & (1 << 7)) { |
| 257 |
|
| 258 |
decoder->progress_entry.state = HPACK_ENTRY_STATE_INDEXED; |
| 259 |
|
| 260 |
} else if (first_byte & (1 << 6)) { |
| 261 |
|
| 262 |
decoder->progress_entry.u.literal.compression = AWS_HTTP_HEADER_COMPRESSION_USE_CACHE; |
| 263 |
decoder->progress_entry.u.literal.prefix_size = 6; |
| 264 |
decoder->progress_entry.state = HPACK_ENTRY_STATE_LITERAL_BEGIN; |
| 265 |
|
| 266 |
} else if (first_byte & (1 << 5)) { |
| 267 |
|
| 268 |
decoder->progress_entry.state = HPACK_ENTRY_STATE_DYNAMIC_TABLE_RESIZE; |
| 269 |
|
| 270 |
} else if (first_byte & (1 << 4)) { |
| 271 |
|
| 272 |
decoder->progress_entry.u.literal.compression = AWS_HTTP_HEADER_COMPRESSION_NO_FORWARD_CACHE; |
| 273 |
decoder->progress_entry.u.literal.prefix_size = 4; |
| 274 |
decoder->progress_entry.state = HPACK_ENTRY_STATE_LITERAL_BEGIN; |
| 275 |
} else { |
| 276 |
|
| 277 |
decoder->progress_entry.u.literal.compression = AWS_HTTP_HEADER_COMPRESSION_NO_CACHE; |
| 278 |
decoder->progress_entry.u.literal.prefix_size = 4; |
| 279 |
decoder->progress_entry.state = HPACK_ENTRY_STATE_LITERAL_BEGIN; |
| 280 |
} |
| 281 |
} break; |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
case HPACK_ENTRY_STATE_INDEXED: { |
| 287 |
bool complete = false; |
| 288 |
uint64_t *index = &decoder->progress_entry.u.indexed.index; |
| 289 |
if (aws_hpack_decode_integer(decoder, to_decode, 7, index, &complete)) { |
| 290 |
return AWS_OP_ERR; |
| 291 |
} |
| 292 |
|
| 293 |
if (!complete) { |
| 294 |
break; |
| 295 |
} |
| 296 |
|
| 297 |
const struct aws_http_header *header = s_get_header_u64(decoder, *index); |
| 298 |
if (!header) { |
| 299 |
return AWS_OP_ERR; |
| 300 |
} |
| 301 |
|
| 302 |
result->type = AWS_HPACK_DECODE_T_HEADER_FIELD; |
| 303 |
result->data.header_field = *header; |
| 304 |
goto handle_complete; |
| 305 |
} break; |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
case HPACK_ENTRY_STATE_LITERAL_BEGIN: { |
| 316 |
struct hpack_progress_literal *literal = &decoder->progress_entry.u.literal; |
| 317 |
|
| 318 |
bool index_complete = false; |
| 319 |
if (aws_hpack_decode_integer( |
| 320 |
decoder, to_decode, literal->prefix_size, &literal->name_index, &index_complete)) { |
| 321 |
return AWS_OP_ERR; |
| 322 |
} |
| 323 |
|
| 324 |
if (!index_complete) { |
| 325 |
break; |
| 326 |
} |
| 327 |
|
| 328 |
if (literal->name_index == 0) { |
| 329 |
|
| 330 |
decoder->progress_entry.state = HPACK_ENTRY_STATE_LITERAL_NAME_STRING; |
| 331 |
break; |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
const struct aws_http_header *header = s_get_header_u64(decoder, literal->name_index); |
| 336 |
if (!header) { |
| 337 |
return AWS_OP_ERR; |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
if (aws_byte_buf_append_dynamic(&decoder->progress_entry.scratch, &header->name)) { |
| 343 |
return AWS_OP_ERR; |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
literal->name_length = header->name.len; |
| 349 |
decoder->progress_entry.state = HPACK_ENTRY_STATE_LITERAL_VALUE_STRING; |
| 350 |
} break; |
| 351 |
|
| 352 |
|
| 353 |
case HPACK_ENTRY_STATE_LITERAL_NAME_STRING: { |
| 354 |
bool string_complete = false; |
| 355 |
if (aws_hpack_decode_string(decoder, to_decode, &decoder->progress_entry.scratch, &string_complete)) { |
| 356 |
return AWS_OP_ERR; |
| 357 |
} |
| 358 |
|
| 359 |
if (!string_complete) { |
| 360 |
break; |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
decoder->progress_entry.u.literal.name_length = decoder->progress_entry.scratch.len; |
| 366 |
decoder->progress_entry.state = HPACK_ENTRY_STATE_LITERAL_VALUE_STRING; |
| 367 |
} break; |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
case HPACK_ENTRY_STATE_LITERAL_VALUE_STRING: { |
| 372 |
bool string_complete = false; |
| 373 |
if (aws_hpack_decode_string(decoder, to_decode, &decoder->progress_entry.scratch, &string_complete)) { |
| 374 |
return AWS_OP_ERR; |
| 375 |
} |
| 376 |
|
| 377 |
if (!string_complete) { |
| 378 |
break; |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
struct hpack_progress_literal *literal = &decoder->progress_entry.u.literal; |
| 383 |
|
| 384 |
|
| 385 |
struct aws_http_header header; |
| 386 |
header.value = aws_byte_cursor_from_buf(&decoder->progress_entry.scratch); |
| 387 |
header.name = aws_byte_cursor_advance(&header.value, literal->name_length); |
| 388 |
header.compression = literal->compression; |
| 389 |
|
| 390 |
|
| 391 |
if (literal->compression == AWS_HTTP_HEADER_COMPRESSION_USE_CACHE) { |
| 392 |
if (aws_hpack_insert_header(&decoder->context, &header)) { |
| 393 |
return AWS_OP_ERR; |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
result->type = AWS_HPACK_DECODE_T_HEADER_FIELD; |
| 398 |
result->data.header_field = header; |
| 399 |
goto handle_complete; |
| 400 |
} break; |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
case HPACK_ENTRY_STATE_DYNAMIC_TABLE_RESIZE: { |
| 405 |
uint64_t *size64 = &decoder->progress_entry.u.dynamic_table_resize.size; |
| 406 |
bool size_complete = false; |
| 407 |
if (aws_hpack_decode_integer(decoder, to_decode, 5, size64, &size_complete)) { |
| 408 |
return AWS_OP_ERR; |
| 409 |
} |
| 410 |
|
| 411 |
if (!size_complete) { |
| 412 |
break; |
| 413 |
} |
| 414 |
|
| 415 |
|
| 416 |
if (*size64 > decoder->dynamic_table_protocol_max_size_setting) { |
| 417 |
HPACK_LOG(ERROR, decoder, "Dynamic table update size is larger than the protocal setting"); |
| 418 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 419 |
} |
| 420 |
size_t size = (size_t)*size64; |
| 421 |
|
| 422 |
HPACK_LOGF(TRACE, decoder, "Dynamic table size update %zu", size); |
| 423 |
if (aws_hpack_resize_dynamic_table(&decoder->context, size)) { |
| 424 |
return AWS_OP_ERR; |
| 425 |
} |
| 426 |
|
| 427 |
result->type = AWS_HPACK_DECODE_T_DYNAMIC_TABLE_RESIZE; |
| 428 |
result->data.dynamic_table_resize = size; |
| 429 |
goto handle_complete; |
| 430 |
} break; |
| 431 |
|
| 432 |
default: { |
| 433 |
AWS_ASSERT(0 && "invalid state"); |
| 434 |
} break; |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
AWS_ASSERT(to_decode->len == 0); |
| 439 |
result->type = AWS_HPACK_DECODE_T_ONGOING; |
| 440 |
return AWS_OP_SUCCESS; |
| 441 |
|
| 442 |
handle_complete: |
| 443 |
AWS_ASSERT(result->type != AWS_HPACK_DECODE_T_ONGOING); |
| 444 |
decoder->progress_entry.state = HPACK_ENTRY_STATE_INIT; |
| 445 |
return AWS_OP_SUCCESS; |
| 446 |
} |