signable.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /**
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. * SPDX-License-Identifier: Apache-2.0.
  4. */
  5. #include <aws/auth/signable.h>
  6. #include <aws/common/string.h>
  7. void aws_signable_destroy(struct aws_signable *signable) {
  8. if (signable == NULL) {
  9. return;
  10. }
  11. if (signable->vtable != NULL) {
  12. signable->vtable->destroy(signable);
  13. }
  14. }
  15. int aws_signable_get_property(
  16. const struct aws_signable *signable,
  17. const struct aws_string *name,
  18. struct aws_byte_cursor *out_value) {
  19. AWS_ASSERT(signable && signable->vtable && signable->vtable->get_property);
  20. return signable->vtable->get_property(signable, name, out_value);
  21. }
  22. int aws_signable_get_property_list(
  23. const struct aws_signable *signable,
  24. const struct aws_string *name,
  25. struct aws_array_list **out_property_list) {
  26. AWS_ASSERT(signable && signable->vtable && signable->vtable->get_property_list);
  27. return signable->vtable->get_property_list(signable, name, out_property_list);
  28. }
  29. int aws_signable_get_payload_stream(const struct aws_signable *signable, struct aws_input_stream **out_input_stream) {
  30. AWS_ASSERT(signable && signable->vtable && signable->vtable->get_payload_stream);
  31. return signable->vtable->get_payload_stream(signable, out_input_stream);
  32. }
  33. AWS_STRING_FROM_LITERAL(g_aws_http_headers_property_list_name, "headers");
  34. AWS_STRING_FROM_LITERAL(g_aws_http_query_params_property_list_name, "params");
  35. AWS_STRING_FROM_LITERAL(g_aws_http_method_property_name, "method");
  36. AWS_STRING_FROM_LITERAL(g_aws_http_uri_property_name, "uri");
  37. AWS_STRING_FROM_LITERAL(g_aws_signature_property_name, "signature");
  38. AWS_STRING_FROM_LITERAL(g_aws_previous_signature_property_name, "previous-signature");
  39. AWS_STRING_FROM_LITERAL(g_aws_canonical_request_property_name, "canonical-request");
  40. /*
  41. * This is a simple aws_signable wrapper implementation for AWS's canonical representation of an http request
  42. */
  43. struct aws_signable_canonical_request_impl {
  44. struct aws_string *canonical_request;
  45. };
  46. static int s_aws_signable_canonical_request_get_property(
  47. const struct aws_signable *signable,
  48. const struct aws_string *name,
  49. struct aws_byte_cursor *out_value) {
  50. struct aws_signable_canonical_request_impl *impl = signable->impl;
  51. AWS_ZERO_STRUCT(*out_value);
  52. /*
  53. * uri and method can be queried directly from the wrapper request
  54. */
  55. if (aws_string_eq(name, g_aws_canonical_request_property_name)) {
  56. *out_value = aws_byte_cursor_from_string(impl->canonical_request);
  57. } else {
  58. return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
  59. }
  60. return AWS_OP_SUCCESS;
  61. }
  62. static int s_aws_signable_canonical_request_get_property_list(
  63. const struct aws_signable *signable,
  64. const struct aws_string *name,
  65. struct aws_array_list **out_list) {
  66. (void)signable;
  67. (void)name;
  68. (void)out_list;
  69. *out_list = NULL;
  70. return aws_raise_error(AWS_ERROR_UNSUPPORTED_OPERATION);
  71. }
  72. static int s_aws_signable_canonical_request_get_payload_stream(
  73. const struct aws_signable *signable,
  74. struct aws_input_stream **out_input_stream) {
  75. (void)signable;
  76. *out_input_stream = NULL;
  77. return AWS_OP_SUCCESS;
  78. }
  79. static void s_aws_signable_canonical_request_destroy(struct aws_signable *signable) {
  80. if (signable == NULL) {
  81. return;
  82. }
  83. struct aws_signable_canonical_request_impl *impl = signable->impl;
  84. if (impl == NULL) {
  85. return;
  86. }
  87. aws_string_destroy(impl->canonical_request);
  88. aws_mem_release(signable->allocator, signable);
  89. }
  90. static struct aws_signable_vtable s_signable_canonical_request_vtable = {
  91. .get_property = s_aws_signable_canonical_request_get_property,
  92. .get_property_list = s_aws_signable_canonical_request_get_property_list,
  93. .get_payload_stream = s_aws_signable_canonical_request_get_payload_stream,
  94. .destroy = s_aws_signable_canonical_request_destroy,
  95. };
  96. struct aws_signable *aws_signable_new_canonical_request(
  97. struct aws_allocator *allocator,
  98. struct aws_byte_cursor canonical_request) {
  99. struct aws_signable *signable = NULL;
  100. struct aws_signable_canonical_request_impl *impl = NULL;
  101. aws_mem_acquire_many(
  102. allocator,
  103. 2,
  104. &signable,
  105. sizeof(struct aws_signable),
  106. &impl,
  107. sizeof(struct aws_signable_canonical_request_impl));
  108. if (signable == NULL || impl == NULL) {
  109. return NULL;
  110. }
  111. AWS_ZERO_STRUCT(*signable);
  112. AWS_ZERO_STRUCT(*impl);
  113. signable->allocator = allocator;
  114. signable->vtable = &s_signable_canonical_request_vtable;
  115. signable->impl = impl;
  116. impl->canonical_request = aws_string_new_from_array(allocator, canonical_request.ptr, canonical_request.len);
  117. if (impl->canonical_request == NULL) {
  118. goto on_error;
  119. }
  120. return signable;
  121. on_error:
  122. aws_signable_destroy(signable);
  123. return NULL;
  124. }