signing_config.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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_config.h>
  6. #include <aws/auth/credentials.h>
  7. const struct aws_byte_cursor g_aws_signed_body_value_empty_sha256 =
  8. AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
  9. const struct aws_byte_cursor g_aws_signed_body_value_unsigned_payload =
  10. AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("UNSIGNED-PAYLOAD");
  11. const struct aws_byte_cursor g_aws_signed_body_value_streaming_unsigned_payload_trailer =
  12. AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("STREAMING-UNSIGNED-PAYLOAD-TRAILER");
  13. const struct aws_byte_cursor g_aws_signed_body_value_streaming_aws4_hmac_sha256_payload =
  14. AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("STREAMING-AWS4-HMAC-SHA256-PAYLOAD");
  15. const struct aws_byte_cursor g_aws_signed_body_value_streaming_aws4_hmac_sha256_payload_trailer =
  16. AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("STREAMING-AWS4-HMAC-SHA256-PAYLOAD-TRAILER");
  17. const struct aws_byte_cursor g_aws_signed_body_value_streaming_aws4_ecdsa_p256_sha256_payload =
  18. AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("STREAMING-AWS4-ECDSA-P256-SHA256-PAYLOAD");
  19. const struct aws_byte_cursor g_aws_signed_body_value_streaming_aws4_ecdsa_p256_sha256_payload_trailer =
  20. AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("STREAMING-AWS4-ECDSA-P256-SHA256-PAYLOAD-TRAILER");
  21. const struct aws_byte_cursor g_aws_signed_body_value_streaming_aws4_hmac_sha256_events =
  22. AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("STREAMING-AWS4-HMAC-SHA256-EVENTS");
  23. const char *aws_signing_algorithm_to_string(enum aws_signing_algorithm algorithm) {
  24. switch (algorithm) {
  25. case AWS_SIGNING_ALGORITHM_V4:
  26. return "SigV4";
  27. case AWS_SIGNING_ALGORITHM_V4_ASYMMETRIC:
  28. return "SigV4Asymmetric";
  29. default:
  30. break;
  31. }
  32. return "Unknown";
  33. }
  34. int aws_validate_aws_signing_config_aws(const struct aws_signing_config_aws *config) {
  35. if (config == NULL) {
  36. AWS_LOGF_ERROR(AWS_LS_AUTH_SIGNING, "AWS signing config is null");
  37. return aws_raise_error(AWS_AUTH_SIGNING_INVALID_CONFIGURATION);
  38. }
  39. if (config->signature_type == AWS_ST_HTTP_REQUEST_EVENT && config->algorithm != AWS_SIGNING_ALGORITHM_V4) {
  40. /*
  41. * Not supported yet.
  42. *
  43. * Need to determine if the Transcribe service supports Sigv4a and how to test it.
  44. * Transcribe's examples are insufficient.
  45. */
  46. AWS_LOGF_ERROR(AWS_LS_AUTH_SIGNING, "(id=%p) Event signing is only supported for Sigv4 yet", (void *)config);
  47. return aws_raise_error(AWS_AUTH_SIGNING_INVALID_CONFIGURATION);
  48. }
  49. if (config->signature_type != AWS_ST_HTTP_REQUEST_HEADERS &&
  50. config->signature_type != AWS_ST_HTTP_REQUEST_QUERY_PARAMS) {
  51. /*
  52. * If we're not signing the full request then it's critical that the credentials we're using are the same
  53. * credentials used on the original request. If we're using a provider to fetch credentials then that is
  54. * not guaranteed. For now, force users to always pass in credentials when signing events or chunks.
  55. *
  56. * The correct long-term solution would be to add a way to pass the credentials used in the initial
  57. * signing back to the user in the completion callback. Then the user could supply those credentials
  58. * to all subsequent chunk/event signings. The fact that we don't do that yet doesn't invalidate this check.
  59. */
  60. if (config->credentials == NULL) {
  61. AWS_LOGF_ERROR(
  62. AWS_LS_AUTH_SIGNING,
  63. "(id=%p) Chunk/event signing config must contain explicit credentials",
  64. (void *)config);
  65. return aws_raise_error(AWS_AUTH_SIGNING_INVALID_CONFIGURATION);
  66. }
  67. }
  68. if (config->region.len == 0) {
  69. AWS_LOGF_ERROR(AWS_LS_AUTH_SIGNING, "(id=%p) Signing config is missing a region identifier", (void *)config);
  70. return aws_raise_error(AWS_AUTH_SIGNING_INVALID_CONFIGURATION);
  71. }
  72. if (config->service.len == 0) {
  73. AWS_LOGF_ERROR(AWS_LS_AUTH_SIGNING, "(id=%p) Signing config is missing a service identifier", (void *)config);
  74. return aws_raise_error(AWS_AUTH_SIGNING_INVALID_CONFIGURATION);
  75. }
  76. switch (config->algorithm) {
  77. case AWS_SIGNING_ALGORITHM_V4:
  78. if (config->credentials == NULL && config->credentials_provider == NULL) {
  79. AWS_LOGF_ERROR(
  80. AWS_LS_AUTH_SIGNING,
  81. "(id=%p) Sigv4 signing config is missing a credentials provider or credentials",
  82. (void *)config);
  83. return aws_raise_error(AWS_AUTH_SIGNING_INVALID_CONFIGURATION);
  84. }
  85. if (config->credentials != NULL && !aws_credentials_is_anonymous(config->credentials)) {
  86. if (aws_credentials_get_access_key_id(config->credentials).len == 0 ||
  87. aws_credentials_get_secret_access_key(config->credentials).len == 0) {
  88. AWS_LOGF_ERROR(
  89. AWS_LS_AUTH_SIGNING,
  90. "(id=%p) Sigv4 signing configured with invalid credentials",
  91. (void *)config);
  92. return aws_raise_error(AWS_AUTH_SIGNING_INVALID_CREDENTIALS);
  93. }
  94. }
  95. break;
  96. case AWS_SIGNING_ALGORITHM_V4_ASYMMETRIC:
  97. if (config->credentials == NULL && config->credentials_provider == NULL) {
  98. AWS_LOGF_ERROR(
  99. AWS_LS_AUTH_SIGNING,
  100. "(id=%p) Sigv4 asymmetric signing config is missing a credentials provider or credentials",
  101. (void *)config);
  102. return aws_raise_error(AWS_AUTH_SIGNING_INVALID_CONFIGURATION);
  103. }
  104. break;
  105. default:
  106. return aws_raise_error(AWS_AUTH_SIGNING_INVALID_CONFIGURATION);
  107. }
  108. return AWS_OP_SUCCESS;
  109. }