Debian Codegraph @

crt/aws-c-common/source/cbor.c

raw symbols SCIP
1 #include "external/libcbor/cbor.h"
2 #include <aws/common/cbor.h>
3
4 #include <aws/common/array_list.h>
5 #include <aws/common/logging.h>
6 #include <aws/common/private/byte_buf.h>
7 #include <aws/common/private/external_module_impl.h>
8 #include <float.h>
9 #include <inttypes.h>
10 #include <math.h>
11
12 static bool s_aws_cbor_module_initialized = false;
13
14 const static size_t s_cbor_element_width_64bit = 9;
15 const static size_t s_cbor_element_width_32bit = 5;
16
17 /* Resource limits to prevent hostile input from causing excessive allocation or stack exhaustion */
18 const static uint64_t s_cbor_max_container_size = 10000;
19 const static size_t s_cbor_max_nesting_depth = 128;
20
21 enum s_cbor_simple_val {
22 AWS_CBOR_SIMPLE_VAL_FALSE = 20,
23 AWS_CBOR_SIMPLE_VAL_TRUE = 21,
24 AWS_CBOR_SIMPLE_VAL_NULL = 22,
25 AWS_CBOR_SIMPLE_VAL_UNDEFINED = 23,
26 AWS_CBOR_SIMPLE_VAL_BREAK = 31,
27 };
28
29 void aws_cbor_module_init(struct aws_allocator *allocator) {
30 (void)allocator;
31 if (!s_aws_cbor_module_initialized) {
32 /* Not allow any allocation from libcbor */
33 cbor_set_allocs(NULL, NULL, NULL);
34 s_aws_cbor_module_initialized = true;
35 }
36 }
37
38 void aws_cbor_module_cleanup(void) {
39 if (s_aws_cbor_module_initialized) {
40 s_aws_cbor_module_initialized = false;
41 }
42 }
43
44 /* Return c-string for aws_cbor_type */
45 const char *aws_cbor_type_cstr(enum aws_cbor_type type) {
46 /* clang-format off */
47 switch (type) {
48 case (AWS_CBOR_TYPE_UINT): return "AWS_CBOR_TYPE_UINT";
49 case (AWS_CBOR_TYPE_NEGINT): return "AWS_CBOR_TYPE_NEGINT";
50 case (AWS_CBOR_TYPE_FLOAT): return "AWS_CBOR_TYPE_FLOAT";
51 case (AWS_CBOR_TYPE_BYTES): return "AWS_CBOR_TYPE_BYTES";
52 case (AWS_CBOR_TYPE_TEXT): return "AWS_CBOR_TYPE_TEXT";
53 case (AWS_CBOR_TYPE_ARRAY_START): return "AWS_CBOR_TYPE_ARRAY_START";
54 case (AWS_CBOR_TYPE_MAP_START): return "AWS_CBOR_TYPE_MAP_START";
55 case (AWS_CBOR_TYPE_TAG): return "AWS_CBOR_TYPE_TAG";
56 case (AWS_CBOR_TYPE_BOOL): return "AWS_CBOR_TYPE_BOOL";
57 case (AWS_CBOR_TYPE_NULL): return "AWS_CBOR_TYPE_NULL";
58 case (AWS_CBOR_TYPE_UNDEFINED): return "AWS_CBOR_TYPE_UNDEFINED";
59 case (AWS_CBOR_TYPE_BREAK): return "AWS_CBOR_TYPE_BREAK";
60 case (AWS_CBOR_TYPE_INDEF_BYTES_START): return "AWS_CBOR_TYPE_INDEF_BYTES_START";
61 case (AWS_CBOR_TYPE_INDEF_TEXT_START): return "AWS_CBOR_TYPE_INDEF_TEXT_START";
62 case (AWS_CBOR_TYPE_INDEF_ARRAY_START): return "AWS_CBOR_TYPE_INDEF_ARRAY_START";
63 case (AWS_CBOR_TYPE_INDEF_MAP_START): return "AWS_CBOR_TYPE_INDEF_MAP_START";
64 default: return "<UNKNOWN TYPE>";
65 }
66 /* clang-format on */
67 }
68
69 /*******************************************************************************
70 * ENCODE
71 ******************************************************************************/
72
73 struct aws_cbor_encoder {
74 struct aws_allocator *allocator;
75 struct aws_byte_buf encoded_buf;
76 };
77
78 struct aws_cbor_encoder *aws_cbor_encoder_new(struct aws_allocator *allocator) {
79 struct aws_cbor_encoder *encoder = aws_mem_calloc(allocator, 1, sizeof(struct aws_cbor_encoder));
80 encoder->allocator = allocator;
81 aws_byte_buf_init(&encoder->encoded_buf, allocator, 256);
82
83 return encoder;
84 }
85
86 struct aws_cbor_encoder *aws_cbor_encoder_destroy(struct aws_cbor_encoder *encoder) {
87 aws_byte_buf_clean_up(&encoder->encoded_buf);
88 aws_mem_release(encoder->allocator, encoder);
89 return NULL;
90 }
91
92 struct aws_byte_cursor aws_cbor_encoder_get_encoded_data(const struct aws_cbor_encoder *encoder) {
93 return aws_byte_cursor_from_buf(&encoder->encoded_buf);
94 }
95
96 void aws_cbor_encoder_reset(struct aws_cbor_encoder *encoder) {
97 aws_byte_buf_reset(&encoder->encoded_buf, false);
98 }
99
100 static uint8_t *s_get_encoder_current_position(struct aws_cbor_encoder *encoder) {
101 return encoder->encoded_buf.buffer + encoder->encoded_buf.len;
102 }
103
104 static size_t s_get_encoder_remaining_len(struct aws_cbor_encoder *encoder) {
105 return encoder->encoded_buf.capacity - encoder->encoded_buf.len;
106 }
107
108 /**
109 * @brief Marcos to ensure the encoder have enough space to encode the value into the buffer using given `fn`, and then
110 * encode it.
111 */
112 #define ENCODE_THROUGH_LIBCBOR(encoder, length_to_reserve, value, fn) \
113 do { \
114 int error = aws_byte_buf_reserve_smart_relative(&(encoder)->encoded_buf, length_to_reserve); \
115 (void)error; \
116 AWS_FATAL_ASSERT(error == AWS_ERROR_SUCCESS); \
117 size_t encoded_len = fn(value, s_get_encoder_current_position(encoder), s_get_encoder_remaining_len(encoder)); \
118 AWS_FATAL_ASSERT((encoded_len) != 0); \
119 (encoder)->encoded_buf.len += (encoded_len); \
120 } while (false)
121
122 void aws_cbor_encoder_write_uint(struct aws_cbor_encoder *encoder, uint64_t value) {
123 ENCODE_THROUGH_LIBCBOR(encoder, s_cbor_element_width_64bit, value, cbor_encode_uint);
124 }
125
126 void aws_cbor_encoder_write_negint(struct aws_cbor_encoder *encoder, uint64_t value) {
127 ENCODE_THROUGH_LIBCBOR(encoder, s_cbor_element_width_64bit, value, cbor_encode_negint);
128 }
129
130 void aws_cbor_encoder_write_single_float(struct aws_cbor_encoder *encoder, float value) {
131 ENCODE_THROUGH_LIBCBOR(encoder, s_cbor_element_width_32bit, value, cbor_encode_single);
132 }
133
134 void aws_cbor_encoder_write_float(struct aws_cbor_encoder *encoder, double value) {
135 /**
136 * As suggested by AWS SDK SEP, write the float value as small as possible. But, do not encode to half-float.
137 * Convert the float value to integer if the conversion will not cause any precision loss.
138 */
139 if (!isfinite(value)) {
140 /* For special value: NAN/INFINITY, type cast to float and encode into single float. */
141 aws_cbor_encoder_write_single_float(encoder, (float)value);
142 return;
143 }
144 /* Conversation from int to floating-type is implementation defined if loss of precision */
145 if (value <= (double)INT64_MAX && value >= (double)INT64_MIN) {
146 /**
147 * A prvalue of a floating point type can be converted to a prvalue of an integer type. The conversion
148 * truncates; that is, the fractional part is discarded. The behavior is undefined if the truncated value cannot
149 * be represented in the destination type.
150 * Check against the INT64 range to avoid undefined behavior
151 *
152 * Comparing against INT64_MAX instead of UINT64_MAX to simplify the code, which may loss the opportunity to
153 * convert the UINT64 range from double to uint64_t. However, converting double to uint64_t will not benefit the
154 * total length encoded.
155 **/
156 int64_t int_value = (int64_t)value;
157 if (value == (double)int_value) {
158 if (int_value < 0) {
159 aws_cbor_encoder_write_negint(encoder, (uint64_t)(-1 - int_value));
160 } else {
161 aws_cbor_encoder_write_uint(encoder, (uint64_t)(int_value));
162 }
163 return;
164 }
165 }
166 if (value <= FLT_MAX && value >= -FLT_MAX) {
167 /* Only try to convert the value within the range of float. */
168 float float_value = (float)value;
169 double converted_value = (double)float_value;
170 /* Try to cast a round trip to detect any precision loss. */
171 if (value == converted_value) {
172 aws_cbor_encoder_write_single_float(encoder, float_value);
173 return;
174 }
175 }
176
177 ENCODE_THROUGH_LIBCBOR(encoder, s_cbor_element_width_64bit, value, cbor_encode_double);
178 }
179
180 void aws_cbor_encoder_write_map_start(struct aws_cbor_encoder *encoder, size_t number_entries) {
181 ENCODE_THROUGH_LIBCBOR(encoder, s_cbor_element_width_64bit, number_entries, cbor_encode_map_start);
182 }
183
184 void aws_cbor_encoder_write_tag(struct aws_cbor_encoder *encoder, uint64_t tag_number) {
185 ENCODE_THROUGH_LIBCBOR(encoder, s_cbor_element_width_64bit, tag_number, cbor_encode_tag);
186 }
187
188 void aws_cbor_encoder_write_array_start(struct aws_cbor_encoder *encoder, size_t number_entries) {
189 ENCODE_THROUGH_LIBCBOR(encoder, s_cbor_element_width_64bit, number_entries, cbor_encode_array_start);
190 }
191
192 void aws_cbor_encoder_write_bytes(struct aws_cbor_encoder *encoder, struct aws_byte_cursor from) {
193 /* Reserve the bytes for the byte string start cbor item and the actual bytes */
194 /* Encode the first cbor item for byte string */
195 ENCODE_THROUGH_LIBCBOR(encoder, s_cbor_element_width_64bit + from.len, from.len, cbor_encode_bytestring_start);
196 /* Append the actual bytes to follow the cbor item */
197 aws_byte_buf_append(&encoder->encoded_buf, &from);
198 }
199
200 void aws_cbor_encoder_write_text(struct aws_cbor_encoder *encoder, struct aws_byte_cursor from) {
201 /* Reserve the bytes for the byte string start cbor item and the actual string */
202 /* Encode the first cbor item for byte string */
203 ENCODE_THROUGH_LIBCBOR(encoder, s_cbor_element_width_64bit + from.len, from.len, cbor_encode_string_start);
204 /* Append the actual string to follow the cbor item */
205 aws_byte_buf_append(&encoder->encoded_buf, &from);
206 }
207
208 void aws_cbor_encoder_write_bool(struct aws_cbor_encoder *encoder, bool value) {
209 /* Major type 7 (simple), value 20 (false) and 21 (true) */
210 uint8_t ctrl_value = value == true ? AWS_CBOR_SIMPLE_VAL_TRUE : AWS_CBOR_SIMPLE_VAL_FALSE;
211 ENCODE_THROUGH_LIBCBOR(encoder, 1, ctrl_value, cbor_encode_ctrl);
212 }
213
214 void aws_cbor_encoder_write_null(struct aws_cbor_encoder *encoder) {
215 /* Major type 7 (simple), value 22 (null) */
216 ENCODE_THROUGH_LIBCBOR(encoder, 1, AWS_CBOR_SIMPLE_VAL_NULL /*null*/, cbor_encode_ctrl);
217 }
218
219 void aws_cbor_encoder_write_undefined(struct aws_cbor_encoder *encoder) {
220 /* Major type 7 (simple), value 23 (undefined) */
221 ENCODE_THROUGH_LIBCBOR(encoder, 1, AWS_CBOR_SIMPLE_VAL_UNDEFINED /*undefined*/, cbor_encode_ctrl);
222 }
223
224 static void s_cbor_encoder_write_type_only(struct aws_cbor_encoder *encoder, enum aws_cbor_type type) {
225 /* All inf start takes 1 byte only */
226 aws_byte_buf_reserve_smart_relative(&encoder->encoded_buf, 1);
227 size_t encoded_len = 0;
228 switch (type) {
229 case AWS_CBOR_TYPE_INDEF_BYTES_START:
230 encoded_len = cbor_encode_indef_bytestring_start(
231 s_get_encoder_current_position(encoder), s_get_encoder_remaining_len(encoder));
232 break;
233 case AWS_CBOR_TYPE_INDEF_TEXT_START:
234 encoded_len = cbor_encode_indef_string_start(
235 s_get_encoder_current_position(encoder), s_get_encoder_remaining_len(encoder));
236 break;
237 case AWS_CBOR_TYPE_INDEF_ARRAY_START:
238 encoded_len = cbor_encode_indef_array_start(
239 s_get_encoder_current_position(encoder), s_get_encoder_remaining_len(encoder));
240 break;
241 case AWS_CBOR_TYPE_INDEF_MAP_START:
242 encoded_len = cbor_encode_indef_map_start(
243 s_get_encoder_current_position(encoder), s_get_encoder_remaining_len(encoder));
244 break;
245 case AWS_CBOR_TYPE_BREAK:
246 encoded_len =
247 cbor_encode_break(s_get_encoder_current_position(encoder), s_get_encoder_remaining_len(encoder));
248 break;
249
250 default:
251 AWS_ASSERT(false);
252 break;
253 }
254 AWS_ASSERT(encoded_len == 1);
255 encoder->encoded_buf.len += encoded_len;
256 }
257 void aws_cbor_encoder_write_indef_bytes_start(struct aws_cbor_encoder *encoder) {
258 s_cbor_encoder_write_type_only(encoder, AWS_CBOR_TYPE_INDEF_BYTES_START);
259 }
260
261 void aws_cbor_encoder_write_indef_text_start(struct aws_cbor_encoder *encoder) {
262 s_cbor_encoder_write_type_only(encoder, AWS_CBOR_TYPE_INDEF_TEXT_START);
263 }
264
265 void aws_cbor_encoder_write_indef_array_start(struct aws_cbor_encoder *encoder) {
266 s_cbor_encoder_write_type_only(encoder, AWS_CBOR_TYPE_INDEF_ARRAY_START);
267 }
268
269 void aws_cbor_encoder_write_indef_map_start(struct aws_cbor_encoder *encoder) {
270 s_cbor_encoder_write_type_only(encoder, AWS_CBOR_TYPE_INDEF_MAP_START);
271 }
272
273 void aws_cbor_encoder_write_break(struct aws_cbor_encoder *encoder) {
274 s_cbor_encoder_write_type_only(encoder, AWS_CBOR_TYPE_BREAK);
275 }
276
277 /*******************************************************************************
278 * DECODE
279 ******************************************************************************/
280
281 struct aws_cbor_decoder_context {
282 enum aws_cbor_type type;
283
284 /* All the values only valid when the type is set to corresponding type. */
285 union {
286 uint64_t unsigned_int_val;
287 uint64_t negative_int_val;
288 double float_val;
289 uint64_t tag_val;
290 bool boolean_val;
291 struct aws_byte_cursor bytes_val;
292 struct aws_byte_cursor text_val;
293 uint64_t map_start;
294 uint64_t array_start;
295 } u;
296 };
297
298 struct aws_cbor_decoder {
299 struct aws_allocator *allocator;
300
301 struct aws_byte_cursor src;
302
303 struct aws_cbor_decoder_context cached_context;
304
305 /* Number of bytes from src consumed by peek (cached but not yet popped) */
306 size_t cached_bytes_consumed;
307
308 /* Error code during decoding. Fail the decoding process without recovering, */
309 int error_code;
310
311 /* Current nesting depth for recursive decoding */
312 size_t current_depth;
313 };
314
315 struct aws_cbor_decoder *aws_cbor_decoder_new(struct aws_allocator *allocator, struct aws_byte_cursor src) {
316
317 struct aws_cbor_decoder *decoder = aws_mem_calloc(allocator, 1, sizeof(struct aws_cbor_decoder));
318 decoder->allocator = allocator;
319 decoder->src = src;
320 decoder->cached_context.type = AWS_CBOR_TYPE_UNKNOWN;
321 return decoder;
322 }
323
324 struct aws_cbor_decoder *aws_cbor_decoder_destroy(struct aws_cbor_decoder *decoder) {
325 aws_mem_release(decoder->allocator, decoder);
326 return NULL;
327 }
328
329 size_t aws_cbor_decoder_get_remaining_length(const struct aws_cbor_decoder *decoder) {
330 return decoder->src.len;
331 }
332
333 size_t aws_cbor_decoder_get_unconsumed_length(const struct aws_cbor_decoder *decoder) {
334 if (decoder->cached_context.type != AWS_CBOR_TYPE_UNKNOWN) {
335 /* peek_type decodes the next element and advances src, but from the caller's perspective
336 * those bytes haven't been consumed yet (the item hasn't been popped). Add them back. */
337 return decoder->src.len + decoder->cached_bytes_consumed;
338 }
339 return decoder->src.len;
340 }
341
342 void aws_cbor_decoder_reset_src(struct aws_cbor_decoder *decoder, struct aws_byte_cursor src) {
343 decoder->src = src;
344 /* Reset all cached state */
345 decoder->cached_context.type = AWS_CBOR_TYPE_UNKNOWN;
346 decoder->error_code = AWS_ERROR_SUCCESS;
347 }
348
349 #define LIBCBOR_VALUE_CALLBACK(field, callback_type, cbor_type) \
350 static void s_##field##_callback(void *ctx, callback_type val) { \
351 struct aws_cbor_decoder *decoder = ctx; \
352 AWS_ASSERT((decoder)->cached_context.type == AWS_CBOR_TYPE_UNKNOWN); \
353 (decoder)->cached_context.u.field = val; \
354 (decoder)->cached_context.type = cbor_type; \
355 }
356
357 LIBCBOR_VALUE_CALLBACK(unsigned_int_val, uint64_t, AWS_CBOR_TYPE_UINT)
358 LIBCBOR_VALUE_CALLBACK(negative_int_val, uint64_t, AWS_CBOR_TYPE_NEGINT)
359 LIBCBOR_VALUE_CALLBACK(boolean_val, bool, AWS_CBOR_TYPE_BOOL)
360 LIBCBOR_VALUE_CALLBACK(float_val, double, AWS_CBOR_TYPE_FLOAT)
361 LIBCBOR_VALUE_CALLBACK(map_start, uint64_t, AWS_CBOR_TYPE_MAP_START)
362 LIBCBOR_VALUE_CALLBACK(array_start, uint64_t, AWS_CBOR_TYPE_ARRAY_START)
363 LIBCBOR_VALUE_CALLBACK(tag_val, uint64_t, AWS_CBOR_TYPE_TAG)
364
365 static void s_uint8_callback(void *ctx, uint8_t data) {
366 s_unsigned_int_val_callback(ctx, (uint64_t)data);
367 }
368
369 static void s_uint16_callback(void *ctx, uint16_t data) {
370 s_unsigned_int_val_callback(ctx, (uint64_t)data);
371 }
372
373 static void s_uint32_callback(void *ctx, uint32_t data) {
374 s_unsigned_int_val_callback(ctx, (uint64_t)data);
375 }
376
377 static void s_negint8_callback(void *ctx, uint8_t data) {
378 s_negative_int_val_callback(ctx, (uint64_t)data);
379 }
380
381 static void s_negint16_callback(void *ctx, uint16_t data) {
382 s_negative_int_val_callback(ctx, (uint64_t)data);
383 }
384
385 static void s_negint32_callback(void *ctx, uint32_t data) {
386 s_negative_int_val_callback(ctx, (uint64_t)data);
387 }
388
389 static void s_float_callback(void *ctx, float data) {
390 s_float_val_callback(ctx, (double)data);
391 }
392
393 static void s_bytes_callback(void *ctx, const unsigned char *cbor_data, uint64_t length) {
394 struct aws_cbor_decoder *decoder = ctx;
395 AWS_ASSERT((decoder)->cached_context.type == AWS_CBOR_TYPE_UNKNOWN);
396 if (length > SIZE_MAX) {
397 AWS_LOGF_ERROR(AWS_LS_COMMON_CBOR, "Decoded a bytes with %" PRIu64 " bytes causing overflow .", length);
398 decoder->error_code = AWS_ERROR_OVERFLOW_DETECTED;
399 return;
400 }
401 decoder->cached_context.type = AWS_CBOR_TYPE_BYTES;
402 decoder->cached_context.u.bytes_val.ptr = (uint8_t *)cbor_data;
403 decoder->cached_context.u.bytes_val.len = (size_t)length;
404 }
405
406 static void s_str_callback(void *ctx, const unsigned char *cbor_data, uint64_t length) {
407 struct aws_cbor_decoder *decoder = ctx;
408 AWS_ASSERT((decoder)->cached_context.type == AWS_CBOR_TYPE_UNKNOWN);
409 if (length > SIZE_MAX) {
410 AWS_LOGF_ERROR(AWS_LS_COMMON_CBOR, "Decoded a string with %" PRIu64 " bytes causing overflow .", length);
411 decoder->error_code = AWS_ERROR_OVERFLOW_DETECTED;
412 return;
413 }
414 decoder->cached_context.type = AWS_CBOR_TYPE_TEXT;
415 decoder->cached_context.u.text_val.ptr = (uint8_t *)cbor_data;
416 decoder->cached_context.u.text_val.len = (size_t)length;
417 }
418
419 #define LIBCBOR_SIMPLE_CALLBACK(field, cbor_type) \
420 static void s_##field##_callback(void *ctx) { \
421 struct aws_cbor_decoder *decoder = ctx; \
422 AWS_ASSERT((decoder)->cached_context.type == AWS_CBOR_TYPE_UNKNOWN); \
423 (decoder)->cached_context.type = cbor_type; \
424 }
425
426 LIBCBOR_SIMPLE_CALLBACK(inf_bytes, AWS_CBOR_TYPE_INDEF_BYTES_START)
427 LIBCBOR_SIMPLE_CALLBACK(inf_str, AWS_CBOR_TYPE_INDEF_TEXT_START)
428 LIBCBOR_SIMPLE_CALLBACK(inf_array, AWS_CBOR_TYPE_INDEF_ARRAY_START)
429 LIBCBOR_SIMPLE_CALLBACK(inf_map, AWS_CBOR_TYPE_INDEF_MAP_START)
430
431 LIBCBOR_SIMPLE_CALLBACK(inf_break, AWS_CBOR_TYPE_BREAK)
432 LIBCBOR_SIMPLE_CALLBACK(undefined, AWS_CBOR_TYPE_UNDEFINED)
433 LIBCBOR_SIMPLE_CALLBACK(null, AWS_CBOR_TYPE_NULL)
434
435 static struct cbor_callbacks s_callbacks = {
436 /** Unsigned int */
437 .uint64 = s_unsigned_int_val_callback,
438 /** Unsigned int */
439 .uint32 = s_uint32_callback,
440 /** Unsigned int */
441 .uint16 = s_uint16_callback,
442 /** Unsigned int */
443 .uint8 = s_uint8_callback,
444
445 /** Negative int */
446 .negint64 = s_negative_int_val_callback,
447 /** Negative int */
448 .negint32 = s_negint32_callback,
449 /** Negative int */
450 .negint16 = s_negint16_callback,
451 /** Negative int */
452 .negint8 = s_negint8_callback,
453
454 /** Indefinite byte string start */
455 .byte_string_start = s_inf_bytes_callback,
456 /** Definite byte string */
457 .byte_string = s_bytes_callback,
458
459 /** Definite string */
460 .string = s_str_callback,
461 /** Indefinite string start */
462 .string_start = s_inf_str_callback,
463
464 /** Definite array */
465 .indef_array_start = s_inf_array_callback,
466 /** Indefinite array */
467 .array_start = s_array_start_callback,
468
469 /** Definite map */
470 .indef_map_start = s_inf_map_callback,
471 /** Indefinite map */
472 .map_start = s_map_start_callback,
473
474 /** Tags */
475 .tag = s_tag_val_callback,
476
477 /** Half float */
478 .float2 = s_float_callback,
479 /** Single float */
480 .float4 = s_float_callback,
481 /** Double float */
482 .float8 = s_float_val_callback,
483 /** Undef */
484 .undefined = s_undefined_callback,
485 /** Null */
486 .null = s_null_callback,
487 /** Bool */
488 .boolean = s_boolean_val_callback,
489
490 /** Indefinite item break */
491 .indef_break = s_inf_break_callback,
492 };
493
494 /**
495 * decode the next element to the cached_content.
496 */
497 static int s_cbor_decode_next_element(struct aws_cbor_decoder *decoder) {
498 struct cbor_decoder_result result = cbor_stream_decode(decoder->src.ptr, decoder->src.len, &s_callbacks, decoder);
499 switch (result.status) {
500 case CBOR_DECODER_NEDATA:
501 AWS_LOGF_ERROR(
502 AWS_LS_COMMON_CBOR,
503 "The decoder doesn't have enough data to decode the next element. At least %zu bytes more needed.",
504 result.required);
505 decoder->error_code = AWS_ERROR_INVALID_CBOR;
506 break;
507 case CBOR_DECODER_ERROR:
508 AWS_LOGF_ERROR(AWS_LS_COMMON_CBOR, "The cbor data is malformed to decode.");
509 decoder->error_code = AWS_ERROR_INVALID_CBOR;
510 break;
511 default:
512 break;
513 }
514
515 if (decoder->error_code) {
516 /* Error happened during decoding */
517 return aws_raise_error(decoder->error_code);
518 }
519
520 aws_byte_cursor_advance(&decoder->src, result.read);
521 decoder->cached_bytes_consumed = result.read;
522
523 return AWS_OP_SUCCESS;
524 }
525
526 /**
527 * Defines a static internal function: s_cbor_decoder_pop_next_##field
528 * that decodes the next element, checks type, and returns the value.
529 */
530 #define S_GET_NEXT_ITEM(field, out_type, expected_cbor_type) \
531 /* NOLINTNEXTLINE(bugprone-macro-parentheses) */ \
532 static int s_cbor_decoder_pop_next_##field(struct aws_cbor_decoder *decoder, out_type *out) { \
533 if ((decoder)->error_code) { \
534 return aws_raise_error((decoder)->error_code); \
535 } \
536 if ((decoder)->cached_context.type != AWS_CBOR_TYPE_UNKNOWN) { \
537 goto decode_done; \
538 } \
539 if (s_cbor_decode_next_element(decoder)) { \
540 return AWS_OP_ERR; \
541 } \
542 decode_done: \
543 if ((decoder)->cached_context.type != (expected_cbor_type)) { \
544 AWS_LOGF_ERROR( \
545 AWS_LS_COMMON_CBOR, \
546 "The decoder got unexpected type: %d (%s), while expecting type: %d (%s).", \
547 (decoder)->cached_context.type, \
548 aws_cbor_type_cstr((decoder)->cached_context.type), \
549 (expected_cbor_type), \
550 aws_cbor_type_cstr(expected_cbor_type)); \
551 return aws_raise_error(AWS_ERROR_CBOR_UNEXPECTED_TYPE); \
552 } else { \
553 (decoder)->cached_context.type = AWS_CBOR_TYPE_UNKNOWN; \
554 *out = (decoder)->cached_context.u.field; \
555 } \
556 return AWS_OP_SUCCESS; \
557 }
558
559 /**
560 * Defines the public API function: aws_cbor_decoder_pop_next_##field
561 * that simply calls the static internal implementation.
562 */
563 #define GET_NEXT_ITEM(field, out_type) \
564 /* NOLINTNEXTLINE(bugprone-macro-parentheses) */ \
565 int aws_cbor_decoder_pop_next_##field(struct aws_cbor_decoder *decoder, out_type *out) { \
566 return s_cbor_decoder_pop_next_##field(decoder, out); \
567 }
568
569 S_GET_NEXT_ITEM(unsigned_int_val, uint64_t, AWS_CBOR_TYPE_UINT)
570 S_GET_NEXT_ITEM(negative_int_val, uint64_t, AWS_CBOR_TYPE_NEGINT)
571 S_GET_NEXT_ITEM(float_val, double, AWS_CBOR_TYPE_FLOAT)
572 S_GET_NEXT_ITEM(boolean_val, bool, AWS_CBOR_TYPE_BOOL)
573 S_GET_NEXT_ITEM(text_val, struct aws_byte_cursor, AWS_CBOR_TYPE_TEXT)
574 S_GET_NEXT_ITEM(bytes_val, struct aws_byte_cursor, AWS_CBOR_TYPE_BYTES)
575 S_GET_NEXT_ITEM(tag_val, uint64_t, AWS_CBOR_TYPE_TAG)
576 S_GET_NEXT_ITEM(array_start, uint64_t, AWS_CBOR_TYPE_ARRAY_START)
577 S_GET_NEXT_ITEM(map_start, uint64_t, AWS_CBOR_TYPE_MAP_START)
578
579 GET_NEXT_ITEM(unsigned_int_val, uint64_t)
580 GET_NEXT_ITEM(negative_int_val, uint64_t)
581 GET_NEXT_ITEM(float_val, double)
582 GET_NEXT_ITEM(boolean_val, bool)
583 GET_NEXT_ITEM(text_val, struct aws_byte_cursor)
584 GET_NEXT_ITEM(bytes_val, struct aws_byte_cursor)
585 GET_NEXT_ITEM(tag_val, uint64_t)
586
587 /* array_start and map_start public wrappers add container size check */
588 int aws_cbor_decoder_pop_next_array_start(struct aws_cbor_decoder *decoder, uint64_t *out) {
589 if (s_cbor_decoder_pop_next_array_start(decoder, out)) {
590 return AWS_OP_ERR;
591 }
592 if (*out > s_cbor_max_container_size) {
593 AWS_LOGF_ERROR(
594 AWS_LS_COMMON_CBOR,
595 "Array size %" PRIu64 " exceeds maximum allowed %" PRIu64 ".",
596 *out,
597 (uint64_t)s_cbor_max_container_size);
598 decoder->error_code = AWS_ERROR_CBOR_RESOURCE_LIMIT_EXCEEDED;
599 return aws_raise_error(AWS_ERROR_CBOR_RESOURCE_LIMIT_EXCEEDED);
600 }
601 return AWS_OP_SUCCESS;
602 }
603
604 int aws_cbor_decoder_pop_next_map_start(struct aws_cbor_decoder *decoder, uint64_t *out) {
605 if (s_cbor_decoder_pop_next_map_start(decoder, out)) {
606 return AWS_OP_ERR;
607 }
608 if (*out > s_cbor_max_container_size) {
609 AWS_LOGF_ERROR(
610 AWS_LS_COMMON_CBOR,
611 "Map size %" PRIu64 " exceeds maximum allowed %" PRIu64 ".",
612 *out,
613 (uint64_t)s_cbor_max_container_size);
614 decoder->error_code = AWS_ERROR_CBOR_RESOURCE_LIMIT_EXCEEDED;
615 return aws_raise_error(AWS_ERROR_CBOR_RESOURCE_LIMIT_EXCEEDED);
616 }
617 return AWS_OP_SUCCESS;
618 }
619
620 int aws_cbor_decoder_peek_type(struct aws_cbor_decoder *decoder, enum aws_cbor_type *out_type) {
621 if (decoder->error_code) {
622 /* Error happened during decoding */
623 return aws_raise_error(decoder->error_code);
624 }
625
626 if (decoder->cached_context.type != AWS_CBOR_TYPE_UNKNOWN) {
627 /* There was a cached context, return the type. */
628 *out_type = decoder->cached_context.type;
629 return AWS_OP_SUCCESS;
630 }
631
632 /* Decode */
633 if (s_cbor_decode_next_element(decoder)) {
634 return AWS_OP_ERR;
635 }
636 *out_type = decoder->cached_context.type;
637 return AWS_OP_SUCCESS;
638 }
639
640 int aws_cbor_decoder_consume_next_whole_data_item(struct aws_cbor_decoder *decoder) {
641 if (decoder->error_code) {
642 return aws_raise_error(decoder->error_code);
643 }
644
645 if (++decoder->current_depth > s_cbor_max_nesting_depth) {
646 --decoder->current_depth;
647 AWS_LOGF_ERROR(AWS_LS_COMMON_CBOR, "Nesting depth exceeds maximum allowed %zu.", s_cbor_max_nesting_depth);
648 decoder->error_code = AWS_ERROR_CBOR_RESOURCE_LIMIT_EXCEEDED;
649 return aws_raise_error(AWS_ERROR_CBOR_RESOURCE_LIMIT_EXCEEDED);
650 }
651
652 if (decoder->cached_context.type == AWS_CBOR_TYPE_UNKNOWN) {
653 if (s_cbor_decode_next_element(decoder)) {
654 goto error;
655 }
656 }
657 switch (decoder->cached_context.type) {
658 case AWS_CBOR_TYPE_TAG:
659 decoder->cached_context.type = AWS_CBOR_TYPE_UNKNOWN;
660 if (aws_cbor_decoder_consume_next_whole_data_item(decoder)) {
661 goto error;
662 }
663 break;
664 case AWS_CBOR_TYPE_MAP_START: {
665 uint64_t num_map_item = decoder->cached_context.u.map_start;
666 decoder->cached_context.type = AWS_CBOR_TYPE_UNKNOWN;
667 for (uint64_t i = 0; i < num_map_item; i++) {
668 if (aws_cbor_decoder_consume_next_whole_data_item(decoder)) {
669 goto error;
670 }
671 if (aws_cbor_decoder_consume_next_whole_data_item(decoder)) {
672 goto error;
673 }
674 }
675 break;
676 }
677 case AWS_CBOR_TYPE_ARRAY_START: {
678 uint64_t num_array_item = decoder->cached_context.u.array_start;
679 decoder->cached_context.type = AWS_CBOR_TYPE_UNKNOWN;
680 for (uint64_t i = 0; i < num_array_item; i++) {
681 if (aws_cbor_decoder_consume_next_whole_data_item(decoder)) {
682 goto error;
683 }
684 }
685 break;
686 }
687 case AWS_CBOR_TYPE_INDEF_BYTES_START:
688 case AWS_CBOR_TYPE_INDEF_TEXT_START:
689 case AWS_CBOR_TYPE_INDEF_ARRAY_START:
690 case AWS_CBOR_TYPE_INDEF_MAP_START: {
691 enum aws_cbor_type next_type;
692 decoder->cached_context.type = AWS_CBOR_TYPE_UNKNOWN;
693 if (aws_cbor_decoder_peek_type(decoder, &next_type)) {
694 goto error;
695 }
696 while (next_type != AWS_CBOR_TYPE_BREAK) {
697 if (aws_cbor_decoder_consume_next_whole_data_item(decoder)) {
698 goto error;
699 }
700 if (aws_cbor_decoder_peek_type(decoder, &next_type)) {
701 goto error;
702 }
703 }
704 break;
705 }
706
707 default:
708 break;
709 }
710
711 decoder->cached_context.type = AWS_CBOR_TYPE_UNKNOWN;
712 --decoder->current_depth;
713 return AWS_OP_SUCCESS;
714
715 error:
716 --decoder->current_depth;
717 return AWS_OP_ERR;
718 }
719
720 int aws_cbor_decoder_consume_next_single_element(struct aws_cbor_decoder *decoder) {
721 enum aws_cbor_type out_type = 0;
722 if (aws_cbor_decoder_peek_type(decoder, &out_type)) {
723 return AWS_OP_ERR;
724 }
725 /* Reset the type to clear the cache. */
726 decoder->cached_context.type = AWS_CBOR_TYPE_UNKNOWN;
727 return AWS_OP_SUCCESS;
728 }