auth.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /**
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. * SPDX-License-Identifier: Apache-2.0.
  4. */
  5. #include <aws/auth/auth.h>
  6. #include <aws/auth/private/aws_signing.h>
  7. #include <aws/cal/cal.h>
  8. #include <aws/http/http.h>
  9. #include <aws/sdkutils/sdkutils.h>
  10. #include <aws/common/error.h>
  11. #include <aws/common/json.h>
  12. #define AWS_DEFINE_ERROR_INFO_AUTH(CODE, STR) AWS_DEFINE_ERROR_INFO(CODE, STR, "aws-c-auth")
  13. /* clang-format off */
  14. static struct aws_error_info s_errors[] = {
  15. AWS_DEFINE_ERROR_INFO_AUTH(
  16. AWS_AUTH_SIGNING_UNSUPPORTED_ALGORITHM,
  17. "Attempt to sign an http request with an unsupported version of the AWS signing protocol"),
  18. AWS_DEFINE_ERROR_INFO_AUTH(
  19. AWS_AUTH_SIGNING_MISMATCHED_CONFIGURATION,
  20. "Attempt to sign an http request with a signing configuration unrecognized by the invoked signer"),
  21. AWS_DEFINE_ERROR_INFO_AUTH(
  22. AWS_AUTH_SIGNING_NO_CREDENTIALS,
  23. "Attempt to sign an http request without credentials"),
  24. AWS_DEFINE_ERROR_INFO_AUTH(
  25. AWS_AUTH_SIGNING_ILLEGAL_REQUEST_QUERY_PARAM,
  26. "Attempt to sign an http request that includes a query param that signing may add"),
  27. AWS_DEFINE_ERROR_INFO_AUTH(
  28. AWS_AUTH_SIGNING_ILLEGAL_REQUEST_HEADER,
  29. "Attempt to sign an http request that includes a header that signing may add"),
  30. AWS_DEFINE_ERROR_INFO_AUTH(
  31. AWS_AUTH_SIGNING_INVALID_CONFIGURATION,
  32. "Attempt to sign an http request with an invalid signing configuration"),
  33. AWS_DEFINE_ERROR_INFO_AUTH(
  34. AWS_AUTH_CREDENTIALS_PROVIDER_INVALID_ENVIRONMENT,
  35. "Valid credentials could not be sourced from process environment"),
  36. AWS_DEFINE_ERROR_INFO_AUTH(
  37. AWS_AUTH_CREDENTIALS_PROVIDER_INVALID_DELEGATE,
  38. "Valid credentials could not be sourced from the provided vtable"),
  39. AWS_DEFINE_ERROR_INFO_AUTH(
  40. AWS_AUTH_CREDENTIALS_PROVIDER_PROFILE_SOURCE_FAILURE,
  41. "Valid credentials could not be sourced by a profile provider"),
  42. AWS_DEFINE_ERROR_INFO_AUTH(
  43. AWS_AUTH_CREDENTIALS_PROVIDER_IMDS_SOURCE_FAILURE,
  44. "Valid credentials could not be sourced by the IMDS provider"),
  45. AWS_DEFINE_ERROR_INFO_AUTH(
  46. AWS_AUTH_CREDENTIALS_PROVIDER_STS_SOURCE_FAILURE,
  47. "Valid credentials could not be sourced by the STS provider"),
  48. AWS_DEFINE_ERROR_INFO_AUTH(
  49. AWS_AUTH_CREDENTIALS_PROVIDER_HTTP_STATUS_FAILURE,
  50. "Unsuccessful status code returned from credentials-fetching http request"),
  51. AWS_DEFINE_ERROR_INFO_AUTH(
  52. AWS_AUTH_PROVIDER_PARSER_UNEXPECTED_RESPONSE,
  53. "Invalid response document encountered while querying credentials via http"),
  54. AWS_DEFINE_ERROR_INFO_AUTH(
  55. AWS_AUTH_CREDENTIALS_PROVIDER_ECS_SOURCE_FAILURE,
  56. "Valid credentials could not be sourced by the ECS provider"),
  57. AWS_DEFINE_ERROR_INFO_AUTH(
  58. AWS_AUTH_CREDENTIALS_PROVIDER_X509_SOURCE_FAILURE,
  59. "Valid credentials could not be sourced by the X509 provider"),
  60. AWS_DEFINE_ERROR_INFO_AUTH(
  61. AWS_AUTH_CREDENTIALS_PROVIDER_PROCESS_SOURCE_FAILURE,
  62. "Valid credentials could not be sourced by the process provider"),
  63. AWS_DEFINE_ERROR_INFO_AUTH(
  64. AWS_AUTH_CREDENTIALS_PROVIDER_STS_WEB_IDENTITY_SOURCE_FAILURE,
  65. "Valid credentials could not be sourced by the sts web identity provider"),
  66. AWS_DEFINE_ERROR_INFO_AUTH(
  67. AWS_AUTH_SIGNING_UNSUPPORTED_SIGNATURE_TYPE,
  68. "Attempt to sign using an unusupported signature type"),
  69. AWS_DEFINE_ERROR_INFO_AUTH(
  70. AWS_AUTH_SIGNING_MISSING_PREVIOUS_SIGNATURE,
  71. "Attempt to sign a streaming item without supplying a previous signature"),
  72. AWS_DEFINE_ERROR_INFO_AUTH(
  73. AWS_AUTH_SIGNING_INVALID_CREDENTIALS,
  74. "Attempt to perform a signing operation with invalid credentials"),
  75. AWS_DEFINE_ERROR_INFO_AUTH(
  76. AWS_AUTH_CANONICAL_REQUEST_MISMATCH,
  77. "Expected canonical request did not match the computed canonical request"),
  78. AWS_DEFINE_ERROR_INFO_AUTH(
  79. AWS_AUTH_SIGV4A_SIGNATURE_VALIDATION_FAILURE,
  80. "The supplied sigv4a signature was not a valid signature for the hashed string to sign"),
  81. AWS_DEFINE_ERROR_INFO_AUTH(
  82. AWS_AUTH_CREDENTIALS_PROVIDER_COGNITO_SOURCE_FAILURE,
  83. "Valid credentials could not be sourced by the cognito provider"),
  84. AWS_DEFINE_ERROR_INFO_AUTH(
  85. AWS_AUTH_CREDENTIALS_PROVIDER_DELEGATE_FAILURE,
  86. "Valid credentials could not be sourced by the delegate provider"),
  87. };
  88. /* clang-format on */
  89. static struct aws_error_info_list s_error_list = {
  90. .error_list = s_errors,
  91. .count = sizeof(s_errors) / sizeof(struct aws_error_info),
  92. };
  93. static struct aws_log_subject_info s_auth_log_subject_infos[] = {
  94. DEFINE_LOG_SUBJECT_INFO(
  95. AWS_LS_AUTH_GENERAL,
  96. "AuthGeneral",
  97. "Subject for aws-c-auth logging that defies categorization."),
  98. DEFINE_LOG_SUBJECT_INFO(AWS_LS_AUTH_PROFILE, "AuthProfile", "Subject for config profile related logging."),
  99. DEFINE_LOG_SUBJECT_INFO(
  100. AWS_LS_AUTH_CREDENTIALS_PROVIDER,
  101. "AuthCredentialsProvider",
  102. "Subject for credentials provider related logging."),
  103. DEFINE_LOG_SUBJECT_INFO(AWS_LS_AUTH_SIGNING, "AuthSigning", "Subject for AWS request signing logging."),
  104. };
  105. static struct aws_log_subject_info_list s_auth_log_subject_list = {
  106. .subject_list = s_auth_log_subject_infos,
  107. .count = AWS_ARRAY_SIZE(s_auth_log_subject_infos),
  108. };
  109. static bool s_library_initialized = false;
  110. static struct aws_allocator *s_library_allocator = NULL;
  111. void aws_auth_library_init(struct aws_allocator *allocator) {
  112. if (s_library_initialized) {
  113. return;
  114. }
  115. if (allocator) {
  116. s_library_allocator = allocator;
  117. } else {
  118. s_library_allocator = aws_default_allocator();
  119. }
  120. aws_sdkutils_library_init(s_library_allocator);
  121. aws_cal_library_init(s_library_allocator);
  122. aws_http_library_init(s_library_allocator);
  123. aws_register_error_info(&s_error_list);
  124. aws_register_log_subject_info_list(&s_auth_log_subject_list);
  125. AWS_FATAL_ASSERT(aws_signing_init_signing_tables(allocator) == AWS_OP_SUCCESS);
  126. s_library_initialized = true;
  127. }
  128. void aws_auth_library_clean_up(void) {
  129. if (!s_library_initialized) {
  130. return;
  131. }
  132. s_library_initialized = false;
  133. aws_signing_clean_up_signing_tables();
  134. aws_unregister_log_subject_info_list(&s_auth_log_subject_list);
  135. aws_unregister_error_info(&s_error_list);
  136. aws_http_library_clean_up();
  137. aws_cal_library_clean_up();
  138. aws_sdkutils_library_clean_up();
  139. s_library_allocator = NULL;
  140. }