@@ -50,16 +50,17 @@ class AWSSigV4Signer {
5050
5151 // To sign
5252 std::string string_to_sign =
53- std::format (" {}\n {}\n {}\n {}\n " , hash_algo, request_date ,
53+ std::format (" {}\n {}\n {}\n {}" , hash_algo, timestamp ,
5454 credential_scope, hex_cannonical_request);
55- std::string signature = hex (HMAC_SHA256 (deriveSigningKey (request_date), string_to_sign));
55+ std::string signature =
56+ hex (HMAC_SHA256 (deriveSigningKey (request_date), SHA256_DIGEST_LENGTH, string_to_sign));
5657
5758 // Build the final auth header value
5859 request.header (
5960 " Authorization" ,
6061 std::format (" {} Credential={}/{}, SignedHeaders={}, Signature={}" ,
6162 hash_algo, access_key, credential_scope, signed_headers,
62- hex_cannonical_request, signature));
63+ signature));
6364 }
6465
6566 std::string createCannonicalRequest (HttpRequest &request) {
@@ -71,7 +72,13 @@ class AWSSigV4Signer {
7172 if (size_t bpos = url.find (" amazonaws.com" ); bpos != std::string::npos) {
7273 uri = url.erase (0 , bpos + 13 );
7374 } else {
74- // Assume localhost falls here...
75+ // Assume localhost:XXXX (dirty, sorry :( i know)
76+ size_t path_start = url.find (' /' , 7 );
77+ if (path_start != std::string::npos) {
78+ uri = url.substr (path_start);
79+ } else {
80+ uri = " /" ;
81+ }
7582 }
7683
7784 // URI Query-string
@@ -112,11 +119,9 @@ class AWSSigV4Signer {
112119 return digest;
113120 }
114121
115- const unsigned char *HMAC_SHA256 (const unsigned char *key,
122+ const unsigned char *HMAC_SHA256 (const unsigned char *key, size_t key_len,
116123 const std::string &data) {
117- unsigned int hashLen;
118-
119- return HMAC (EVP_sha256 (), key, strlen ((char *)key),
124+ return HMAC (EVP_sha256 (), key, key_len,
120125 reinterpret_cast <const unsigned char *>(data.c_str ()),
121126 data.size (), NULL , NULL );
122127 }
@@ -130,7 +135,6 @@ class AWSSigV4Signer {
130135 return ss.str ();
131136 }
132137
133-
134138 // Move to S3 client class
135139 // --
136140 // Required by AWS SigV4 to be in ISO8601 format
@@ -164,13 +168,16 @@ class AWSSigV4Signer {
164168 }
165169 }
166170
167- const unsigned char * deriveSigningKey (const std::string request_date) {
168- const std::string initial_candidate = " AWS4" + secret_key;
169- const unsigned char * keyCandidate = reinterpret_cast <const unsigned char *>(initial_candidate.c_str ());
170- const unsigned char * DateKey = HMAC_SHA256 (keyCandidate, request_date);
171- const unsigned char * DateRegionKey = HMAC_SHA256 (DateKey, aws_region);
172- const unsigned char * DateRegionServiceKey = HMAC_SHA256 (DateRegionKey, " s3" );
173- const unsigned char * SigningKey = HMAC_SHA256 (DateRegionServiceKey, " aws4_request" );
171+ const unsigned char *deriveSigningKey (const std::string request_date) {
172+ const std::string initial_candidate = " AWS4" + secret_key;
173+ const unsigned char *keyCandidate =
174+ reinterpret_cast <const unsigned char *>(initial_candidate.c_str ());
175+ const unsigned char *DateKey = HMAC_SHA256 (keyCandidate, initial_candidate.size (), request_date);
176+ const unsigned char *DateRegionKey = HMAC_SHA256 (DateKey, SHA256_DIGEST_LENGTH, aws_region);
177+ const unsigned char *DateRegionServiceKey =
178+ HMAC_SHA256 (DateRegionKey, SHA256_DIGEST_LENGTH, " s3" );
179+ const unsigned char *SigningKey =
180+ HMAC_SHA256 (DateRegionServiceKey, SHA256_DIGEST_LENGTH, " aws4_request" );
174181 return SigningKey;
175182 }
176183};
0 commit comments