| 1 |
// |
| 2 |
// Copyright 2020 gRPC authors. |
| 3 |
// |
| 4 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
// you may not use this file except in compliance with the License. |
| 6 |
// You may obtain a copy of the License at |
| 7 |
// |
| 8 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
// |
| 10 |
// Unless required by applicable law or agreed to in writing, software |
| 11 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
// See the License for the specific language governing permissions and |
| 14 |
// limitations under the License. |
| 15 |
// |
| 16 |
#include <grpc/support/port_platform.h> |
| 17 |
|
| 18 |
#include "src/core/lib/security/credentials/external/aws_request_signer.h" |
| 19 |
|
| 20 |
#include <algorithm> |
| 21 |
#include <utility> |
| 22 |
#include <vector> |
| 23 |
|
| 24 |
#include <openssl/crypto.h> |
| 25 |
#include <openssl/evp.h> |
| 26 |
#include <openssl/hmac.h> |
| 27 |
#include <openssl/sha.h> |
| 28 |
|
| 29 |
#include "absl/status/statusor.h" |
| 30 |
#include "absl/strings/ascii.h" |
| 31 |
#include "absl/strings/escaping.h" |
| 32 |
#include "absl/strings/str_cat.h" |
| 33 |
#include "absl/strings/str_format.h" |
| 34 |
#include "absl/strings/str_join.h" |
| 35 |
#include "absl/strings/str_split.h" |
| 36 |
#include "absl/strings/string_view.h" |
| 37 |
#include "absl/time/clock.h" |
| 38 |
#include "absl/time/time.h" |
| 39 |
|
| 40 |
namespace grpc_core { |
| 41 |
|
| 42 |
namespace { |
| 43 |
|
| 44 |
const char kAlgorithm[] = "AWS4-HMAC-SHA256"; |
| 45 |
const char kDateFormat[] = "%a, %d %b %E4Y %H:%M:%S %Z"; |
| 46 |
const char kXAmzDateFormat[] = "%Y%m%dT%H%M%SZ"; |
| 47 |
|
| 48 |
void SHA256(const std::string& str, unsigned char out[SHA256_DIGEST_LENGTH]) { |
| 49 |
SHA256_CTX sha256; |
| 50 |
SHA256_Init(&sha256); |
| 51 |
SHA256_Update(&sha256, str.c_str(), str.size()); |
| 52 |
SHA256_Final(out, &sha256); |
| 53 |
} |
| 54 |
|
| 55 |
std::string SHA256Hex(const std::string& str) { |
| 56 |
unsigned char hash[SHA256_DIGEST_LENGTH]; |
| 57 |
SHA256(str, hash); |
| 58 |
std::string hash_str(reinterpret_cast<char const*>(hash), |
| 59 |
SHA256_DIGEST_LENGTH); |
| 60 |
return absl::BytesToHexString(hash_str); |
| 61 |
} |
| 62 |
|
| 63 |
std::string HMAC(const std::string& key, const std::string& msg) { |
| 64 |
unsigned int len; |
| 65 |
unsigned char digest[EVP_MAX_MD_SIZE]; |
| 66 |
HMAC(EVP_sha256(), key.c_str(), key.length(), |
| 67 |
reinterpret_cast<const unsigned char*>(msg.c_str()), msg.length(), |
| 68 |
digest, &len); |
| 69 |
return std::string(digest, digest + len); |
| 70 |
} |
| 71 |
|
| 72 |
} // namespace |
| 73 |
|
| 74 |
AwsRequestSigner::AwsRequestSigner( |
| 75 |
std::string access_key_id, std::string secret_access_key, std::string token, |
| 76 |
std::string method, std::string url, std::string region, |
| 77 |
std::string request_payload, |
| 78 |
std::map<std::string, std::string> additional_headers, |
| 79 |
grpc_error_handle* error) |
| 80 |
: access_key_id_(std::move(access_key_id)), |
| 81 |
secret_access_key_(std::move(secret_access_key)), |
| 82 |
token_(std::move(token)), |
| 83 |
method_(std::move(method)), |
| 84 |
region_(std::move(region)), |
| 85 |
request_payload_(std::move(request_payload)), |
| 86 |
additional_headers_(std::move(additional_headers)) { |
| 87 |
auto amz_date_it = additional_headers_.find("x-amz-date"); |
| 88 |
auto date_it = additional_headers_.find("date"); |
| 89 |
if (amz_date_it != additional_headers_.end() && |
| 90 |
date_it != additional_headers_.end()) { |
| 91 |
*error = GRPC_ERROR_CREATE( |
| 92 |
"Only one of {date, x-amz-date} can be specified, not both."); |
| 93 |
return; |
| 94 |
} |
| 95 |
if (amz_date_it != additional_headers_.end()) { |
| 96 |
static_request_date_ = amz_date_it->second; |
| 97 |
} else if (date_it != additional_headers_.end()) { |
| 98 |
absl::Time request_date; |
| 99 |
std::string err_str; |
| 100 |
if (!absl::ParseTime(kDateFormat, date_it->second, &request_date, |
| 101 |
&err_str)) { |
| 102 |
*error = GRPC_ERROR_CREATE(err_str.c_str()); |
| 103 |
return; |
| 104 |
} |
| 105 |
static_request_date_ = |
| 106 |
absl::FormatTime(kXAmzDateFormat, request_date, absl::UTCTimeZone()); |
| 107 |
} |
| 108 |
absl::StatusOr<URI> tmp_url = URI::Parse(url); |
| 109 |
if (!tmp_url.ok()) { |
| 110 |
*error = GRPC_ERROR_CREATE("Invalid Aws request url."); |
| 111 |
return; |
| 112 |
} |
| 113 |
url_ = tmp_url.value(); |
| 114 |
} |
| 115 |
|
| 116 |
std::map<std::string, std::string> AwsRequestSigner::GetSignedRequestHeaders() { |
| 117 |
std::string request_date_full; |
| 118 |
if (!static_request_date_.empty()) { |
| 119 |
if (!request_headers_.empty()) { |
| 120 |
return request_headers_; |
| 121 |
} |
| 122 |
request_date_full = static_request_date_; |
| 123 |
} else { |
| 124 |
absl::Time request_date = absl::Now(); |
| 125 |
request_date_full = |
| 126 |
absl::FormatTime(kXAmzDateFormat, request_date, absl::UTCTimeZone()); |
| 127 |
} |
| 128 |
std::string request_date_short = request_date_full.substr(0, 8); |
| 129 |
// TASK 1: Create a canonical request for Signature Version 4 |
| 130 |
// https://docs.aws.amazon.com/general/latest/gr/sigv4-create-canonical-request.html |
| 131 |
std::vector<absl::string_view> canonical_request_vector; |
| 132 |
// 1. HTTPRequestMethod |
| 133 |
canonical_request_vector.emplace_back(method_); |
| 134 |
canonical_request_vector.emplace_back("\n"); |
| 135 |
// 2. CanonicalURI |
| 136 |
canonical_request_vector.emplace_back( |
| 137 |
url_.path().empty() ? "/" : absl::string_view(url_.path())); |
| 138 |
canonical_request_vector.emplace_back("\n"); |
| 139 |
// 3. CanonicalQueryString |
| 140 |
std::vector<std::string> query_vector; |
| 141 |
for (const URI::QueryParam& query_kv : url_.query_parameter_pairs()) { |
| 142 |
query_vector.emplace_back(absl::StrCat(query_kv.key, "=", query_kv.value)); |
| 143 |
} |
| 144 |
std::string query = absl::StrJoin(query_vector, "&"); |
| 145 |
canonical_request_vector.emplace_back(query); |
| 146 |
canonical_request_vector.emplace_back("\n"); |
| 147 |
// 4. CanonicalHeaders |
| 148 |
if (request_headers_.empty()) { |
| 149 |
request_headers_.insert({"host", url_.authority()}); |
| 150 |
if (!token_.empty()) { |
| 151 |
request_headers_.insert({"x-amz-security-token", token_}); |
| 152 |
} |
| 153 |
for (const auto& header : additional_headers_) { |
| 154 |
request_headers_.insert( |
| 155 |
{absl::AsciiStrToLower(header.first), header.second}); |
| 156 |
} |
| 157 |
} |
| 158 |
if (additional_headers_.find("date") == additional_headers_.end()) { |
| 159 |
request_headers_["x-amz-date"] = request_date_full; |
| 160 |
} |
| 161 |
std::vector<absl::string_view> canonical_headers_vector; |
| 162 |
for (const auto& header : request_headers_) { |
| 163 |
canonical_headers_vector.emplace_back(header.first); |
| 164 |
canonical_headers_vector.emplace_back(":"); |
| 165 |
canonical_headers_vector.emplace_back(header.second); |
| 166 |
canonical_headers_vector.emplace_back("\n"); |
| 167 |
} |
| 168 |
std::string canonical_headers = absl::StrJoin(canonical_headers_vector, ""); |
| 169 |
canonical_request_vector.emplace_back(canonical_headers); |
| 170 |
canonical_request_vector.emplace_back("\n"); |
| 171 |
// 5. SignedHeaders |
| 172 |
std::vector<absl::string_view> signed_headers_vector; |
| 173 |
signed_headers_vector.reserve(request_headers_.size()); |
| 174 |
for (const auto& header : request_headers_) { |
| 175 |
signed_headers_vector.emplace_back(header.first); |
| 176 |
} |
| 177 |
std::string signed_headers = absl::StrJoin(signed_headers_vector, ";"); |
| 178 |
canonical_request_vector.emplace_back(signed_headers); |
| 179 |
canonical_request_vector.emplace_back("\n"); |
| 180 |
// 6. RequestPayload |
| 181 |
std::string hashed_request_payload = SHA256Hex(request_payload_); |
| 182 |
canonical_request_vector.emplace_back(hashed_request_payload); |
| 183 |
std::string canonical_request = absl::StrJoin(canonical_request_vector, ""); |
| 184 |
// TASK 2: Create a string to sign for Signature Version 4 |
| 185 |
// https://docs.aws.amazon.com/general/latest/gr/sigv4-create-string-to-sign.html |
| 186 |
std::vector<absl::string_view> string_to_sign_vector; |
| 187 |
// 1. Algorithm |
| 188 |
string_to_sign_vector.emplace_back("AWS4-HMAC-SHA256"); |
| 189 |
string_to_sign_vector.emplace_back("\n"); |
| 190 |
// 2. RequestDateTime |
| 191 |
string_to_sign_vector.emplace_back(request_date_full); |
| 192 |
string_to_sign_vector.emplace_back("\n"); |
| 193 |
// 3. CredentialScope |
| 194 |
std::pair<absl::string_view, absl::string_view> host_parts = |
| 195 |
absl::StrSplit(url_.authority(), absl::MaxSplits('.', 1)); |
| 196 |
std::string service_name(host_parts.first); |
| 197 |
std::string credential_scope = absl::StrFormat( |
| 198 |
"%s/%s/%s/aws4_request", request_date_short, region_, service_name); |
| 199 |
string_to_sign_vector.emplace_back(credential_scope); |
| 200 |
string_to_sign_vector.emplace_back("\n"); |
| 201 |
// 4. HashedCanonicalRequest |
| 202 |
std::string hashed_canonical_request = SHA256Hex(canonical_request); |
| 203 |
string_to_sign_vector.emplace_back(hashed_canonical_request); |
| 204 |
std::string string_to_sign = absl::StrJoin(string_to_sign_vector, ""); |
| 205 |
// TASK 3: Task 3: Calculate the signature for AWS Signature Version 4 |
| 206 |
// https://docs.aws.amazon.com/general/latest/gr/sigv4-calculate-signature.html |
| 207 |
// 1. Derive your signing key. |
| 208 |
std::string date = HMAC("AWS4" + secret_access_key_, request_date_short); |
| 209 |
std::string region = HMAC(date, region_); |
| 210 |
std::string service = HMAC(region, service_name); |
| 211 |
std::string signing = HMAC(service, "aws4_request"); |
| 212 |
// 2. Calculate the signature. |
| 213 |
std::string signature_str = HMAC(signing, string_to_sign); |
| 214 |
std::string signature = absl::BytesToHexString(signature_str); |
| 215 |
// TASK 4: Add the signature to the HTTP request |
| 216 |
// https://docs.aws.amazon.com/general/latest/gr/sigv4-add-signature-to-request.html |
| 217 |
std::string authorization_header = absl::StrFormat( |
| 218 |
"%s Credential=%s/%s, SignedHeaders=%s, Signature=%s", kAlgorithm, |
| 219 |
access_key_id_, credential_scope, signed_headers, signature); |
| 220 |
request_headers_["Authorization"] = authorization_header; |
| 221 |
return request_headers_; |
| 222 |
} |
| 223 |
|
| 224 |
} // namespace grpc_core |