8#include <smithy/interceptor/Interceptor.h>
10#include <aws/core/AmazonWebServiceRequest.h>
11#include <aws/core/http/HttpRequest.h>
12#include <aws/core/http/HttpResponse.h>
13#include <aws/core/utils/HashingUtils.h>
14#include <aws/core/utils/crypto/MD5.h>
15#include <aws/core/utils/crypto/CRC32.h>
16#include <aws/core/utils/crypto/Sha256.h>
17#include <aws/core/utils/crypto/Sha1.h>
18#include <aws/core/utils/crypto/PrecalculatedHash.h>
19#include <aws/core/platform/Environment.h>
52 if (request.
GetBody() !=
nullptr)
64 if (httpRequest ==
nullptr)
67 "ValidationErrorException",
68 "Checksum request validation missing request",
72 request.GetChecksumAlgorithmName().c_str());
73 if (request.GetServiceSpecificParameters())
75 auto requestChecksumOverride = request.GetServiceSpecificParameters()->parameterMap.find(
77 if (requestChecksumOverride != request.GetServiceSpecificParameters()->parameterMap.end())
79 checksumAlgorithmName = requestChecksumOverride->second;
83 bool shouldSkipChecksum = request.GetServiceSpecificParameters() &&
84 request.GetServiceSpecificParameters()->parameterMap.find(
"overrideChecksumDisable") !=
85 request.GetServiceSpecificParameters()->parameterMap.end();
88 if (!checksumAlgorithmName.empty() && !shouldSkipChecksum)
91 const Aws::String checksumType =
"x-amz-checksum-" + checksumAlgorithmName;
93 const auto checksumHeader = headers.find(checksumType);
94 bool checksumValueAndAlgorithmProvided = checksumHeader != headers.end();
98 if (request.IsStreaming() && checksumValueAndAlgorithmProvided)
100 const auto hash = Aws::MakeShared<PrecalculatedHash>(
102 httpRequest->SetRequestHash(checksumAlgorithmName, hash);
104 else if (checksumValueAndAlgorithmProvided)
106 httpRequest->SetHeaderValue(checksumType, checksumHeader->second);
108 else if (checksumAlgorithmName ==
"crc32")
110 if (request.IsStreaming())
112 httpRequest->SetRequestHash(checksumAlgorithmName,
117 httpRequest->SetHeaderValue(checksumType,
122 else if (checksumAlgorithmName ==
"crc32c")
124 if (request.IsStreaming())
126 httpRequest->SetRequestHash(checksumAlgorithmName,
131 httpRequest->SetHeaderValue(checksumType,
136 else if (checksumAlgorithmName ==
"sha256")
138 if (request.IsStreaming())
140 httpRequest->SetRequestHash(checksumAlgorithmName,
145 httpRequest->SetHeaderValue(checksumType,
150 else if (checksumAlgorithmName ==
"sha1")
152 if (request.IsStreaming())
154 httpRequest->SetRequestHash(checksumAlgorithmName,
159 httpRequest->SetHeaderValue(checksumType,
173 "Checksum algorithm: " << checksumAlgorithmName <<
174 "is not supported by SDK.");
179 if (request.ShouldValidateResponseChecksum())
181 for (
const Aws::String& responseChecksumAlgorithmName : request.GetResponseChecksumAlgorithmNames())
185 if (checksumAlgorithmName ==
"crc32c")
189 httpRequest->AddResponseValidationHash(
"crc32c", crc32c);
191 else if (checksumAlgorithmName ==
"crc32")
195 httpRequest->AddResponseValidationHash(
"crc32", crc32);
197 else if (checksumAlgorithmName ==
"sha1")
199 std::shared_ptr<Sha1> sha1 = Aws::MakeShared<Sha1>(
201 httpRequest->AddResponseValidationHash(
"sha1", sha1);
203 else if (checksumAlgorithmName ==
"sha256")
207 httpRequest->AddResponseValidationHash(
"sha256", sha256);
212 "Checksum algorithm: " << checksumAlgorithmName <<
213 " is not supported in validating response body yet.");
224 if (httpRequest ==
nullptr || httpResponse ==
nullptr)
227 "ValidationErrorException",
228 "Checksum response validation missing request or response",
231 for (
const auto& hashIterator : httpRequest->GetResponseValidationHashes())
235 if (httpResponse->HasHeader(checksumHeaderKey.c_str()))
237 const Aws::String& checksumHeaderValue = httpResponse->GetHeader(checksumHeaderKey);
243 "Response checksums mismatch",
246 error.SetResponseCode(httpResponse->GetResponseCode());
247 error.SetRemoteHostIpAddress(
248 httpResponse->GetOriginatingRequest().GetResolvedRemoteHost());
virtual std::shared_ptr< Aws::IOStream > GetBody() const =0
void SetResponseHeaders(const Aws::Http::HeaderValueCollection &headers)
static ByteBuffer CalculateCRC32(const Aws::String &str)
static Aws::String Base64Encode(const ByteBuffer &byteBuffer)
static ByteBuffer CalculateMD5(const Aws::String &str)
static ByteBuffer CalculateSHA1(const Aws::String &str)
static ByteBuffer CalculateCRC32C(const Aws::String &str)
static ByteBuffer CalculateSHA256(const Aws::String &str)
static Aws::String ToLower(const char *source)
Aws::Http::HeaderValueCollection HeaderValueCollection
ChecksumInterceptor(const ChecksumInterceptor &other)=delete
ChecksumInterceptor & operator=(ChecksumInterceptor &&other) noexcept=default
ChecksumInterceptor()=default
ModifyRequestOutcome ModifyBeforeSigning(interceptor::InterceptorContext &context) override
ChecksumInterceptor & operator=(const ChecksumInterceptor &other)=delete
ModifyResponseOutcome ModifyBeforeDeserialization(interceptor::InterceptorContext &context) override
~ChecksumInterceptor() override=default
static std::shared_ptr< Aws::IOStream > GetBodyStream(const Aws::AmazonWebServiceRequest &request)
ChecksumInterceptor(ChecksumInterceptor &&other) noexcept=default
const Aws::AmazonWebServiceRequest & GetModeledRequest() const
std::shared_ptr< Aws::Http::HttpRequest > GetTransmitRequest() const
std::shared_ptr< Aws::Http::HttpResponse > GetTransmitResponse() const
Aws::Map< Aws::String, Aws::String > HeaderValueCollection
std::shared_ptr< T > MakeShared(const char *allocationTag, ArgTypes &&... args)
std::basic_string< char, std::char_traits< char >, Aws::Allocator< char > > String
static const char AWS_SMITHY_CLIENT_CHECKSUM[]
static const char CHECKSUM_CONTENT_MD5_HEADER[]