signing_result.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /**
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. * SPDX-License-Identifier: Apache-2.0.
  4. */
  5. #include <aws/auth/signing_result.h>
  6. #include <aws/common/byte_buf.h>
  7. #include <aws/common/string.h>
  8. #define INITIAL_SIGNING_RESULT_PROPERTIES_SIZE 10
  9. #define INITIAL_SIGNING_RESULT_PROPERTY_LISTS_TABLE_SIZE 10
  10. #define INITIAL_SIGNING_RESULT_PROPERTY_LIST_SIZE 10
  11. static void s_aws_signing_result_property_clean_up(struct aws_signing_result_property *pair) {
  12. aws_string_destroy(pair->name);
  13. aws_string_destroy(pair->value);
  14. }
  15. static void s_aws_hash_callback_property_list_destroy(void *value) {
  16. struct aws_array_list *property_list = value;
  17. size_t property_count = aws_array_list_length(property_list);
  18. for (size_t i = 0; i < property_count; ++i) {
  19. struct aws_signing_result_property property;
  20. AWS_ZERO_STRUCT(property);
  21. if (aws_array_list_get_at(property_list, &property, i)) {
  22. continue;
  23. }
  24. s_aws_signing_result_property_clean_up(&property);
  25. }
  26. struct aws_allocator *allocator = property_list->alloc;
  27. aws_array_list_clean_up(property_list);
  28. aws_mem_release(allocator, property_list);
  29. }
  30. int aws_signing_result_init(struct aws_signing_result *result, struct aws_allocator *allocator) {
  31. AWS_ZERO_STRUCT(*result);
  32. result->allocator = allocator;
  33. if (aws_hash_table_init(
  34. &result->properties,
  35. allocator,
  36. INITIAL_SIGNING_RESULT_PROPERTIES_SIZE,
  37. aws_hash_string,
  38. aws_hash_callback_string_eq,
  39. aws_hash_callback_string_destroy,
  40. aws_hash_callback_string_destroy) ||
  41. aws_hash_table_init(
  42. &result->property_lists,
  43. allocator,
  44. INITIAL_SIGNING_RESULT_PROPERTY_LISTS_TABLE_SIZE,
  45. aws_hash_string,
  46. aws_hash_callback_string_eq,
  47. aws_hash_callback_string_destroy,
  48. s_aws_hash_callback_property_list_destroy)) {
  49. goto on_error;
  50. }
  51. return AWS_OP_SUCCESS;
  52. on_error:
  53. aws_signing_result_clean_up(result);
  54. return AWS_OP_ERR;
  55. }
  56. void aws_signing_result_clean_up(struct aws_signing_result *result) {
  57. aws_hash_table_clean_up(&result->properties);
  58. aws_hash_table_clean_up(&result->property_lists);
  59. }
  60. int aws_signing_result_set_property(
  61. struct aws_signing_result *result,
  62. const struct aws_string *property_name,
  63. const struct aws_byte_cursor *property_value) {
  64. struct aws_string *name = NULL;
  65. struct aws_string *value = NULL;
  66. name = aws_string_new_from_string(result->allocator, property_name);
  67. value = aws_string_new_from_array(result->allocator, property_value->ptr, property_value->len);
  68. if (name == NULL || value == NULL) {
  69. goto on_error;
  70. }
  71. if (aws_hash_table_put(&result->properties, name, value, NULL)) {
  72. goto on_error;
  73. }
  74. return AWS_OP_SUCCESS;
  75. on_error:
  76. aws_string_destroy(name);
  77. aws_string_destroy(value);
  78. return AWS_OP_ERR;
  79. }
  80. int aws_signing_result_get_property(
  81. const struct aws_signing_result *result,
  82. const struct aws_string *property_name,
  83. struct aws_string **out_property_value) {
  84. struct aws_hash_element *element = NULL;
  85. aws_hash_table_find(&result->properties, property_name, &element);
  86. *out_property_value = NULL;
  87. if (element != NULL) {
  88. *out_property_value = element->value;
  89. }
  90. return AWS_OP_SUCCESS;
  91. }
  92. static struct aws_array_list *s_get_or_create_property_list(
  93. struct aws_signing_result *result,
  94. const struct aws_string *list_name) {
  95. struct aws_hash_element *element = NULL;
  96. aws_hash_table_find(&result->property_lists, list_name, &element);
  97. if (element != NULL) {
  98. return element->value;
  99. }
  100. struct aws_array_list *properties = aws_mem_acquire(result->allocator, sizeof(struct aws_array_list));
  101. if (properties == NULL) {
  102. return NULL;
  103. }
  104. AWS_ZERO_STRUCT(*properties);
  105. struct aws_string *name_copy = aws_string_new_from_string(result->allocator, list_name);
  106. if (name_copy == NULL) {
  107. goto on_error;
  108. }
  109. if (aws_array_list_init_dynamic(
  110. properties,
  111. result->allocator,
  112. INITIAL_SIGNING_RESULT_PROPERTY_LIST_SIZE,
  113. sizeof(struct aws_signing_result_property))) {
  114. goto on_error;
  115. }
  116. if (aws_hash_table_put(&result->property_lists, name_copy, properties, NULL)) {
  117. goto on_error;
  118. }
  119. return properties;
  120. on_error:
  121. aws_string_destroy(name_copy);
  122. aws_array_list_clean_up(properties);
  123. aws_mem_release(result->allocator, properties);
  124. return NULL;
  125. }
  126. int aws_signing_result_append_property_list(
  127. struct aws_signing_result *result,
  128. const struct aws_string *list_name,
  129. const struct aws_byte_cursor *property_name,
  130. const struct aws_byte_cursor *property_value) {
  131. struct aws_array_list *properties = s_get_or_create_property_list(result, list_name);
  132. if (properties == NULL) {
  133. return AWS_OP_ERR;
  134. }
  135. struct aws_string *name = NULL;
  136. struct aws_string *value = NULL;
  137. name = aws_string_new_from_array(result->allocator, property_name->ptr, property_name->len);
  138. value = aws_string_new_from_array(result->allocator, property_value->ptr, property_value->len);
  139. struct aws_signing_result_property property;
  140. property.name = name;
  141. property.value = value;
  142. if (aws_array_list_push_back(properties, &property)) {
  143. goto on_error;
  144. }
  145. return AWS_OP_SUCCESS;
  146. on_error:
  147. aws_string_destroy(name);
  148. aws_string_destroy(value);
  149. return AWS_OP_ERR;
  150. }
  151. void aws_signing_result_get_property_list(
  152. const struct aws_signing_result *result,
  153. const struct aws_string *list_name,
  154. struct aws_array_list **out_list) {
  155. *out_list = NULL;
  156. struct aws_hash_element *element = NULL;
  157. aws_hash_table_find(&result->property_lists, list_name, &element);
  158. if (element != NULL) {
  159. *out_list = element->value;
  160. }
  161. }
  162. void aws_signing_result_get_property_value_in_property_list(
  163. const struct aws_signing_result *result,
  164. const struct aws_string *list_name,
  165. const struct aws_string *property_name,
  166. struct aws_string **out_value) {
  167. *out_value = NULL;
  168. struct aws_array_list *property_list = NULL;
  169. aws_signing_result_get_property_list(result, list_name, &property_list);
  170. if (property_list == NULL) {
  171. return;
  172. }
  173. size_t pair_count = aws_array_list_length(property_list);
  174. for (size_t i = 0; i < pair_count; ++i) {
  175. struct aws_signing_result_property pair;
  176. AWS_ZERO_STRUCT(pair);
  177. if (aws_array_list_get_at(property_list, &pair, i)) {
  178. continue;
  179. }
  180. if (pair.name == NULL) {
  181. continue;
  182. }
  183. if (aws_string_eq_ignore_case(property_name, pair.name)) {
  184. *out_value = pair.value;
  185. break;
  186. }
  187. }
  188. }