Debian Codegraph @

crt/aws-c-http/source/hpack_decoder.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
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 /* Used while decoding the header name & value, grows if necessary */
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 /* Return a byte with the N right-most bits masked.
51 * Ex: 2 -> 00000011 */
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 /* Read the first byte, and check whether this is it, or we need to continue */
78 uint8_t byte = 0;
79 bool succ = aws_byte_cursor_read_u8(to_decode, &byte);
80 AWS_FATAL_ASSERT(succ);
81
82 /* Cut the prefix */
83 byte &= prefix_mask;
84
85 /* No matter what, the first byte's value is always added to the integer */
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 /* Check if we're done */
107 if ((byte & 128) == 0) {
108 goto handle_complete;
109 }
110
111 /* Increment the bit count */
112 progress->bit_count += 7;
113
114 /* 7 Bits are expected to be used, so if we get to the point where any of
115 * those bits can't be used it's a decoding error */
116 if (progress->bit_count > 64 - 7) {
117 return aws_raise_error(AWS_ERROR_OVERFLOW_DETECTED);
118 }
119 } break;
120 }
121 }
122
123 /* Fell out of data loop, must need more data */
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 /* Do init stuff */
150 progress->state = HPACK_STRING_STATE_LENGTH;
151 progress->use_huffman = *to_decode->ptr >> 7;
152 aws_huffman_decoder_reset(&decoder->huffman_decoder);
153 /* fallthrough, since we didn't consume any data */
154 }
155 /* FALLTHRU */
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 /* Take either as much data as we need, or as much as we can */
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 /* Decoder should consume all bytes we feed it.
191 * EOS (end-of-string) symbol could stop it early, but HPACK says to treat EOS as error. */
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 /* If whole length consumed, we're done */
203 if (progress->length == 0) {
204 /* #TODO Validate any padding bits left over in final byte of string.
205 * "A padding not corresponding to the most significant bits of the
206 * code for the EOS symbol MUST be treated as a decoding error" */
207
208 /* #TODO impose limits on string length */
209
210 goto handle_complete;
211 }
212 } break;
213 }
214 }
215
216 handle_ongoing:
217 /* Fell out of to_decode loop, must still be in progress */
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 /* Implements RFC-7541 Section 6 - Binary Format */
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 /* Run state machine until we decode a complete entry.
240 * Every state requires data, so we can simply loop until no more data available. */
241 while (to_decode->len) {
242 switch (decoder->progress_entry.state) {
243
244 case HPACK_ENTRY_STATE_INIT: {
245 /* Reset entry */
246 AWS_ZERO_STRUCT(decoder->progress_entry.u);
247 decoder->progress_entry.scratch.len = 0;
248
249 /* Determine next state by looking at first few bits of the next byte:
250 * 1xxxxxxx: Indexed Header Field Representation
251 * 01xxxxxx: Literal Header Field with Incremental Indexing
252 * 001xxxxx: Dynamic Table Size Update
253 * 0001xxxx: Literal Header Field Never Indexed
254 * 0000xxxx: Literal Header Field without Indexing */
255 uint8_t first_byte = to_decode->ptr[0];
256 if (first_byte & (1 << 7)) {
257 /* 1xxxxxxx: Indexed Header Field Representation */
258 decoder->progress_entry.state = HPACK_ENTRY_STATE_INDEXED;
259
260 } else if (first_byte & (1 << 6)) {
261 /* 01xxxxxx: Literal Header Field with Incremental Indexing */
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 /* 001xxxxx: Dynamic Table Size Update */
268 decoder->progress_entry.state = HPACK_ENTRY_STATE_DYNAMIC_TABLE_RESIZE;
269
270 } else if (first_byte & (1 << 4)) {
271 /* 0001xxxx: Literal Header Field Never Indexed */
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 /* 0000xxxx: Literal Header Field without Indexing */
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 /* RFC-7541 6.1. Indexed Header Field Representation.
284 * Decode one integer, which is an index into the table.
285 * Result is the header name and value stored there. */
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 /* RFC-7541 6.2. Literal Header Field Representation.
308 * We use multiple states to decode a literal...
309 * The header-name MAY come from the table and MAY be encoded as a string.
310 * The header-value is ALWAYS encoded as a string.
311 *
312 * This BEGIN state decodes one integer.
313 * If it's non-zero, then it's the index in the table where we'll get the header-name from.
314 * If it's zero, then we move to the HEADER_NAME state and decode header-name as a string instead */
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 /* Index 0 means header-name is not in table. Need to decode header-name as a string instead */
330 decoder->progress_entry.state = HPACK_ENTRY_STATE_LITERAL_NAME_STRING;
331 break;
332 }
333
334 /* Otherwise we found index of header-name in table. */
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 /* Store the name in scratch. We don't just keep a pointer to it because it could be
341 * evicted from the dynamic table later, when we save the literal. */
342 if (aws_byte_buf_append_dynamic(&decoder->progress_entry.scratch, &header->name)) {
343 return AWS_OP_ERR;
344 }
345
346 /* Move on to decoding header-value.
347 * Value will also decode into the scratch, so save where name ends. */
348 literal->name_length = header->name.len;
349 decoder->progress_entry.state = HPACK_ENTRY_STATE_LITERAL_VALUE_STRING;
350 } break;
351
352 /* We only end up in this state if header-name is encoded as string. */
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 /* Done decoding name string! Move on to decoding the value string.
364 * Value will also decode into the scratch, so save where name ends. */
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 /* Final state for "literal" entries.
370 * Decode the header-value string, then deliver the results. */
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 /* Done decoding value string. Done decoding entry. */
382 struct hpack_progress_literal *literal = &decoder->progress_entry.u.literal;
383
384 /* Set up a header with name and value (which are packed one after the other in scratch) */
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 /* Save to table if necessary */
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 /* RFC-7541 6.3. Dynamic Table Size Update
403 * Read one integer, which is the new maximum size for the dynamic table. */
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 /* The new maximum size MUST be lower than or equal to the limit determined by the protocol using HPACK.
415 * A value that exceeds this limit MUST be treated as a decoding error. */
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 }