| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/auth/private/aws_signing.h> |
| 7 |
|
| 8 |
#include <aws/auth/credentials.h> |
| 9 |
#include <aws/auth/private/key_derivation.h> |
| 10 |
#include <aws/auth/signable.h> |
| 11 |
#include <aws/auth/signing.h> |
| 12 |
#include <aws/cal/ecc.h> |
| 13 |
#include <aws/cal/hash.h> |
| 14 |
#include <aws/cal/hmac.h> |
| 15 |
#include <aws/common/date_time.h> |
| 16 |
#include <aws/common/encoding.h> |
| 17 |
#include <aws/common/string.h> |
| 18 |
#include <aws/io/stream.h> |
| 19 |
#include <aws/io/uri.h> |
| 20 |
|
| 21 |
#include <ctype.h> |
| 22 |
#include <inttypes.h> |
| 23 |
|
| 24 |
#if defined(_MSC_VER) |
| 25 |
# pragma warning(disable : 4204) |
| 26 |
#endif |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
#define BODY_READ_BUFFER_SIZE 4096 |
| 36 |
#define CANONICAL_REQUEST_STARTING_SIZE 1024 |
| 37 |
#define STRING_TO_SIGN_STARTING_SIZE 256 |
| 38 |
#define SIGNED_HEADERS_STARTING_SIZE 256 |
| 39 |
#define CANONICAL_HEADER_BLOCK_STARTING_SIZE 1024 |
| 40 |
#define AUTHORIZATION_VALUE_STARTING_SIZE 512 |
| 41 |
#define PAYLOAD_HASH_STARTING_SIZE (AWS_SHA256_LEN * 2) |
| 42 |
#define CREDENTIAL_SCOPE_STARTING_SIZE 128 |
| 43 |
#define ACCESS_CREDENTIAL_SCOPE_STARTING_SIZE 149 |
| 44 |
#define SCRATCH_BUF_STARTING_SIZE 256 |
| 45 |
#define MAX_AUTHORIZATION_HEADER_COUNT 4 |
| 46 |
#define MAX_AUTHORIZATION_QUERY_PARAM_COUNT 6 |
| 47 |
#define DEFAULT_PATH_COMPONENT_COUNT 10 |
| 48 |
#define CANONICAL_REQUEST_SPLIT_OVER_ESTIMATE 20 |
| 49 |
#define HEX_ENCODED_SIGNATURE_OVER_ESTIMATE 256 |
| 50 |
#define MAX_ECDSA_P256_SIGNATURE_AS_BINARY_LENGTH 72 |
| 51 |
#define MAX_ECDSA_P256_SIGNATURE_AS_HEX_LENGTH (MAX_ECDSA_P256_SIGNATURE_AS_BINARY_LENGTH * 2) |
| 52 |
#define AWS_SIGV4A_SIGNATURE_PADDING_BYTE ('*') |
| 53 |
|
| 54 |
AWS_STRING_FROM_LITERAL(g_aws_signing_content_header_name, "x-amz-content-sha256"); |
| 55 |
AWS_STRING_FROM_LITERAL(g_aws_signing_authorization_header_name, "Authorization"); |
| 56 |
AWS_STRING_FROM_LITERAL(g_aws_signing_authorization_query_param_name, "X-Amz-Signature"); |
| 57 |
AWS_STRING_FROM_LITERAL(g_aws_signing_algorithm_query_param_name, "X-Amz-Algorithm"); |
| 58 |
AWS_STRING_FROM_LITERAL(g_aws_signing_credential_query_param_name, "X-Amz-Credential"); |
| 59 |
AWS_STRING_FROM_LITERAL(g_aws_signing_date_name, "X-Amz-Date"); |
| 60 |
AWS_STRING_FROM_LITERAL(g_aws_signing_signed_headers_query_param_name, "X-Amz-SignedHeaders"); |
| 61 |
AWS_STRING_FROM_LITERAL(g_aws_signing_security_token_name, "X-Amz-Security-Token"); |
| 62 |
AWS_STRING_FROM_LITERAL(g_aws_signing_s3session_token_name, "X-Amz-S3session-Token"); |
| 63 |
AWS_STRING_FROM_LITERAL(g_aws_signing_expires_query_param_name, "X-Amz-Expires"); |
| 64 |
AWS_STRING_FROM_LITERAL(g_aws_signing_region_set_name, "X-Amz-Region-Set"); |
| 65 |
|
| 66 |
AWS_STATIC_STRING_FROM_LITERAL(s_signature_type_sigv4_http_request, "AWS4-HMAC-SHA256"); |
| 67 |
AWS_STATIC_STRING_FROM_LITERAL(s_signature_type_sigv4_s3_chunked_payload, "AWS4-HMAC-SHA256-PAYLOAD"); |
| 68 |
AWS_STATIC_STRING_FROM_LITERAL(s_signature_type_sigv4a_s3_chunked_payload, "AWS4-ECDSA-P256-SHA256-PAYLOAD"); |
| 69 |
|
| 70 |
AWS_STATIC_STRING_FROM_LITERAL(s_signature_type_sigv4_s3_chunked_trailer_payload, "AWS4-HMAC-SHA256-TRAILER"); |
| 71 |
AWS_STATIC_STRING_FROM_LITERAL(s_signature_type_sigv4a_s3_chunked_trailer_payload, "AWS4-ECDSA-P256-SHA256-TRAILER"); |
| 72 |
|
| 73 |
|
| 74 |
static struct aws_hash_table s_forbidden_headers; |
| 75 |
static struct aws_hash_table s_forbidden_params; |
| 76 |
static struct aws_hash_table s_skipped_headers; |
| 77 |
|
| 78 |
static struct aws_byte_cursor s_amzn_trace_id_header_name; |
| 79 |
static struct aws_byte_cursor s_user_agent_header_name; |
| 80 |
static struct aws_byte_cursor s_connection_header_name; |
| 81 |
static struct aws_byte_cursor s_sec_websocket_key_header_name; |
| 82 |
static struct aws_byte_cursor s_sec_websocket_protocol_header_name; |
| 83 |
static struct aws_byte_cursor s_sec_websocket_version_header_name; |
| 84 |
static struct aws_byte_cursor s_upgrade_header_name; |
| 85 |
static struct aws_byte_cursor s_transfer_encoding_header_name; |
| 86 |
|
| 87 |
static struct aws_byte_cursor s_amz_content_sha256_header_name; |
| 88 |
static struct aws_byte_cursor s_amz_date_header_name; |
| 89 |
static struct aws_byte_cursor s_authorization_header_name; |
| 90 |
static struct aws_byte_cursor s_region_set_header_name; |
| 91 |
static struct aws_byte_cursor s_amz_security_token_header_name; |
| 92 |
static struct aws_byte_cursor s_amz_s3session_token_header_name; |
| 93 |
|
| 94 |
static struct aws_byte_cursor s_amz_signature_param_name; |
| 95 |
static struct aws_byte_cursor s_amz_date_param_name; |
| 96 |
static struct aws_byte_cursor s_amz_credential_param_name; |
| 97 |
static struct aws_byte_cursor s_amz_algorithm_param_name; |
| 98 |
static struct aws_byte_cursor s_amz_signed_headers_param_name; |
| 99 |
static struct aws_byte_cursor s_amz_security_token_param_name; |
| 100 |
static struct aws_byte_cursor s_amz_expires_param_name; |
| 101 |
static struct aws_byte_cursor s_amz_region_set_param_name; |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
int aws_signing_init_signing_tables(struct aws_allocator *allocator) { |
| 109 |
|
| 110 |
if (aws_hash_table_init( |
| 111 |
&s_skipped_headers, |
| 112 |
allocator, |
| 113 |
10, |
| 114 |
aws_hash_byte_cursor_ptr_ignore_case, |
| 115 |
(aws_hash_callback_eq_fn *)aws_byte_cursor_eq_ignore_case, |
| 116 |
NULL, |
| 117 |
NULL)) { |
| 118 |
return AWS_OP_ERR; |
| 119 |
} |
| 120 |
|
| 121 |
s_amzn_trace_id_header_name = aws_byte_cursor_from_c_str("x-amzn-trace-id"); |
| 122 |
if (aws_hash_table_put(&s_skipped_headers, &s_amzn_trace_id_header_name, NULL, NULL)) { |
| 123 |
return AWS_OP_ERR; |
| 124 |
} |
| 125 |
|
| 126 |
s_user_agent_header_name = aws_byte_cursor_from_c_str("User-Agent"); |
| 127 |
if (aws_hash_table_put(&s_skipped_headers, &s_user_agent_header_name, NULL, NULL)) { |
| 128 |
return AWS_OP_ERR; |
| 129 |
} |
| 130 |
|
| 131 |
s_connection_header_name = aws_byte_cursor_from_c_str("connection"); |
| 132 |
if (aws_hash_table_put(&s_skipped_headers, &s_connection_header_name, NULL, NULL)) { |
| 133 |
return AWS_OP_ERR; |
| 134 |
} |
| 135 |
|
| 136 |
s_connection_header_name = aws_byte_cursor_from_c_str("expect"); |
| 137 |
if (aws_hash_table_put(&s_skipped_headers, &s_connection_header_name, NULL, NULL)) { |
| 138 |
return AWS_OP_ERR; |
| 139 |
} |
| 140 |
|
| 141 |
s_sec_websocket_key_header_name = aws_byte_cursor_from_c_str("sec-websocket-key"); |
| 142 |
if (aws_hash_table_put(&s_skipped_headers, &s_sec_websocket_key_header_name, NULL, NULL)) { |
| 143 |
return AWS_OP_ERR; |
| 144 |
} |
| 145 |
|
| 146 |
s_sec_websocket_protocol_header_name = aws_byte_cursor_from_c_str("sec-websocket-protocol"); |
| 147 |
if (aws_hash_table_put(&s_skipped_headers, &s_sec_websocket_protocol_header_name, NULL, NULL)) { |
| 148 |
return AWS_OP_ERR; |
| 149 |
} |
| 150 |
|
| 151 |
s_sec_websocket_version_header_name = aws_byte_cursor_from_c_str("sec-websocket-version"); |
| 152 |
if (aws_hash_table_put(&s_skipped_headers, &s_sec_websocket_version_header_name, NULL, NULL)) { |
| 153 |
return AWS_OP_ERR; |
| 154 |
} |
| 155 |
|
| 156 |
s_upgrade_header_name = aws_byte_cursor_from_c_str("upgrade"); |
| 157 |
if (aws_hash_table_put(&s_skipped_headers, &s_upgrade_header_name, NULL, NULL)) { |
| 158 |
return AWS_OP_ERR; |
| 159 |
} |
| 160 |
|
| 161 |
s_transfer_encoding_header_name = aws_byte_cursor_from_c_str("transfer-encoding"); |
| 162 |
if (aws_hash_table_put(&s_skipped_headers, &s_transfer_encoding_header_name, NULL, NULL)) { |
| 163 |
return AWS_OP_ERR; |
| 164 |
} |
| 165 |
|
| 166 |
if (aws_hash_table_init( |
| 167 |
&s_forbidden_headers, |
| 168 |
allocator, |
| 169 |
10, |
| 170 |
aws_hash_byte_cursor_ptr_ignore_case, |
| 171 |
(aws_hash_callback_eq_fn *)aws_byte_cursor_eq_ignore_case, |
| 172 |
NULL, |
| 173 |
NULL)) { |
| 174 |
return AWS_OP_ERR; |
| 175 |
} |
| 176 |
|
| 177 |
s_amz_content_sha256_header_name = aws_byte_cursor_from_string(g_aws_signing_content_header_name); |
| 178 |
if (aws_hash_table_put(&s_forbidden_headers, &s_amz_content_sha256_header_name, NULL, NULL)) { |
| 179 |
return AWS_OP_ERR; |
| 180 |
} |
| 181 |
|
| 182 |
s_amz_date_header_name = aws_byte_cursor_from_string(g_aws_signing_date_name); |
| 183 |
if (aws_hash_table_put(&s_forbidden_headers, &s_amz_date_header_name, NULL, NULL)) { |
| 184 |
return AWS_OP_ERR; |
| 185 |
} |
| 186 |
|
| 187 |
s_authorization_header_name = aws_byte_cursor_from_string(g_aws_signing_authorization_header_name); |
| 188 |
if (aws_hash_table_put(&s_forbidden_headers, &s_authorization_header_name, NULL, NULL)) { |
| 189 |
return AWS_OP_ERR; |
| 190 |
} |
| 191 |
|
| 192 |
s_region_set_header_name = aws_byte_cursor_from_string(g_aws_signing_region_set_name); |
| 193 |
if (aws_hash_table_put(&s_forbidden_headers, &s_region_set_header_name, NULL, NULL)) { |
| 194 |
return AWS_OP_ERR; |
| 195 |
} |
| 196 |
|
| 197 |
s_amz_security_token_header_name = aws_byte_cursor_from_string(g_aws_signing_security_token_name); |
| 198 |
if (aws_hash_table_put(&s_forbidden_headers, &s_amz_security_token_header_name, NULL, NULL)) { |
| 199 |
return AWS_OP_ERR; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
s_amz_s3session_token_header_name = aws_byte_cursor_from_string(g_aws_signing_s3session_token_name); |
| 204 |
|
| 205 |
if (aws_hash_table_init( |
| 206 |
&s_forbidden_params, |
| 207 |
allocator, |
| 208 |
10, |
| 209 |
aws_hash_byte_cursor_ptr_ignore_case, |
| 210 |
(aws_hash_callback_eq_fn *)aws_byte_cursor_eq_ignore_case, |
| 211 |
NULL, |
| 212 |
NULL)) { |
| 213 |
return AWS_OP_ERR; |
| 214 |
} |
| 215 |
|
| 216 |
s_amz_signature_param_name = aws_byte_cursor_from_string(g_aws_signing_authorization_query_param_name); |
| 217 |
if (aws_hash_table_put(&s_forbidden_params, &s_amz_signature_param_name, NULL, NULL)) { |
| 218 |
return AWS_OP_ERR; |
| 219 |
} |
| 220 |
|
| 221 |
s_amz_date_param_name = aws_byte_cursor_from_string(g_aws_signing_date_name); |
| 222 |
if (aws_hash_table_put(&s_forbidden_params, &s_amz_date_param_name, NULL, NULL)) { |
| 223 |
return AWS_OP_ERR; |
| 224 |
} |
| 225 |
|
| 226 |
s_amz_credential_param_name = aws_byte_cursor_from_string(g_aws_signing_credential_query_param_name); |
| 227 |
if (aws_hash_table_put(&s_forbidden_params, &s_amz_credential_param_name, NULL, NULL)) { |
| 228 |
return AWS_OP_ERR; |
| 229 |
} |
| 230 |
|
| 231 |
s_amz_algorithm_param_name = aws_byte_cursor_from_string(g_aws_signing_algorithm_query_param_name); |
| 232 |
if (aws_hash_table_put(&s_forbidden_params, &s_amz_algorithm_param_name, NULL, NULL)) { |
| 233 |
return AWS_OP_ERR; |
| 234 |
} |
| 235 |
|
| 236 |
s_amz_signed_headers_param_name = aws_byte_cursor_from_string(g_aws_signing_signed_headers_query_param_name); |
| 237 |
if (aws_hash_table_put(&s_forbidden_params, &s_amz_signed_headers_param_name, NULL, NULL)) { |
| 238 |
return AWS_OP_ERR; |
| 239 |
} |
| 240 |
|
| 241 |
s_amz_security_token_param_name = aws_byte_cursor_from_string(g_aws_signing_security_token_name); |
| 242 |
if (aws_hash_table_put(&s_forbidden_params, &s_amz_security_token_param_name, NULL, NULL)) { |
| 243 |
return AWS_OP_ERR; |
| 244 |
} |
| 245 |
|
| 246 |
s_amz_expires_param_name = aws_byte_cursor_from_string(g_aws_signing_expires_query_param_name); |
| 247 |
if (aws_hash_table_put(&s_forbidden_params, &s_amz_expires_param_name, NULL, NULL)) { |
| 248 |
return AWS_OP_ERR; |
| 249 |
} |
| 250 |
|
| 251 |
s_amz_region_set_param_name = aws_byte_cursor_from_string(g_aws_signing_region_set_name); |
| 252 |
if (aws_hash_table_put(&s_forbidden_params, &s_amz_region_set_param_name, NULL, NULL)) { |
| 253 |
return AWS_OP_ERR; |
| 254 |
} |
| 255 |
|
| 256 |
return AWS_OP_SUCCESS; |
| 257 |
} |
| 258 |
|
| 259 |
void aws_signing_clean_up_signing_tables(void) { |
| 260 |
aws_hash_table_clean_up(&s_skipped_headers); |
| 261 |
aws_hash_table_clean_up(&s_forbidden_headers); |
| 262 |
aws_hash_table_clean_up(&s_forbidden_params); |
| 263 |
} |
| 264 |
|
| 265 |
static bool s_is_header_based_signature_value(enum aws_signature_type signature_type) { |
| 266 |
switch (signature_type) { |
| 267 |
case AWS_ST_HTTP_REQUEST_HEADERS: |
| 268 |
case AWS_ST_CANONICAL_REQUEST_HEADERS: |
| 269 |
return true; |
| 270 |
|
| 271 |
default: |
| 272 |
return false; |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
static bool s_is_query_param_based_signature_value(enum aws_signature_type signature_type) { |
| 277 |
switch (signature_type) { |
| 278 |
case AWS_ST_HTTP_REQUEST_QUERY_PARAMS: |
| 279 |
case AWS_ST_CANONICAL_REQUEST_QUERY_PARAMS: |
| 280 |
return true; |
| 281 |
|
| 282 |
default: |
| 283 |
return false; |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
static int s_get_signature_type_cursor(struct aws_signing_state_aws *state, struct aws_byte_cursor *cursor) { |
| 288 |
switch (state->config.signature_type) { |
| 289 |
case AWS_ST_HTTP_REQUEST_HEADERS: |
| 290 |
case AWS_ST_HTTP_REQUEST_QUERY_PARAMS: |
| 291 |
case AWS_ST_CANONICAL_REQUEST_HEADERS: |
| 292 |
case AWS_ST_CANONICAL_REQUEST_QUERY_PARAMS: |
| 293 |
if (state->config.algorithm == AWS_SIGNING_ALGORITHM_V4_ASYMMETRIC) { |
| 294 |
*cursor = aws_byte_cursor_from_string(g_signature_type_sigv4a_http_request); |
| 295 |
} else { |
| 296 |
*cursor = aws_byte_cursor_from_string(s_signature_type_sigv4_http_request); |
| 297 |
} |
| 298 |
break; |
| 299 |
case AWS_ST_HTTP_REQUEST_CHUNK: |
| 300 |
case AWS_ST_HTTP_REQUEST_EVENT: |
| 301 |
if (state->config.algorithm == AWS_SIGNING_ALGORITHM_V4_ASYMMETRIC) { |
| 302 |
*cursor = aws_byte_cursor_from_string(s_signature_type_sigv4a_s3_chunked_payload); |
| 303 |
} else { |
| 304 |
*cursor = aws_byte_cursor_from_string(s_signature_type_sigv4_s3_chunked_payload); |
| 305 |
} |
| 306 |
break; |
| 307 |
case AWS_ST_HTTP_REQUEST_TRAILING_HEADERS: |
| 308 |
if (state->config.algorithm == AWS_SIGNING_ALGORITHM_V4_ASYMMETRIC) { |
| 309 |
*cursor = aws_byte_cursor_from_string(s_signature_type_sigv4a_s3_chunked_trailer_payload); |
| 310 |
} else { |
| 311 |
*cursor = aws_byte_cursor_from_string(s_signature_type_sigv4_s3_chunked_trailer_payload); |
| 312 |
} |
| 313 |
break; |
| 314 |
|
| 315 |
default: |
| 316 |
return aws_raise_error(AWS_AUTH_SIGNING_UNSUPPORTED_SIGNATURE_TYPE); |
| 317 |
} |
| 318 |
|
| 319 |
return AWS_OP_SUCCESS; |
| 320 |
} |
| 321 |
|
| 322 |
static int s_append_sts_signature_type(struct aws_signing_state_aws *state, struct aws_byte_buf *dest) { |
| 323 |
struct aws_byte_cursor algorithm_cursor; |
| 324 |
if (s_get_signature_type_cursor(state, &algorithm_cursor)) { |
| 325 |
return AWS_OP_ERR; |
| 326 |
} |
| 327 |
|
| 328 |
return aws_byte_buf_append_dynamic(dest, &algorithm_cursor); |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
struct aws_signing_state_aws *aws_signing_state_new( |
| 335 |
struct aws_allocator *allocator, |
| 336 |
const struct aws_signing_config_aws *config, |
| 337 |
const struct aws_signable *signable, |
| 338 |
aws_signing_complete_fn *on_complete, |
| 339 |
void *userdata) { |
| 340 |
|
| 341 |
if (aws_validate_aws_signing_config_aws(config)) { |
| 342 |
return NULL; |
| 343 |
} |
| 344 |
|
| 345 |
struct aws_signing_state_aws *state = aws_mem_calloc(allocator, 1, sizeof(struct aws_signing_state_aws)); |
| 346 |
if (!state) { |
| 347 |
return NULL; |
| 348 |
} |
| 349 |
|
| 350 |
state->allocator = allocator; |
| 351 |
|
| 352 |
|
| 353 |
state->config = *config; |
| 354 |
|
| 355 |
if (state->config.credentials_provider != NULL) { |
| 356 |
aws_credentials_provider_acquire(state->config.credentials_provider); |
| 357 |
} |
| 358 |
|
| 359 |
if (state->config.credentials != NULL) { |
| 360 |
aws_credentials_acquire(state->config.credentials); |
| 361 |
} |
| 362 |
|
| 363 |
if (aws_byte_buf_init_cache_and_update_cursors( |
| 364 |
&state->config_string_buffer, |
| 365 |
allocator, |
| 366 |
&state->config.region, |
| 367 |
&state->config.service, |
| 368 |
&state->config.signed_body_value, |
| 369 |
NULL )) { |
| 370 |
goto on_error; |
| 371 |
} |
| 372 |
|
| 373 |
state->signable = signable; |
| 374 |
state->on_complete = on_complete; |
| 375 |
state->userdata = userdata; |
| 376 |
|
| 377 |
if (aws_signing_result_init(&state->result, allocator)) { |
| 378 |
goto on_error; |
| 379 |
} |
| 380 |
|
| 381 |
if (aws_byte_buf_init(&state->canonical_request, allocator, CANONICAL_REQUEST_STARTING_SIZE) || |
| 382 |
aws_byte_buf_init(&state->string_to_sign, allocator, STRING_TO_SIGN_STARTING_SIZE) || |
| 383 |
aws_byte_buf_init(&state->signed_headers, allocator, SIGNED_HEADERS_STARTING_SIZE) || |
| 384 |
aws_byte_buf_init(&state->canonical_header_block, allocator, CANONICAL_HEADER_BLOCK_STARTING_SIZE) || |
| 385 |
aws_byte_buf_init(&state->payload_hash, allocator, PAYLOAD_HASH_STARTING_SIZE) || |
| 386 |
aws_byte_buf_init(&state->credential_scope, allocator, CREDENTIAL_SCOPE_STARTING_SIZE) || |
| 387 |
aws_byte_buf_init(&state->access_credential_scope, allocator, ACCESS_CREDENTIAL_SCOPE_STARTING_SIZE) || |
| 388 |
aws_byte_buf_init(&state->date, allocator, AWS_DATE_TIME_STR_MAX_LEN) || |
| 389 |
aws_byte_buf_init(&state->signature, allocator, PAYLOAD_HASH_STARTING_SIZE) || |
| 390 |
aws_byte_buf_init(&state->string_to_sign_payload, allocator, PAYLOAD_HASH_STARTING_SIZE) || |
| 391 |
aws_byte_buf_init(&state->scratch_buf, allocator, SCRATCH_BUF_STARTING_SIZE)) { |
| 392 |
|
| 393 |
goto on_error; |
| 394 |
} |
| 395 |
|
| 396 |
snprintf( |
| 397 |
state->expiration_array, AWS_ARRAY_SIZE(state->expiration_array), "%" PRIu64 "", config->expiration_in_seconds); |
| 398 |
|
| 399 |
return state; |
| 400 |
|
| 401 |
on_error: |
| 402 |
aws_signing_state_destroy(state); |
| 403 |
return NULL; |
| 404 |
} |
| 405 |
|
| 406 |
void aws_signing_state_destroy(struct aws_signing_state_aws *state) { |
| 407 |
aws_signing_result_clean_up(&state->result); |
| 408 |
|
| 409 |
aws_credentials_provider_release(state->config.credentials_provider); |
| 410 |
aws_credentials_release(state->config.credentials); |
| 411 |
|
| 412 |
aws_byte_buf_clean_up(&state->config_string_buffer); |
| 413 |
aws_byte_buf_clean_up(&state->canonical_request); |
| 414 |
aws_byte_buf_clean_up(&state->string_to_sign); |
| 415 |
aws_byte_buf_clean_up(&state->signed_headers); |
| 416 |
aws_byte_buf_clean_up(&state->canonical_header_block); |
| 417 |
aws_byte_buf_clean_up(&state->payload_hash); |
| 418 |
aws_byte_buf_clean_up(&state->credential_scope); |
| 419 |
aws_byte_buf_clean_up(&state->access_credential_scope); |
| 420 |
aws_byte_buf_clean_up(&state->date); |
| 421 |
aws_byte_buf_clean_up(&state->signature); |
| 422 |
aws_byte_buf_clean_up(&state->string_to_sign_payload); |
| 423 |
aws_byte_buf_clean_up(&state->scratch_buf); |
| 424 |
|
| 425 |
aws_mem_release(state->allocator, state); |
| 426 |
} |
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
static int s_append_canonical_method(struct aws_signing_state_aws *state) { |
| 436 |
const struct aws_signable *signable = state->signable; |
| 437 |
struct aws_byte_buf *buffer = &state->canonical_request; |
| 438 |
|
| 439 |
struct aws_byte_cursor method_cursor; |
| 440 |
aws_signable_get_property(signable, g_aws_http_method_property_name, &method_cursor); |
| 441 |
|
| 442 |
if (aws_byte_buf_append_dynamic(buffer, &method_cursor)) { |
| 443 |
return AWS_OP_ERR; |
| 444 |
} |
| 445 |
|
| 446 |
if (aws_byte_buf_append_byte_dynamic(buffer, '\n')) { |
| 447 |
return AWS_OP_ERR; |
| 448 |
} |
| 449 |
|
| 450 |
return AWS_OP_SUCCESS; |
| 451 |
} |
| 452 |
|
| 453 |
static int s_append_with_lookup( |
| 454 |
struct aws_byte_buf *dst, |
| 455 |
const struct aws_byte_cursor *src, |
| 456 |
const uint8_t *lookup_table) { |
| 457 |
|
| 458 |
if (aws_byte_buf_reserve_relative(dst, src->len)) { |
| 459 |
return AWS_OP_ERR; |
| 460 |
} |
| 461 |
|
| 462 |
if (aws_byte_buf_append_with_lookup(dst, src, lookup_table)) { |
| 463 |
return AWS_OP_ERR; |
| 464 |
} |
| 465 |
|
| 466 |
return AWS_OP_SUCCESS; |
| 467 |
} |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
static int s_append_normalized_path( |
| 480 |
const struct aws_byte_cursor *raw_path, |
| 481 |
struct aws_allocator *allocator, |
| 482 |
struct aws_byte_buf *dest) { |
| 483 |
|
| 484 |
struct aws_array_list raw_split; |
| 485 |
AWS_ZERO_STRUCT(raw_split); |
| 486 |
|
| 487 |
struct aws_array_list normalized_split; |
| 488 |
AWS_ZERO_STRUCT(normalized_split); |
| 489 |
|
| 490 |
int result = AWS_OP_ERR; |
| 491 |
|
| 492 |
if (aws_array_list_init_dynamic( |
| 493 |
&raw_split, allocator, DEFAULT_PATH_COMPONENT_COUNT, sizeof(struct aws_byte_cursor))) { |
| 494 |
goto cleanup; |
| 495 |
} |
| 496 |
|
| 497 |
if (aws_byte_cursor_split_on_char(raw_path, '/', &raw_split)) { |
| 498 |
goto cleanup; |
| 499 |
} |
| 500 |
|
| 501 |
const size_t raw_split_count = aws_array_list_length(&raw_split); |
| 502 |
if (aws_array_list_init_dynamic(&normalized_split, allocator, raw_split_count, sizeof(struct aws_byte_cursor))) { |
| 503 |
goto cleanup; |
| 504 |
} |
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
for (size_t i = 0; i < raw_split_count; ++i) { |
| 511 |
struct aws_byte_cursor path_component; |
| 512 |
AWS_ZERO_STRUCT(path_component); |
| 513 |
if (aws_array_list_get_at(&raw_split, &path_component, i)) { |
| 514 |
goto cleanup; |
| 515 |
} |
| 516 |
|
| 517 |
if (path_component.len == 0 || (path_component.len == 1 && *path_component.ptr == '.')) { |
| 518 |
|
| 519 |
continue; |
| 520 |
} |
| 521 |
|
| 522 |
if (path_component.len == 2 && *path_component.ptr == '.' && *(path_component.ptr + 1) == '.') { |
| 523 |
|
| 524 |
aws_array_list_pop_back(&normalized_split); |
| 525 |
} else { |
| 526 |
aws_array_list_push_back(&normalized_split, &path_component); |
| 527 |
} |
| 528 |
} |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
bool ends_with_slash = raw_path->len > 0 && raw_path->ptr[raw_path->len - 1] == '/'; |
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
if (aws_byte_buf_append_byte_dynamic(dest, '/')) { |
| 539 |
goto cleanup; |
| 540 |
} |
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
const size_t normalized_split_count = aws_array_list_length(&normalized_split); |
| 547 |
for (size_t i = 0; i < normalized_split_count; ++i) { |
| 548 |
struct aws_byte_cursor normalized_path_component; |
| 549 |
AWS_ZERO_STRUCT(normalized_path_component); |
| 550 |
if (aws_array_list_get_at(&normalized_split, &normalized_path_component, i)) { |
| 551 |
goto cleanup; |
| 552 |
} |
| 553 |
|
| 554 |
if (aws_byte_buf_append_dynamic(dest, &normalized_path_component)) { |
| 555 |
goto cleanup; |
| 556 |
} |
| 557 |
|
| 558 |
if (i + 1 < normalized_split_count || ends_with_slash) { |
| 559 |
if (aws_byte_buf_append_byte_dynamic(dest, '/')) { |
| 560 |
goto cleanup; |
| 561 |
} |
| 562 |
} |
| 563 |
} |
| 564 |
|
| 565 |
result = AWS_OP_SUCCESS; |
| 566 |
|
| 567 |
cleanup: |
| 568 |
|
| 569 |
aws_array_list_clean_up(&raw_split); |
| 570 |
aws_array_list_clean_up(&normalized_split); |
| 571 |
|
| 572 |
return result; |
| 573 |
} |
| 574 |
|
| 575 |
static int s_append_canonical_path(const struct aws_uri *uri, struct aws_signing_state_aws *state) { |
| 576 |
const struct aws_signing_config_aws *config = &state->config; |
| 577 |
struct aws_byte_buf *canonical_request_buffer = &state->canonical_request; |
| 578 |
struct aws_allocator *allocator = state->allocator; |
| 579 |
int result = AWS_OP_ERR; |
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
struct aws_byte_buf normalized_path; |
| 587 |
AWS_ZERO_STRUCT(normalized_path); |
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
if (config->flags.use_double_uri_encode) { |
| 596 |
struct aws_byte_cursor path_cursor; |
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
if (config->flags.should_normalize_uri_path) { |
| 607 |
if (aws_byte_buf_init(&normalized_path, state->allocator, uri->path.len)) { |
| 608 |
goto cleanup; |
| 609 |
} |
| 610 |
|
| 611 |
if (s_append_normalized_path(&uri->path, allocator, &normalized_path)) { |
| 612 |
goto cleanup; |
| 613 |
} |
| 614 |
|
| 615 |
path_cursor = aws_byte_cursor_from_buf(&normalized_path); |
| 616 |
} else { |
| 617 |
path_cursor = uri->path; |
| 618 |
} |
| 619 |
|
| 620 |
if (aws_byte_buf_append_encoding_uri_path(canonical_request_buffer, &path_cursor)) { |
| 621 |
goto cleanup; |
| 622 |
} |
| 623 |
} else { |
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
if (config->flags.should_normalize_uri_path) { |
| 629 |
if (s_append_normalized_path(&uri->path, allocator, canonical_request_buffer)) { |
| 630 |
goto cleanup; |
| 631 |
} |
| 632 |
} else { |
| 633 |
if (aws_byte_buf_append_dynamic(canonical_request_buffer, &uri->path)) { |
| 634 |
goto cleanup; |
| 635 |
} |
| 636 |
} |
| 637 |
} |
| 638 |
|
| 639 |
if (aws_byte_buf_append_byte_dynamic(canonical_request_buffer, '\n')) { |
| 640 |
goto cleanup; |
| 641 |
} |
| 642 |
|
| 643 |
result = AWS_OP_SUCCESS; |
| 644 |
|
| 645 |
cleanup: |
| 646 |
|
| 647 |
aws_byte_buf_clean_up(&normalized_path); |
| 648 |
|
| 649 |
return result; |
| 650 |
} |
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
int s_canonical_query_param_comparator(const void *lhs, const void *rhs) { |
| 656 |
const struct aws_uri_param *left_param = lhs; |
| 657 |
const struct aws_uri_param *right_param = rhs; |
| 658 |
|
| 659 |
int key_compare = aws_byte_cursor_compare_lexical(&left_param->key, &right_param->key); |
| 660 |
if (key_compare != 0) { |
| 661 |
return key_compare; |
| 662 |
} |
| 663 |
|
| 664 |
return aws_byte_cursor_compare_lexical(&left_param->value, &right_param->value); |
| 665 |
} |
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
struct stable_header { |
| 676 |
struct aws_signable_property_list_pair header; |
| 677 |
size_t original_index; |
| 678 |
}; |
| 679 |
|
| 680 |
int s_canonical_header_comparator(const void *lhs, const void *rhs) { |
| 681 |
const struct stable_header *left_header = lhs; |
| 682 |
const struct stable_header *right_header = rhs; |
| 683 |
|
| 684 |
int result = aws_byte_cursor_compare_lookup( |
| 685 |
&left_header->header.name, &right_header->header.name, aws_lookup_table_to_lower_get()); |
| 686 |
if (result != 0) { |
| 687 |
return result; |
| 688 |
} |
| 689 |
|
| 690 |
|
| 691 |
if (left_header->original_index < right_header->original_index) { |
| 692 |
return -1; |
| 693 |
} |
| 694 |
|
| 695 |
|
| 696 |
AWS_ASSERT(left_header->original_index > right_header->original_index); |
| 697 |
|
| 698 |
return 1; |
| 699 |
} |
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
static int s_append_canonical_query_param(struct aws_uri_param *encoded_param, struct aws_byte_buf *buffer) { |
| 705 |
if (aws_byte_buf_append_dynamic(buffer, &encoded_param->key)) { |
| 706 |
return AWS_OP_ERR; |
| 707 |
} |
| 708 |
|
| 709 |
if (aws_byte_buf_append_byte_dynamic(buffer, '=')) { |
| 710 |
return AWS_OP_ERR; |
| 711 |
} |
| 712 |
|
| 713 |
if (aws_byte_buf_append_dynamic(buffer, &encoded_param->value)) { |
| 714 |
return AWS_OP_ERR; |
| 715 |
} |
| 716 |
|
| 717 |
return AWS_OP_SUCCESS; |
| 718 |
} |
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
static int s_add_query_param_to_signing_result( |
| 725 |
struct aws_signing_state_aws *state, |
| 726 |
const struct aws_uri_param *unencoded_param) { |
| 727 |
|
| 728 |
state->scratch_buf.len = 0; |
| 729 |
if (aws_byte_buf_append_encoding_uri_param(&state->scratch_buf, &unencoded_param->key)) { |
| 730 |
return AWS_OP_ERR; |
| 731 |
} |
| 732 |
size_t key_len = state->scratch_buf.len; |
| 733 |
if (aws_byte_buf_append_encoding_uri_param(&state->scratch_buf, &unencoded_param->value)) { |
| 734 |
return AWS_OP_ERR; |
| 735 |
} |
| 736 |
struct aws_byte_cursor encoded_val = aws_byte_cursor_from_buf(&state->scratch_buf); |
| 737 |
struct aws_byte_cursor encoded_key = aws_byte_cursor_advance(&encoded_val, key_len); |
| 738 |
|
| 739 |
if (aws_signing_result_append_property_list( |
| 740 |
&state->result, g_aws_http_query_params_property_list_name, &encoded_key, &encoded_val)) { |
| 741 |
return AWS_OP_ERR; |
| 742 |
} |
| 743 |
|
| 744 |
return AWS_OP_SUCCESS; |
| 745 |
} |
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
static int s_add_authorization_query_param( |
| 753 |
struct aws_signing_state_aws *state, |
| 754 |
struct aws_array_list *unencoded_auth_params, |
| 755 |
const struct aws_uri_param *unencoded_auth_param) { |
| 756 |
|
| 757 |
|
| 758 |
if (aws_array_list_push_back(unencoded_auth_params, unencoded_auth_param)) { |
| 759 |
return AWS_OP_ERR; |
| 760 |
} |
| 761 |
|
| 762 |
return s_add_query_param_to_signing_result(state, unencoded_auth_param); |
| 763 |
} |
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
static bool s_should_sign_header(struct aws_signing_state_aws *state, struct aws_byte_cursor *name) { |
| 770 |
if (state->config.should_sign_header) { |
| 771 |
if (!state->config.should_sign_header(name, state->config.should_sign_header_ud)) { |
| 772 |
return false; |
| 773 |
} |
| 774 |
} |
| 775 |
|
| 776 |
struct aws_hash_element *element = NULL; |
| 777 |
if (aws_hash_table_find(&s_skipped_headers, name, &element) == AWS_OP_ERR || element != NULL) { |
| 778 |
return false; |
| 779 |
} |
| 780 |
|
| 781 |
return true; |
| 782 |
} |
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
static int s_add_authorization_query_params( |
| 790 |
struct aws_signing_state_aws *state, |
| 791 |
struct aws_array_list *unencoded_query_params) { |
| 792 |
|
| 793 |
if (state->config.signature_type != AWS_ST_HTTP_REQUEST_QUERY_PARAMS) { |
| 794 |
return AWS_OP_SUCCESS; |
| 795 |
} |
| 796 |
|
| 797 |
int result = AWS_OP_ERR; |
| 798 |
|
| 799 |
|
| 800 |
struct aws_uri_param algorithm_param = { |
| 801 |
.key = aws_byte_cursor_from_string(g_aws_signing_algorithm_query_param_name), |
| 802 |
}; |
| 803 |
|
| 804 |
if (s_get_signature_type_cursor(state, &algorithm_param.value)) { |
| 805 |
goto done; |
| 806 |
} |
| 807 |
|
| 808 |
if (s_add_authorization_query_param(state, unencoded_query_params, &algorithm_param)) { |
| 809 |
goto done; |
| 810 |
} |
| 811 |
|
| 812 |
|
| 813 |
struct aws_uri_param credential_param = { |
| 814 |
.key = aws_byte_cursor_from_string(g_aws_signing_credential_query_param_name), |
| 815 |
.value = aws_byte_cursor_from_buf(&state->access_credential_scope), |
| 816 |
}; |
| 817 |
|
| 818 |
if (s_add_authorization_query_param(state, unencoded_query_params, &credential_param)) { |
| 819 |
goto done; |
| 820 |
} |
| 821 |
|
| 822 |
|
| 823 |
struct aws_uri_param date_param = { |
| 824 |
.key = aws_byte_cursor_from_string(g_aws_signing_date_name), |
| 825 |
.value = aws_byte_cursor_from_buf(&state->date), |
| 826 |
}; |
| 827 |
|
| 828 |
if (s_add_authorization_query_param(state, unencoded_query_params, &date_param)) { |
| 829 |
goto done; |
| 830 |
} |
| 831 |
|
| 832 |
|
| 833 |
struct aws_uri_param signed_headers_param = { |
| 834 |
.key = aws_byte_cursor_from_string(g_aws_signing_signed_headers_query_param_name), |
| 835 |
.value = aws_byte_cursor_from_buf(&state->signed_headers), |
| 836 |
}; |
| 837 |
|
| 838 |
if (s_add_authorization_query_param(state, unencoded_query_params, &signed_headers_param)) { |
| 839 |
goto done; |
| 840 |
} |
| 841 |
|
| 842 |
|
| 843 |
uint64_t expiration_in_seconds = state->config.expiration_in_seconds; |
| 844 |
if (expiration_in_seconds > 0) { |
| 845 |
struct aws_uri_param expires_param = { |
| 846 |
.key = aws_byte_cursor_from_string(g_aws_signing_expires_query_param_name), |
| 847 |
.value = aws_byte_cursor_from_c_str(state->expiration_array), |
| 848 |
}; |
| 849 |
|
| 850 |
if (s_add_authorization_query_param(state, unencoded_query_params, &expires_param)) { |
| 851 |
goto done; |
| 852 |
} |
| 853 |
} |
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
struct aws_byte_cursor token_header_name; |
| 858 |
if (state->config.algorithm == AWS_SIGNING_ALGORITHM_V4_S3EXPRESS) { |
| 859 |
|
| 860 |
token_header_name = s_amz_s3session_token_header_name; |
| 861 |
} else { |
| 862 |
|
| 863 |
token_header_name = s_amz_security_token_header_name; |
| 864 |
} |
| 865 |
struct aws_byte_cursor session_token_cursor = aws_credentials_get_session_token(state->config.credentials); |
| 866 |
if (session_token_cursor.len > 0) { |
| 867 |
struct aws_uri_param security_token_param = { |
| 868 |
.key = token_header_name, |
| 869 |
.value = session_token_cursor, |
| 870 |
}; |
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
if (state->config.flags.omit_session_token) { |
| 876 |
if (s_add_query_param_to_signing_result(state, &security_token_param)) { |
| 877 |
goto done; |
| 878 |
} |
| 879 |
} else { |
| 880 |
if (s_add_authorization_query_param(state, unencoded_query_params, &security_token_param)) { |
| 881 |
goto done; |
| 882 |
} |
| 883 |
} |
| 884 |
} |
| 885 |
|
| 886 |
|
| 887 |
if (state->config.algorithm == AWS_SIGNING_ALGORITHM_V4_ASYMMETRIC) { |
| 888 |
struct aws_uri_param region_set_param = { |
| 889 |
.key = aws_byte_cursor_from_string(g_aws_signing_region_set_name), |
| 890 |
.value = state->config.region, |
| 891 |
}; |
| 892 |
|
| 893 |
if (s_add_authorization_query_param(state, unencoded_query_params, ®ion_set_param)) { |
| 894 |
goto done; |
| 895 |
} |
| 896 |
} |
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
result = AWS_OP_SUCCESS; |
| 901 |
|
| 902 |
done: |
| 903 |
return result; |
| 904 |
} |
| 905 |
|
| 906 |
static int s_validate_query_params(struct aws_array_list *unencoded_query_params) { |
| 907 |
const size_t param_count = aws_array_list_length(unencoded_query_params); |
| 908 |
for (size_t i = 0; i < param_count; ++i) { |
| 909 |
struct aws_uri_param param; |
| 910 |
AWS_ZERO_STRUCT(param); |
| 911 |
aws_array_list_get_at(unencoded_query_params, ¶m, i); |
| 912 |
|
| 913 |
struct aws_hash_element *forbidden_element = NULL; |
| 914 |
aws_hash_table_find(&s_forbidden_params, ¶m.key, &forbidden_element); |
| 915 |
|
| 916 |
if (forbidden_element != NULL) { |
| 917 |
AWS_LOGF_ERROR( |
| 918 |
AWS_LS_AUTH_SIGNING, |
| 919 |
"AWS authorization query param \"" PRInSTR "\" found in request while signing", |
| 920 |
AWS_BYTE_CURSOR_PRI(param.key)); |
| 921 |
return aws_raise_error(AWS_AUTH_SIGNING_ILLEGAL_REQUEST_QUERY_PARAM); |
| 922 |
} |
| 923 |
} |
| 924 |
|
| 925 |
return AWS_OP_SUCCESS; |
| 926 |
} |
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
static int s_transform_query_params( |
| 934 |
struct aws_signing_state_aws *state, |
| 935 |
struct aws_array_list *param_list, |
| 936 |
struct aws_array_list *temp_strings, |
| 937 |
int (*byte_buf_append_dynamic_param_fn)(struct aws_byte_buf *, const struct aws_byte_cursor *)) { |
| 938 |
|
| 939 |
const size_t param_count = aws_array_list_length(param_list); |
| 940 |
struct aws_uri_param *param = NULL; |
| 941 |
for (size_t i = 0; i < param_count; ++i) { |
| 942 |
aws_array_list_get_at_ptr(param_list, (void **)¶m, i); |
| 943 |
|
| 944 |
|
| 945 |
state->scratch_buf.len = 0; |
| 946 |
if (byte_buf_append_dynamic_param_fn(&state->scratch_buf, ¶m->key)) { |
| 947 |
return AWS_OP_ERR; |
| 948 |
} |
| 949 |
struct aws_string *key_str = aws_string_new_from_buf(state->allocator, &state->scratch_buf); |
| 950 |
if (!key_str) { |
| 951 |
return AWS_OP_ERR; |
| 952 |
} |
| 953 |
if (aws_array_list_push_back(temp_strings, &key_str)) { |
| 954 |
aws_string_destroy(key_str); |
| 955 |
return AWS_OP_ERR; |
| 956 |
} |
| 957 |
|
| 958 |
|
| 959 |
state->scratch_buf.len = 0; |
| 960 |
if (byte_buf_append_dynamic_param_fn(&state->scratch_buf, ¶m->value)) { |
| 961 |
return AWS_OP_ERR; |
| 962 |
} |
| 963 |
struct aws_string *value_str = aws_string_new_from_buf(state->allocator, &state->scratch_buf); |
| 964 |
if (!value_str) { |
| 965 |
return AWS_OP_ERR; |
| 966 |
} |
| 967 |
if (aws_array_list_push_back(temp_strings, &value_str)) { |
| 968 |
aws_string_destroy(value_str); |
| 969 |
return AWS_OP_ERR; |
| 970 |
} |
| 971 |
|
| 972 |
|
| 973 |
param->key = aws_byte_cursor_from_string(key_str); |
| 974 |
param->value = aws_byte_cursor_from_string(value_str); |
| 975 |
} |
| 976 |
|
| 977 |
return AWS_OP_SUCCESS; |
| 978 |
} |
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
static int s_append_canonical_query_string(struct aws_uri *uri, struct aws_signing_state_aws *state) { |
| 986 |
struct aws_allocator *allocator = state->allocator; |
| 987 |
struct aws_byte_buf *canonical_request_buffer = &state->canonical_request; |
| 988 |
|
| 989 |
int result = AWS_OP_ERR; |
| 990 |
struct aws_array_list query_params; |
| 991 |
AWS_ZERO_STRUCT(query_params); |
| 992 |
struct aws_array_list temp_strings; |
| 993 |
AWS_ZERO_STRUCT(temp_strings); |
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
size_t max_param_count = 0; |
| 998 |
struct aws_uri_param param_i; |
| 999 |
AWS_ZERO_STRUCT(param_i); |
| 1000 |
while (aws_uri_query_string_next_param(uri, ¶m_i)) { |
| 1001 |
++max_param_count; |
| 1002 |
} |
| 1003 |
if (state->config.signature_type == AWS_ST_HTTP_REQUEST_QUERY_PARAMS) { |
| 1004 |
max_param_count += MAX_AUTHORIZATION_QUERY_PARAM_COUNT; |
| 1005 |
} |
| 1006 |
if (max_param_count == 0) { |
| 1007 |
goto finish; |
| 1008 |
} |
| 1009 |
|
| 1010 |
|
| 1011 |
if (aws_array_list_init_dynamic(&query_params, allocator, max_param_count, sizeof(struct aws_uri_param))) { |
| 1012 |
goto cleanup; |
| 1013 |
} |
| 1014 |
|
| 1015 |
|
| 1016 |
if (aws_array_list_init_dynamic( |
| 1017 |
&temp_strings, state->allocator, max_param_count * 4, sizeof(struct aws_string *))) { |
| 1018 |
goto cleanup; |
| 1019 |
} |
| 1020 |
|
| 1021 |
|
| 1022 |
if (aws_uri_query_string_params(uri, &query_params)) { |
| 1023 |
goto cleanup; |
| 1024 |
} |
| 1025 |
|
| 1026 |
|
| 1027 |
if (s_transform_query_params(state, &query_params, &temp_strings, aws_byte_buf_append_decoding_uri)) { |
| 1028 |
goto cleanup; |
| 1029 |
} |
| 1030 |
|
| 1031 |
|
| 1032 |
if (s_validate_query_params(&query_params)) { |
| 1033 |
goto cleanup; |
| 1034 |
} |
| 1035 |
|
| 1036 |
|
| 1037 |
if (s_add_authorization_query_params(state, &query_params)) { |
| 1038 |
goto cleanup; |
| 1039 |
} |
| 1040 |
|
| 1041 |
|
| 1042 |
if (s_transform_query_params(state, &query_params, &temp_strings, aws_byte_buf_append_encoding_uri_param)) { |
| 1043 |
goto cleanup; |
| 1044 |
} |
| 1045 |
|
| 1046 |
const size_t param_count = aws_array_list_length(&query_params); |
| 1047 |
|
| 1048 |
|
| 1049 |
qsort(query_params.data, param_count, sizeof(struct aws_uri_param), s_canonical_query_param_comparator); |
| 1050 |
for (size_t i = 0; i < param_count; ++i) { |
| 1051 |
struct aws_uri_param param; |
| 1052 |
AWS_ZERO_STRUCT(param); |
| 1053 |
if (aws_array_list_get_at(&query_params, ¶m, i)) { |
| 1054 |
goto cleanup; |
| 1055 |
} |
| 1056 |
|
| 1057 |
if (s_append_canonical_query_param(¶m, canonical_request_buffer)) { |
| 1058 |
goto cleanup; |
| 1059 |
} |
| 1060 |
|
| 1061 |
if (i + 1 < param_count) { |
| 1062 |
if (aws_byte_buf_append_byte_dynamic(canonical_request_buffer, '&')) { |
| 1063 |
goto cleanup; |
| 1064 |
} |
| 1065 |
} |
| 1066 |
} |
| 1067 |
|
| 1068 |
finish: |
| 1069 |
if (aws_byte_buf_append_byte_dynamic(canonical_request_buffer, '\n')) { |
| 1070 |
goto cleanup; |
| 1071 |
} |
| 1072 |
|
| 1073 |
result = AWS_OP_SUCCESS; |
| 1074 |
|
| 1075 |
cleanup: |
| 1076 |
|
| 1077 |
aws_array_list_clean_up(&query_params); |
| 1078 |
|
| 1079 |
if (aws_array_list_is_valid(&temp_strings)) { |
| 1080 |
const size_t string_count = aws_array_list_length(&temp_strings); |
| 1081 |
for (size_t i = 0; i < string_count; ++i) { |
| 1082 |
struct aws_string *string = NULL; |
| 1083 |
aws_array_list_get_at(&temp_strings, &string, i); |
| 1084 |
aws_string_destroy(string); |
| 1085 |
} |
| 1086 |
aws_array_list_clean_up(&temp_strings); |
| 1087 |
} |
| 1088 |
|
| 1089 |
return result; |
| 1090 |
} |
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
static bool s_is_space(uint8_t value) { |
| 1097 |
return aws_isspace(value); |
| 1098 |
} |
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
|
| 1116 |
static int s_append_canonical_header( |
| 1117 |
struct aws_signing_state_aws *state, |
| 1118 |
struct aws_signable_property_list_pair *header, |
| 1119 |
const struct aws_byte_cursor *last_seen_header_name) { |
| 1120 |
|
| 1121 |
struct aws_byte_buf *canonical_header_buffer = &state->canonical_header_block; |
| 1122 |
struct aws_byte_buf *signed_headers_buffer = &state->signed_headers; |
| 1123 |
const uint8_t *to_lower_table = aws_lookup_table_to_lower_get(); |
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
if (last_seen_header_name == NULL || |
| 1130 |
aws_byte_cursor_compare_lookup(last_seen_header_name, &header->name, aws_lookup_table_to_lower_get()) != 0) { |
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
if (last_seen_header_name) { |
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
if (aws_byte_buf_append_byte_dynamic(canonical_header_buffer, '\n')) { |
| 1140 |
return AWS_OP_ERR; |
| 1141 |
} |
| 1142 |
|
| 1143 |
if (aws_byte_buf_append_byte_dynamic(signed_headers_buffer, ';')) { |
| 1144 |
return AWS_OP_ERR; |
| 1145 |
} |
| 1146 |
} |
| 1147 |
|
| 1148 |
|
| 1149 |
if (s_append_with_lookup(signed_headers_buffer, &header->name, to_lower_table)) { |
| 1150 |
return AWS_OP_ERR; |
| 1151 |
} |
| 1152 |
|
| 1153 |
|
| 1154 |
if (s_append_with_lookup(canonical_header_buffer, &header->name, to_lower_table)) { |
| 1155 |
return AWS_OP_ERR; |
| 1156 |
} |
| 1157 |
|
| 1158 |
if (aws_byte_buf_append_byte_dynamic(canonical_header_buffer, ':')) { |
| 1159 |
return AWS_OP_ERR; |
| 1160 |
} |
| 1161 |
} else { |
| 1162 |
|
| 1163 |
if (aws_byte_buf_append_byte_dynamic(canonical_header_buffer, ',')) { |
| 1164 |
return AWS_OP_ERR; |
| 1165 |
} |
| 1166 |
} |
| 1167 |
|
| 1168 |
|
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
struct aws_byte_cursor trimmed_value = aws_byte_cursor_trim_pred(&header->value, s_is_space); |
| 1175 |
|
| 1176 |
|
| 1177 |
bool in_space = false; |
| 1178 |
uint8_t *start_ptr = trimmed_value.ptr; |
| 1179 |
uint8_t *end_ptr = trimmed_value.ptr + trimmed_value.len; |
| 1180 |
uint8_t *dest_ptr = canonical_header_buffer->buffer + canonical_header_buffer->len; |
| 1181 |
while (start_ptr < end_ptr) { |
| 1182 |
uint8_t value = *start_ptr; |
| 1183 |
bool is_space = s_is_space(value); |
| 1184 |
if (is_space) { |
| 1185 |
value = ' '; |
| 1186 |
} |
| 1187 |
|
| 1188 |
if (!is_space || !in_space) { |
| 1189 |
*dest_ptr++ = value; |
| 1190 |
++canonical_header_buffer->len; |
| 1191 |
} |
| 1192 |
|
| 1193 |
in_space = is_space; |
| 1194 |
|
| 1195 |
++start_ptr; |
| 1196 |
} |
| 1197 |
|
| 1198 |
return AWS_OP_SUCCESS; |
| 1199 |
} |
| 1200 |
|
| 1201 |
|
| 1202 |
static int s_add_authorization_header( |
| 1203 |
struct aws_signing_state_aws *state, |
| 1204 |
struct aws_array_list *stable_header_list, |
| 1205 |
size_t *out_required_capacity, |
| 1206 |
struct aws_byte_cursor name, |
| 1207 |
struct aws_byte_cursor value) { |
| 1208 |
|
| 1209 |
|
| 1210 |
struct stable_header stable_header = { |
| 1211 |
.original_index = aws_array_list_length(stable_header_list), |
| 1212 |
.header = |
| 1213 |
{ |
| 1214 |
.name = name, |
| 1215 |
.value = value, |
| 1216 |
}, |
| 1217 |
}; |
| 1218 |
if (aws_array_list_push_back(stable_header_list, &stable_header)) { |
| 1219 |
return AWS_OP_ERR; |
| 1220 |
} |
| 1221 |
|
| 1222 |
|
| 1223 |
if (aws_signing_result_append_property_list(&state->result, g_aws_http_headers_property_list_name, &name, &value)) { |
| 1224 |
return AWS_OP_ERR; |
| 1225 |
} |
| 1226 |
|
| 1227 |
*out_required_capacity += name.len + value.len; |
| 1228 |
return AWS_OP_SUCCESS; |
| 1229 |
} |
| 1230 |
|
| 1231 |
|
| 1232 |
|
| 1233 |
|
| 1234 |
|
| 1235 |
|
| 1236 |
|
| 1237 |
|
| 1238 |
|
| 1239 |
static int s_build_canonical_stable_header_list( |
| 1240 |
struct aws_signing_state_aws *state, |
| 1241 |
struct aws_array_list *stable_header_list, |
| 1242 |
size_t *out_required_capacity) { |
| 1243 |
|
| 1244 |
AWS_ASSERT(aws_array_list_length(stable_header_list) == 0); |
| 1245 |
|
| 1246 |
*out_required_capacity = 0; |
| 1247 |
const struct aws_signable *signable = state->signable; |
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
|
| 1252 |
struct aws_array_list *signable_header_list = NULL; |
| 1253 |
if (aws_signable_get_property_list(signable, g_aws_http_headers_property_list_name, &signable_header_list)) { |
| 1254 |
return AWS_OP_ERR; |
| 1255 |
} |
| 1256 |
|
| 1257 |
const size_t signable_header_count = aws_array_list_length(signable_header_list); |
| 1258 |
for (size_t i = 0; i < signable_header_count; ++i) { |
| 1259 |
struct stable_header header_wrapper; |
| 1260 |
AWS_ZERO_STRUCT(header_wrapper); |
| 1261 |
header_wrapper.original_index = i; |
| 1262 |
|
| 1263 |
if (aws_array_list_get_at(signable_header_list, &header_wrapper.header, i)) { |
| 1264 |
return AWS_OP_ERR; |
| 1265 |
} |
| 1266 |
|
| 1267 |
struct aws_byte_cursor *header_name_cursor = &header_wrapper.header.name; |
| 1268 |
if (!s_should_sign_header(state, header_name_cursor)) { |
| 1269 |
continue; |
| 1270 |
} |
| 1271 |
|
| 1272 |
*out_required_capacity += header_wrapper.header.name.len + header_wrapper.header.value.len; |
| 1273 |
|
| 1274 |
if (aws_array_list_push_back(stable_header_list, &header_wrapper)) { |
| 1275 |
return AWS_OP_ERR; |
| 1276 |
} |
| 1277 |
} |
| 1278 |
|
| 1279 |
|
| 1280 |
|
| 1281 |
if (state->config.signature_type == AWS_ST_HTTP_REQUEST_HEADERS) { |
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
|
| 1286 |
|
| 1287 |
struct aws_byte_cursor token_header_name; |
| 1288 |
if (state->config.algorithm == AWS_SIGNING_ALGORITHM_V4_S3EXPRESS) { |
| 1289 |
|
| 1290 |
token_header_name = s_amz_s3session_token_header_name; |
| 1291 |
} else { |
| 1292 |
|
| 1293 |
token_header_name = s_amz_security_token_header_name; |
| 1294 |
} |
| 1295 |
struct aws_byte_cursor session_token_cursor = aws_credentials_get_session_token(state->config.credentials); |
| 1296 |
if (session_token_cursor.len > 0) { |
| 1297 |
|
| 1298 |
|
| 1299 |
if (state->config.flags.omit_session_token) { |
| 1300 |
if (aws_signing_result_append_property_list( |
| 1301 |
&state->result, |
| 1302 |
g_aws_http_headers_property_list_name, |
| 1303 |
&token_header_name, |
| 1304 |
&session_token_cursor)) { |
| 1305 |
return AWS_OP_ERR; |
| 1306 |
} |
| 1307 |
} else { |
| 1308 |
if (s_add_authorization_header( |
| 1309 |
state, stable_header_list, out_required_capacity, token_header_name, session_token_cursor)) { |
| 1310 |
return AWS_OP_ERR; |
| 1311 |
} |
| 1312 |
} |
| 1313 |
} |
| 1314 |
|
| 1315 |
|
| 1316 |
|
| 1317 |
|
| 1318 |
if (s_add_authorization_header( |
| 1319 |
state, |
| 1320 |
stable_header_list, |
| 1321 |
out_required_capacity, |
| 1322 |
s_amz_date_header_name, |
| 1323 |
aws_byte_cursor_from_buf(&state->date))) { |
| 1324 |
return AWS_OP_ERR; |
| 1325 |
} |
| 1326 |
|
| 1327 |
*out_required_capacity += g_aws_signing_date_name->len + state->date.len; |
| 1328 |
|
| 1329 |
|
| 1330 |
|
| 1331 |
|
| 1332 |
if (state->config.algorithm == AWS_SIGNING_ALGORITHM_V4_ASYMMETRIC) { |
| 1333 |
if (s_add_authorization_header( |
| 1334 |
state, |
| 1335 |
stable_header_list, |
| 1336 |
out_required_capacity, |
| 1337 |
aws_byte_cursor_from_string(g_aws_signing_region_set_name), |
| 1338 |
state->config.region)) { |
| 1339 |
return AWS_OP_ERR; |
| 1340 |
} |
| 1341 |
} |
| 1342 |
|
| 1343 |
|
| 1344 |
|
| 1345 |
|
| 1346 |
if (state->config.signed_body_header == AWS_SBHT_X_AMZ_CONTENT_SHA256) { |
| 1347 |
if (s_add_authorization_header( |
| 1348 |
state, |
| 1349 |
stable_header_list, |
| 1350 |
out_required_capacity, |
| 1351 |
s_amz_content_sha256_header_name, |
| 1352 |
aws_byte_cursor_from_buf(&state->payload_hash))) { |
| 1353 |
return AWS_OP_ERR; |
| 1354 |
} |
| 1355 |
} |
| 1356 |
|
| 1357 |
|
| 1358 |
} else if ( |
| 1359 |
state->config.signature_type == AWS_ST_HTTP_REQUEST_QUERY_PARAMS && |
| 1360 |
aws_byte_cursor_eq_c_str(&state->config.service, "vpc-lattice-svcs")) { |
| 1361 |
|
| 1362 |
|
| 1363 |
|
| 1364 |
if (s_add_authorization_header( |
| 1365 |
state, |
| 1366 |
stable_header_list, |
| 1367 |
out_required_capacity, |
| 1368 |
s_amz_content_sha256_header_name, |
| 1369 |
g_aws_signed_body_value_unsigned_payload)) { |
| 1370 |
return AWS_OP_ERR; |
| 1371 |
} |
| 1372 |
} |
| 1373 |
|
| 1374 |
*out_required_capacity += aws_array_list_length(stable_header_list) * 2; |
| 1375 |
|
| 1376 |
return AWS_OP_SUCCESS; |
| 1377 |
} |
| 1378 |
|
| 1379 |
static int s_validate_signable_header_list(struct aws_array_list *header_list, enum aws_signing_algorithm algo) { |
| 1380 |
const size_t header_count = aws_array_list_length(header_list); |
| 1381 |
for (size_t i = 0; i < header_count; ++i) { |
| 1382 |
struct aws_signable_property_list_pair header; |
| 1383 |
AWS_ZERO_STRUCT(header); |
| 1384 |
|
| 1385 |
aws_array_list_get_at(header_list, &header, i); |
| 1386 |
|
| 1387 |
struct aws_hash_element *forbidden_element = NULL; |
| 1388 |
aws_hash_table_find(&s_forbidden_headers, &header.name, &forbidden_element); |
| 1389 |
|
| 1390 |
if (forbidden_element != NULL) { |
| 1391 |
AWS_LOGF_ERROR( |
| 1392 |
AWS_LS_AUTH_SIGNING, |
| 1393 |
"AWS authorization header \"" PRInSTR "\" found in request while signing", |
| 1394 |
AWS_BYTE_CURSOR_PRI(header.name)); |
| 1395 |
return aws_raise_error(AWS_AUTH_SIGNING_ILLEGAL_REQUEST_HEADER); |
| 1396 |
} |
| 1397 |
if (algo == AWS_SIGNING_ALGORITHM_V4_S3EXPRESS) { |
| 1398 |
if (aws_byte_cursor_eq_ignore_case(&header.name, &s_amz_s3session_token_header_name)) { |
| 1399 |
AWS_LOGF_ERROR( |
| 1400 |
AWS_LS_AUTH_SIGNING, |
| 1401 |
"AWS authorization header \"" PRInSTR "\" for S3Express found in request while signing", |
| 1402 |
AWS_BYTE_CURSOR_PRI(header.name)); |
| 1403 |
return aws_raise_error(AWS_AUTH_SIGNING_ILLEGAL_REQUEST_HEADER); |
| 1404 |
} |
| 1405 |
} |
| 1406 |
} |
| 1407 |
|
| 1408 |
return AWS_OP_SUCCESS; |
| 1409 |
} |
| 1410 |
|
| 1411 |
static int s_canonicalize_headers(struct aws_signing_state_aws *state) { |
| 1412 |
const struct aws_signable *signable = state->signable; |
| 1413 |
struct aws_allocator *allocator = state->allocator; |
| 1414 |
struct aws_byte_buf *header_buffer = &state->canonical_header_block; |
| 1415 |
|
| 1416 |
AWS_ASSERT(header_buffer->len == 0); |
| 1417 |
|
| 1418 |
int result = AWS_OP_ERR; |
| 1419 |
|
| 1420 |
struct aws_array_list *signable_header_list = NULL; |
| 1421 |
if (aws_signable_get_property_list(signable, g_aws_http_headers_property_list_name, &signable_header_list)) { |
| 1422 |
return AWS_OP_ERR; |
| 1423 |
} |
| 1424 |
|
| 1425 |
if (s_validate_signable_header_list(signable_header_list, state->config.algorithm)) { |
| 1426 |
return AWS_OP_ERR; |
| 1427 |
} |
| 1428 |
|
| 1429 |
const size_t signable_header_count = aws_array_list_length(signable_header_list); |
| 1430 |
|
| 1431 |
|
| 1432 |
size_t headers_reserve_count = signable_header_count + MAX_AUTHORIZATION_HEADER_COUNT; |
| 1433 |
|
| 1434 |
struct aws_array_list headers; |
| 1435 |
if (aws_array_list_init_dynamic(&headers, allocator, headers_reserve_count, sizeof(struct stable_header))) { |
| 1436 |
return AWS_OP_ERR; |
| 1437 |
} |
| 1438 |
|
| 1439 |
size_t header_buffer_reserve_size = 0; |
| 1440 |
if (s_build_canonical_stable_header_list(state, &headers, &header_buffer_reserve_size)) { |
| 1441 |
goto on_cleanup; |
| 1442 |
} |
| 1443 |
|
| 1444 |
|
| 1445 |
|
| 1446 |
|
| 1447 |
|
| 1448 |
if (aws_byte_buf_reserve(header_buffer, header_buffer_reserve_size)) { |
| 1449 |
return AWS_OP_ERR; |
| 1450 |
} |
| 1451 |
|
| 1452 |
const size_t header_count = aws_array_list_length(&headers); |
| 1453 |
|
| 1454 |
|
| 1455 |
qsort(headers.data, header_count, sizeof(struct stable_header), s_canonical_header_comparator); |
| 1456 |
|
| 1457 |
|
| 1458 |
struct aws_byte_cursor *last_seen_header_name = NULL; |
| 1459 |
for (size_t i = 0; i < header_count; ++i) { |
| 1460 |
struct stable_header *wrapper = NULL; |
| 1461 |
if (aws_array_list_get_at_ptr(&headers, (void **)&wrapper, i)) { |
| 1462 |
goto on_cleanup; |
| 1463 |
} |
| 1464 |
|
| 1465 |
if (s_append_canonical_header(state, &wrapper->header, last_seen_header_name)) { |
| 1466 |
goto on_cleanup; |
| 1467 |
} |
| 1468 |
|
| 1469 |
last_seen_header_name = &wrapper->header.name; |
| 1470 |
} |
| 1471 |
|
| 1472 |
|
| 1473 |
|
| 1474 |
if (header_count > 0) { |
| 1475 |
if (aws_byte_buf_append_byte_dynamic(header_buffer, '\n')) { |
| 1476 |
return AWS_OP_ERR; |
| 1477 |
} |
| 1478 |
} |
| 1479 |
|
| 1480 |
result = AWS_OP_SUCCESS; |
| 1481 |
|
| 1482 |
on_cleanup: |
| 1483 |
|
| 1484 |
aws_array_list_clean_up(&headers); |
| 1485 |
|
| 1486 |
return result; |
| 1487 |
} |
| 1488 |
|
| 1489 |
static int s_append_signed_headers(struct aws_signing_state_aws *state) { |
| 1490 |
|
| 1491 |
struct aws_byte_buf *header_buffer = &state->canonical_header_block; |
| 1492 |
struct aws_byte_buf *signed_headers_buffer = &state->signed_headers; |
| 1493 |
|
| 1494 |
if (aws_byte_buf_append_byte_dynamic(header_buffer, '\n')) { |
| 1495 |
return AWS_OP_ERR; |
| 1496 |
} |
| 1497 |
|
| 1498 |
struct aws_byte_cursor signed_headers_cursor = aws_byte_cursor_from_buf(signed_headers_buffer); |
| 1499 |
if (aws_byte_buf_append_dynamic(header_buffer, &signed_headers_cursor)) { |
| 1500 |
return AWS_OP_ERR; |
| 1501 |
} |
| 1502 |
|
| 1503 |
if (aws_byte_buf_append_byte_dynamic(header_buffer, '\n')) { |
| 1504 |
return AWS_OP_ERR; |
| 1505 |
} |
| 1506 |
|
| 1507 |
return AWS_OP_SUCCESS; |
| 1508 |
} |
| 1509 |
|
| 1510 |
|
| 1511 |
|
| 1512 |
|
| 1513 |
|
| 1514 |
|
| 1515 |
static int s_build_canonical_headers(struct aws_signing_state_aws *state) { |
| 1516 |
if (s_canonicalize_headers(state)) { |
| 1517 |
return AWS_OP_ERR; |
| 1518 |
} |
| 1519 |
if (s_append_signed_headers(state)) { |
| 1520 |
return AWS_OP_ERR; |
| 1521 |
} |
| 1522 |
return AWS_OP_SUCCESS; |
| 1523 |
} |
| 1524 |
|
| 1525 |
|
| 1526 |
|
| 1527 |
|
| 1528 |
static int s_build_canonical_payload(struct aws_signing_state_aws *state) { |
| 1529 |
const struct aws_signable *signable = state->signable; |
| 1530 |
struct aws_allocator *allocator = state->allocator; |
| 1531 |
struct aws_byte_buf *payload_hash_buffer = &state->payload_hash; |
| 1532 |
|
| 1533 |
AWS_ASSERT(payload_hash_buffer->len == 0); |
| 1534 |
|
| 1535 |
struct aws_byte_buf body_buffer; |
| 1536 |
AWS_ZERO_STRUCT(body_buffer); |
| 1537 |
struct aws_byte_buf digest_buffer; |
| 1538 |
AWS_ZERO_STRUCT(digest_buffer); |
| 1539 |
|
| 1540 |
struct aws_hash *hash = NULL; |
| 1541 |
|
| 1542 |
int result = AWS_OP_ERR; |
| 1543 |
if (state->config.signature_type == AWS_ST_HTTP_REQUEST_QUERY_PARAMS && |
| 1544 |
aws_byte_cursor_eq_c_str(&state->config.service, "vpc-lattice-svcs")) { |
| 1545 |
|
| 1546 |
|
| 1547 |
if (aws_byte_buf_append_dynamic(payload_hash_buffer, &g_aws_signed_body_value_unsigned_payload) == |
| 1548 |
AWS_OP_SUCCESS) { |
| 1549 |
result = AWS_OP_SUCCESS; |
| 1550 |
} |
| 1551 |
goto on_cleanup; |
| 1552 |
} |
| 1553 |
|
| 1554 |
if (state->config.signed_body_value.len == 0) { |
| 1555 |
|
| 1556 |
hash = aws_sha256_new(allocator); |
| 1557 |
if (hash == NULL) { |
| 1558 |
return AWS_OP_ERR; |
| 1559 |
} |
| 1560 |
|
| 1561 |
if (aws_byte_buf_init(&body_buffer, allocator, BODY_READ_BUFFER_SIZE) || |
| 1562 |
aws_byte_buf_init(&digest_buffer, allocator, AWS_SHA256_LEN)) { |
| 1563 |
goto on_cleanup; |
| 1564 |
} |
| 1565 |
|
| 1566 |
struct aws_input_stream *payload_stream = NULL; |
| 1567 |
if (aws_signable_get_payload_stream(signable, &payload_stream)) { |
| 1568 |
goto on_cleanup; |
| 1569 |
} |
| 1570 |
|
| 1571 |
if (payload_stream != NULL) { |
| 1572 |
if (aws_input_stream_seek(payload_stream, 0, AWS_SSB_BEGIN)) { |
| 1573 |
goto on_cleanup; |
| 1574 |
} |
| 1575 |
|
| 1576 |
struct aws_stream_status payload_status; |
| 1577 |
AWS_ZERO_STRUCT(payload_status); |
| 1578 |
|
| 1579 |
while (!payload_status.is_end_of_stream) { |
| 1580 |
|
| 1581 |
body_buffer.len = 0; |
| 1582 |
if (aws_input_stream_read(payload_stream, &body_buffer)) { |
| 1583 |
goto on_cleanup; |
| 1584 |
} |
| 1585 |
|
| 1586 |
if (body_buffer.len > 0) { |
| 1587 |
struct aws_byte_cursor body_cursor = aws_byte_cursor_from_buf(&body_buffer); |
| 1588 |
aws_hash_update(hash, &body_cursor); |
| 1589 |
} |
| 1590 |
|
| 1591 |
if (aws_input_stream_get_status(payload_stream, &payload_status)) { |
| 1592 |
goto on_cleanup; |
| 1593 |
} |
| 1594 |
} |
| 1595 |
|
| 1596 |
|
| 1597 |
if (aws_input_stream_seek(payload_stream, 0, AWS_SSB_BEGIN)) { |
| 1598 |
goto on_cleanup; |
| 1599 |
} |
| 1600 |
} |
| 1601 |
|
| 1602 |
if (aws_hash_finalize(hash, &digest_buffer, 0)) { |
| 1603 |
goto on_cleanup; |
| 1604 |
} |
| 1605 |
|
| 1606 |
struct aws_byte_cursor digest_cursor = aws_byte_cursor_from_buf(&digest_buffer); |
| 1607 |
if (aws_hex_encode_append_dynamic(&digest_cursor, payload_hash_buffer)) { |
| 1608 |
goto on_cleanup; |
| 1609 |
} |
| 1610 |
} else { |
| 1611 |
|
| 1612 |
if (aws_byte_buf_append_dynamic(payload_hash_buffer, &state->config.signed_body_value)) { |
| 1613 |
goto on_cleanup; |
| 1614 |
} |
| 1615 |
} |
| 1616 |
|
| 1617 |
result = AWS_OP_SUCCESS; |
| 1618 |
|
| 1619 |
on_cleanup: |
| 1620 |
|
| 1621 |
aws_byte_buf_clean_up(&digest_buffer); |
| 1622 |
aws_byte_buf_clean_up(&body_buffer); |
| 1623 |
|
| 1624 |
if (hash) { |
| 1625 |
aws_hash_destroy(hash); |
| 1626 |
} |
| 1627 |
|
| 1628 |
return result; |
| 1629 |
} |
| 1630 |
|
| 1631 |
|
| 1632 |
|
| 1633 |
|
| 1634 |
static int s_append_canonical_payload_hash(struct aws_signing_state_aws *state) { |
| 1635 |
struct aws_byte_buf *canonical_request_buffer = &state->canonical_request; |
| 1636 |
struct aws_byte_buf *payload_hash_buffer = &state->payload_hash; |
| 1637 |
|
| 1638 |
|
| 1639 |
|
| 1640 |
|
| 1641 |
struct aws_byte_cursor payload_hash_cursor = aws_byte_cursor_from_buf(payload_hash_buffer); |
| 1642 |
if (aws_byte_buf_append_dynamic(canonical_request_buffer, &payload_hash_cursor)) { |
| 1643 |
return AWS_OP_ERR; |
| 1644 |
} |
| 1645 |
|
| 1646 |
|
| 1647 |
|
| 1648 |
return AWS_OP_SUCCESS; |
| 1649 |
} |
| 1650 |
|
| 1651 |
AWS_STATIC_STRING_FROM_LITERAL(s_credential_scope_sigv4_terminator, "aws4_request"); |
| 1652 |
|
| 1653 |
static int s_append_credential_scope_terminator(enum aws_signing_algorithm algorithm, struct aws_byte_buf *dest) { |
| 1654 |
struct aws_byte_cursor terminator_cursor; |
| 1655 |
|
| 1656 |
switch (algorithm) { |
| 1657 |
case AWS_SIGNING_ALGORITHM_V4: |
| 1658 |
case AWS_SIGNING_ALGORITHM_V4_S3EXPRESS: |
| 1659 |
case AWS_SIGNING_ALGORITHM_V4_ASYMMETRIC: |
| 1660 |
terminator_cursor = aws_byte_cursor_from_string(s_credential_scope_sigv4_terminator); |
| 1661 |
break; |
| 1662 |
|
| 1663 |
default: |
| 1664 |
return aws_raise_error(AWS_AUTH_SIGNING_UNSUPPORTED_ALGORITHM); |
| 1665 |
} |
| 1666 |
|
| 1667 |
return aws_byte_buf_append_dynamic(dest, &terminator_cursor); |
| 1668 |
} |
| 1669 |
|
| 1670 |
|
| 1671 |
|
| 1672 |
|
| 1673 |
|
| 1674 |
static int s_build_credential_scope(struct aws_signing_state_aws *state) { |
| 1675 |
AWS_ASSERT(state->credential_scope.len == 0); |
| 1676 |
|
| 1677 |
const struct aws_signing_config_aws *config = &state->config; |
| 1678 |
struct aws_byte_buf *dest = &state->credential_scope; |
| 1679 |
|
| 1680 |
|
| 1681 |
|
| 1682 |
|
| 1683 |
if (aws_byte_buf_reserve_relative(dest, AWS_DATE_TIME_STR_MAX_LEN)) { |
| 1684 |
return AWS_OP_ERR; |
| 1685 |
} |
| 1686 |
|
| 1687 |
if (aws_date_time_to_utc_time_short_str(&config->date, AWS_DATE_FORMAT_ISO_8601_BASIC, dest)) { |
| 1688 |
return AWS_OP_ERR; |
| 1689 |
} |
| 1690 |
|
| 1691 |
if (aws_byte_buf_append_byte_dynamic(dest, '/')) { |
| 1692 |
return AWS_OP_ERR; |
| 1693 |
} |
| 1694 |
|
| 1695 |
if (config->algorithm != AWS_SIGNING_ALGORITHM_V4_ASYMMETRIC) { |
| 1696 |
if (aws_byte_buf_append_dynamic(dest, &config->region)) { |
| 1697 |
return AWS_OP_ERR; |
| 1698 |
} |
| 1699 |
|
| 1700 |
if (aws_byte_buf_append_byte_dynamic(dest, '/')) { |
| 1701 |
return AWS_OP_ERR; |
| 1702 |
} |
| 1703 |
} |
| 1704 |
|
| 1705 |
if (aws_byte_buf_append_dynamic(dest, &config->service)) { |
| 1706 |
return AWS_OP_ERR; |
| 1707 |
} |
| 1708 |
|
| 1709 |
if (aws_byte_buf_append_byte_dynamic(dest, '/')) { |
| 1710 |
return AWS_OP_ERR; |
| 1711 |
} |
| 1712 |
|
| 1713 |
if (s_append_credential_scope_terminator(state->config.algorithm, dest)) { |
| 1714 |
return AWS_OP_ERR; |
| 1715 |
} |
| 1716 |
|
| 1717 |
|
| 1718 |
struct aws_byte_cursor access_key_cursor = aws_credentials_get_access_key_id(state->config.credentials); |
| 1719 |
if (aws_byte_buf_append_dynamic(&state->access_credential_scope, &access_key_cursor)) { |
| 1720 |
return AWS_OP_ERR; |
| 1721 |
} |
| 1722 |
|
| 1723 |
if (aws_byte_buf_append_byte_dynamic(&state->access_credential_scope, '/')) { |
| 1724 |
return AWS_OP_ERR; |
| 1725 |
} |
| 1726 |
|
| 1727 |
struct aws_byte_cursor credential_scope_cursor = aws_byte_cursor_from_buf(&state->credential_scope); |
| 1728 |
if (aws_byte_buf_append_dynamic(&state->access_credential_scope, &credential_scope_cursor)) { |
| 1729 |
return AWS_OP_ERR; |
| 1730 |
} |
| 1731 |
|
| 1732 |
return AWS_OP_SUCCESS; |
| 1733 |
} |
| 1734 |
|
| 1735 |
|
| 1736 |
|
| 1737 |
|
| 1738 |
static int s_build_canonical_request_hash(struct aws_signing_state_aws *state) { |
| 1739 |
struct aws_allocator *allocator = state->allocator; |
| 1740 |
struct aws_byte_buf *dest = &state->string_to_sign_payload; |
| 1741 |
|
| 1742 |
int result = AWS_OP_ERR; |
| 1743 |
|
| 1744 |
struct aws_byte_buf digest_buffer; |
| 1745 |
AWS_ZERO_STRUCT(digest_buffer); |
| 1746 |
|
| 1747 |
if (aws_byte_buf_init(&digest_buffer, allocator, AWS_SHA256_LEN)) { |
| 1748 |
goto cleanup; |
| 1749 |
} |
| 1750 |
|
| 1751 |
struct aws_byte_cursor canonical_request_cursor = aws_byte_cursor_from_buf(&state->canonical_request); |
| 1752 |
if (aws_sha256_compute(allocator, &canonical_request_cursor, &digest_buffer, 0)) { |
| 1753 |
goto cleanup; |
| 1754 |
} |
| 1755 |
|
| 1756 |
struct aws_byte_cursor digest_cursor = aws_byte_cursor_from_buf(&digest_buffer); |
| 1757 |
if (aws_hex_encode_append_dynamic(&digest_cursor, dest)) { |
| 1758 |
goto cleanup; |
| 1759 |
} |
| 1760 |
|
| 1761 |
result = AWS_OP_SUCCESS; |
| 1762 |
|
| 1763 |
cleanup: |
| 1764 |
aws_byte_buf_clean_up(&digest_buffer); |
| 1765 |
|
| 1766 |
return result; |
| 1767 |
} |
| 1768 |
|
| 1769 |
|
| 1770 |
|
| 1771 |
|
| 1772 |
|
| 1773 |
|
| 1774 |
|
| 1775 |
|
| 1776 |
|
| 1777 |
|
| 1778 |
|
| 1779 |
|
| 1780 |
|
| 1781 |
|
| 1782 |
|
| 1783 |
|
| 1784 |
|
| 1785 |
|
| 1786 |
|
| 1787 |
|
| 1788 |
|
| 1789 |
|
| 1790 |
static int s_build_string_to_sign_payload_for_event(struct aws_signing_state_aws *state) { |
| 1791 |
int result = AWS_OP_ERR; |
| 1792 |
|
| 1793 |
struct aws_byte_buf *dest = &state->string_to_sign_payload; |
| 1794 |
|
| 1795 |
|
| 1796 |
|
| 1797 |
|
| 1798 |
|
| 1799 |
|
| 1800 |
struct aws_byte_cursor prev_signature_cursor; |
| 1801 |
AWS_ZERO_STRUCT(prev_signature_cursor); |
| 1802 |
if (aws_signable_get_property(state->signable, g_aws_previous_signature_property_name, &prev_signature_cursor)) { |
| 1803 |
AWS_LOGF_ERROR( |
| 1804 |
AWS_LS_AUTH_SIGNING, "(id=%p) Event signable missing previous signature property", (void *)state->signable); |
| 1805 |
return aws_raise_error(AWS_AUTH_SIGNING_MISSING_PREVIOUS_SIGNATURE); |
| 1806 |
} |
| 1807 |
|
| 1808 |
|
| 1809 |
prev_signature_cursor = aws_trim_padded_sigv4a_signature(prev_signature_cursor); |
| 1810 |
|
| 1811 |
if (aws_byte_buf_append_dynamic(dest, &prev_signature_cursor)) { |
| 1812 |
return AWS_OP_ERR; |
| 1813 |
} |
| 1814 |
|
| 1815 |
if (aws_byte_buf_append_byte_dynamic(dest, '\n')) { |
| 1816 |
return AWS_OP_ERR; |
| 1817 |
} |
| 1818 |
|
| 1819 |
|
| 1820 |
|
| 1821 |
|
| 1822 |
|
| 1823 |
|
| 1824 |
|
| 1825 |
|
| 1826 |
|
| 1827 |
|
| 1828 |
|
| 1829 |
struct aws_byte_buf date_buffer; |
| 1830 |
AWS_ZERO_STRUCT(date_buffer); |
| 1831 |
struct aws_byte_buf digest_buffer; |
| 1832 |
AWS_ZERO_STRUCT(digest_buffer); |
| 1833 |
|
| 1834 |
if (aws_byte_buf_init(&date_buffer, state->allocator, 15)) { |
| 1835 |
goto cleanup; |
| 1836 |
} |
| 1837 |
|
| 1838 |
struct aws_byte_cursor header_name = aws_byte_cursor_from_c_str(":date"); |
| 1839 |
AWS_FATAL_ASSERT(aws_byte_buf_write_u8(&date_buffer, (uint8_t)header_name.len)); |
| 1840 |
if (aws_byte_buf_append_dynamic(&date_buffer, &header_name)) { |
| 1841 |
goto cleanup; |
| 1842 |
} |
| 1843 |
|
| 1844 |
|
| 1845 |
AWS_FATAL_ASSERT(aws_byte_buf_write_u8(&date_buffer, 8 )); |
| 1846 |
AWS_FATAL_ASSERT(aws_byte_buf_write_be64(&date_buffer, (int64_t)aws_date_time_as_millis(&state->config.date))); |
| 1847 |
|
| 1848 |
|
| 1849 |
if (aws_byte_buf_init(&digest_buffer, state->allocator, AWS_SHA256_LEN)) { |
| 1850 |
goto cleanup; |
| 1851 |
} |
| 1852 |
|
| 1853 |
struct aws_byte_cursor date_cursor = aws_byte_cursor_from_buf(&date_buffer); |
| 1854 |
if (aws_sha256_compute(state->allocator, &date_cursor, &digest_buffer, 0)) { |
| 1855 |
goto cleanup; |
| 1856 |
} |
| 1857 |
|
| 1858 |
struct aws_byte_cursor digest_cursor = aws_byte_cursor_from_buf(&digest_buffer); |
| 1859 |
if (aws_hex_encode_append_dynamic(&digest_cursor, dest)) { |
| 1860 |
goto cleanup; |
| 1861 |
} |
| 1862 |
|
| 1863 |
if (aws_byte_buf_append_byte_dynamic(dest, '\n')) { |
| 1864 |
goto cleanup; |
| 1865 |
} |
| 1866 |
|
| 1867 |
|
| 1868 |
|
| 1869 |
|
| 1870 |
|
| 1871 |
|
| 1872 |
struct aws_byte_cursor current_chunk_hash_cursor = aws_byte_cursor_from_buf(&state->payload_hash); |
| 1873 |
if (aws_byte_buf_append_dynamic(dest, ¤t_chunk_hash_cursor)) { |
| 1874 |
goto cleanup; |
| 1875 |
} |
| 1876 |
|
| 1877 |
result = AWS_OP_SUCCESS; |
| 1878 |
|
| 1879 |
cleanup: |
| 1880 |
aws_byte_buf_clean_up(&date_buffer); |
| 1881 |
aws_byte_buf_clean_up(&digest_buffer); |
| 1882 |
|
| 1883 |
return result; |
| 1884 |
} |
| 1885 |
|
| 1886 |
static int s_build_canonical_request_body_chunk(struct aws_signing_state_aws *state) { |
| 1887 |
|
| 1888 |
struct aws_byte_buf *dest = &state->string_to_sign_payload; |
| 1889 |
|
| 1890 |
|
| 1891 |
struct aws_byte_cursor prev_signature_cursor; |
| 1892 |
AWS_ZERO_STRUCT(prev_signature_cursor); |
| 1893 |
if (aws_signable_get_property(state->signable, g_aws_previous_signature_property_name, &prev_signature_cursor)) { |
| 1894 |
AWS_LOGF_ERROR( |
| 1895 |
AWS_LS_AUTH_SIGNING, "(id=%p) Chunk signable missing previous signature property", (void *)state->signable); |
| 1896 |
return aws_raise_error(AWS_AUTH_SIGNING_MISSING_PREVIOUS_SIGNATURE); |
| 1897 |
} |
| 1898 |
|
| 1899 |
|
| 1900 |
prev_signature_cursor = aws_trim_padded_sigv4a_signature(prev_signature_cursor); |
| 1901 |
|
| 1902 |
if (aws_byte_buf_append_dynamic(dest, &prev_signature_cursor)) { |
| 1903 |
return AWS_OP_ERR; |
| 1904 |
} |
| 1905 |
|
| 1906 |
if (aws_byte_buf_append_byte_dynamic(dest, '\n')) { |
| 1907 |
return AWS_OP_ERR; |
| 1908 |
} |
| 1909 |
|
| 1910 |
|
| 1911 |
if (aws_byte_buf_append_dynamic(dest, &g_aws_signed_body_value_empty_sha256)) { |
| 1912 |
return AWS_OP_ERR; |
| 1913 |
} |
| 1914 |
|
| 1915 |
if (aws_byte_buf_append_byte_dynamic(dest, '\n')) { |
| 1916 |
return AWS_OP_ERR; |
| 1917 |
} |
| 1918 |
|
| 1919 |
|
| 1920 |
struct aws_byte_cursor current_chunk_hash_cursor = aws_byte_cursor_from_buf(&state->payload_hash); |
| 1921 |
if (aws_byte_buf_append_dynamic(dest, ¤t_chunk_hash_cursor)) { |
| 1922 |
return AWS_OP_ERR; |
| 1923 |
} |
| 1924 |
|
| 1925 |
return AWS_OP_SUCCESS; |
| 1926 |
} |
| 1927 |
|
| 1928 |
static int s_build_canonical_request_trailing_headers(struct aws_signing_state_aws *state) { |
| 1929 |
|
| 1930 |
struct aws_byte_buf *dest = &state->string_to_sign_payload; |
| 1931 |
|
| 1932 |
|
| 1933 |
struct aws_byte_cursor prev_signature_cursor; |
| 1934 |
AWS_ZERO_STRUCT(prev_signature_cursor); |
| 1935 |
if (aws_signable_get_property(state->signable, g_aws_previous_signature_property_name, &prev_signature_cursor)) { |
| 1936 |
AWS_LOGF_ERROR( |
| 1937 |
AWS_LS_AUTH_SIGNING, |
| 1938 |
"(id=%p) trailing_headers signable missing previous signature property", |
| 1939 |
(void *)state->signable); |
| 1940 |
return aws_raise_error(AWS_AUTH_SIGNING_MISSING_PREVIOUS_SIGNATURE); |
| 1941 |
} |
| 1942 |
|
| 1943 |
|
| 1944 |
prev_signature_cursor = aws_trim_padded_sigv4a_signature(prev_signature_cursor); |
| 1945 |
|
| 1946 |
if (aws_byte_buf_append_dynamic(dest, &prev_signature_cursor)) { |
| 1947 |
return AWS_OP_ERR; |
| 1948 |
} |
| 1949 |
|
| 1950 |
if (aws_byte_buf_append_byte_dynamic(dest, '\n')) { |
| 1951 |
return AWS_OP_ERR; |
| 1952 |
} |
| 1953 |
|
| 1954 |
|
| 1955 |
|
| 1956 |
if (s_canonicalize_headers(state)) { |
| 1957 |
return AWS_OP_ERR; |
| 1958 |
} |
| 1959 |
struct aws_byte_cursor header_block_cursor = aws_byte_cursor_from_buf(&state->canonical_header_block); |
| 1960 |
if (aws_byte_buf_append_dynamic(&state->canonical_request, &header_block_cursor)) { |
| 1961 |
return AWS_OP_ERR; |
| 1962 |
} |
| 1963 |
if (s_build_canonical_request_hash(state)) { |
| 1964 |
return AWS_OP_ERR; |
| 1965 |
} |
| 1966 |
return AWS_OP_SUCCESS; |
| 1967 |
} |
| 1968 |
|
| 1969 |
|
| 1970 |
|
| 1971 |
|
| 1972 |
static int s_build_canonical_request_sigv4(struct aws_signing_state_aws *state) { |
| 1973 |
AWS_ASSERT(state->canonical_request.len == 0); |
| 1974 |
AWS_ASSERT(state->payload_hash.len > 0); |
| 1975 |
|
| 1976 |
int result = AWS_OP_ERR; |
| 1977 |
|
| 1978 |
struct aws_uri uri; |
| 1979 |
AWS_ZERO_STRUCT(uri); |
| 1980 |
|
| 1981 |
struct aws_byte_cursor uri_cursor; |
| 1982 |
if (aws_signable_get_property(state->signable, g_aws_http_uri_property_name, &uri_cursor)) { |
| 1983 |
return AWS_OP_ERR; |
| 1984 |
} |
| 1985 |
|
| 1986 |
if (aws_uri_init_parse(&uri, state->allocator, &uri_cursor)) { |
| 1987 |
goto cleanup; |
| 1988 |
} |
| 1989 |
|
| 1990 |
if (s_build_canonical_headers(state)) { |
| 1991 |
goto cleanup; |
| 1992 |
} |
| 1993 |
|
| 1994 |
if (s_append_canonical_method(state)) { |
| 1995 |
goto cleanup; |
| 1996 |
} |
| 1997 |
|
| 1998 |
if (s_append_canonical_path(&uri, state)) { |
| 1999 |
goto cleanup; |
| 2000 |
} |
| 2001 |
|
| 2002 |
if (s_append_canonical_query_string(&uri, state)) { |
| 2003 |
goto cleanup; |
| 2004 |
} |
| 2005 |
|
| 2006 |
struct aws_byte_cursor header_block_cursor = aws_byte_cursor_from_buf(&state->canonical_header_block); |
| 2007 |
if (aws_byte_buf_append_dynamic(&state->canonical_request, &header_block_cursor)) { |
| 2008 |
goto cleanup; |
| 2009 |
} |
| 2010 |
|
| 2011 |
if (s_append_canonical_payload_hash(state)) { |
| 2012 |
goto cleanup; |
| 2013 |
} |
| 2014 |
|
| 2015 |
if (s_build_canonical_request_hash(state)) { |
| 2016 |
goto cleanup; |
| 2017 |
} |
| 2018 |
|
| 2019 |
result = AWS_OP_SUCCESS; |
| 2020 |
|
| 2021 |
cleanup: |
| 2022 |
|
| 2023 |
aws_uri_clean_up(&uri); |
| 2024 |
|
| 2025 |
return result; |
| 2026 |
} |
| 2027 |
|
| 2028 |
|
| 2029 |
|
| 2030 |
|
| 2031 |
|
| 2032 |
static struct aws_byte_cursor s_get_signed_headers_from_canonical_request( |
| 2033 |
struct aws_allocator *allocator, |
| 2034 |
struct aws_byte_cursor canonical_request) { |
| 2035 |
|
| 2036 |
struct aws_byte_cursor header_cursor; |
| 2037 |
AWS_ZERO_STRUCT(header_cursor); |
| 2038 |
|
| 2039 |
struct aws_array_list splits; |
| 2040 |
AWS_ZERO_STRUCT(splits); |
| 2041 |
|
| 2042 |
if (aws_array_list_init_dynamic( |
| 2043 |
&splits, allocator, CANONICAL_REQUEST_SPLIT_OVER_ESTIMATE, sizeof(struct aws_byte_cursor))) { |
| 2044 |
return header_cursor; |
| 2045 |
} |
| 2046 |
|
| 2047 |
if (aws_byte_cursor_split_on_char(&canonical_request, '\n', &splits)) { |
| 2048 |
goto done; |
| 2049 |
} |
| 2050 |
|
| 2051 |
size_t split_count = aws_array_list_length(&splits); |
| 2052 |
|
| 2053 |
if (split_count > 1) { |
| 2054 |
aws_array_list_get_at(&splits, &header_cursor, split_count - 2); |
| 2055 |
} |
| 2056 |
|
| 2057 |
done: |
| 2058 |
|
| 2059 |
aws_array_list_clean_up(&splits); |
| 2060 |
|
| 2061 |
return header_cursor; |
| 2062 |
} |
| 2063 |
|
| 2064 |
|
| 2065 |
|
| 2066 |
|
| 2067 |
static int s_apply_existing_canonical_request(struct aws_signing_state_aws *state) { |
| 2068 |
|
| 2069 |
struct aws_byte_cursor canonical_request_cursor; |
| 2070 |
AWS_ZERO_STRUCT(canonical_request_cursor); |
| 2071 |
if (aws_signable_get_property(state->signable, g_aws_canonical_request_property_name, &canonical_request_cursor)) { |
| 2072 |
return AWS_OP_ERR; |
| 2073 |
} |
| 2074 |
|
| 2075 |
if (aws_byte_buf_append_dynamic(&state->canonical_request, &canonical_request_cursor)) { |
| 2076 |
return AWS_OP_ERR; |
| 2077 |
} |
| 2078 |
|
| 2079 |
struct aws_byte_cursor signed_headers_cursor = |
| 2080 |
s_get_signed_headers_from_canonical_request(state->allocator, canonical_request_cursor); |
| 2081 |
if (aws_byte_buf_append_dynamic(&state->signed_headers, &signed_headers_cursor)) { |
| 2082 |
return AWS_OP_ERR; |
| 2083 |
} |
| 2084 |
|
| 2085 |
if (s_build_canonical_request_hash(state)) { |
| 2086 |
return AWS_OP_ERR; |
| 2087 |
} |
| 2088 |
|
| 2089 |
return AWS_OP_SUCCESS; |
| 2090 |
} |
| 2091 |
|
| 2092 |
|
| 2093 |
|
| 2094 |
|
| 2095 |
|
| 2096 |
|
| 2097 |
int aws_signing_build_canonical_request(struct aws_signing_state_aws *state) { |
| 2098 |
|
| 2099 |
if (aws_date_time_to_utc_time_str(&state->config.date, AWS_DATE_FORMAT_ISO_8601_BASIC, &state->date)) { |
| 2100 |
return AWS_OP_ERR; |
| 2101 |
} |
| 2102 |
|
| 2103 |
if (s_build_canonical_payload(state)) { |
| 2104 |
return AWS_OP_ERR; |
| 2105 |
} |
| 2106 |
|
| 2107 |
if (s_build_credential_scope(state)) { |
| 2108 |
return AWS_OP_ERR; |
| 2109 |
} |
| 2110 |
|
| 2111 |
switch (state->config.signature_type) { |
| 2112 |
case AWS_ST_HTTP_REQUEST_HEADERS: |
| 2113 |
case AWS_ST_HTTP_REQUEST_QUERY_PARAMS: |
| 2114 |
return s_build_canonical_request_sigv4(state); |
| 2115 |
|
| 2116 |
case AWS_ST_HTTP_REQUEST_CHUNK: |
| 2117 |
return s_build_canonical_request_body_chunk(state); |
| 2118 |
case AWS_ST_HTTP_REQUEST_EVENT: |
| 2119 |
return s_build_string_to_sign_payload_for_event(state); |
| 2120 |
|
| 2121 |
case AWS_ST_HTTP_REQUEST_TRAILING_HEADERS: |
| 2122 |
return s_build_canonical_request_trailing_headers(state); |
| 2123 |
|
| 2124 |
case AWS_ST_CANONICAL_REQUEST_HEADERS: |
| 2125 |
case AWS_ST_CANONICAL_REQUEST_QUERY_PARAMS: |
| 2126 |
return s_apply_existing_canonical_request(state); |
| 2127 |
|
| 2128 |
default: |
| 2129 |
return aws_raise_error(AWS_AUTH_SIGNING_UNSUPPORTED_SIGNATURE_TYPE); |
| 2130 |
} |
| 2131 |
} |
| 2132 |
|
| 2133 |
|
| 2134 |
|
| 2135 |
|
| 2136 |
int aws_signing_build_string_to_sign(struct aws_signing_state_aws *state) { |
| 2137 |
|
| 2138 |
AWS_ASSERT(state->string_to_sign_payload.len > 0); |
| 2139 |
AWS_ASSERT(state->credential_scope.len > 0); |
| 2140 |
AWS_ASSERT(state->string_to_sign.len == 0); |
| 2141 |
|
| 2142 |
struct aws_byte_buf *dest = &state->string_to_sign; |
| 2143 |
|
| 2144 |
if (s_append_sts_signature_type(state, dest)) { |
| 2145 |
return AWS_OP_ERR; |
| 2146 |
} |
| 2147 |
|
| 2148 |
if (aws_byte_buf_append_byte_dynamic(dest, '\n')) { |
| 2149 |
return AWS_OP_ERR; |
| 2150 |
} |
| 2151 |
|
| 2152 |
|
| 2153 |
if (aws_byte_buf_reserve_relative(dest, AWS_DATE_TIME_STR_MAX_LEN)) { |
| 2154 |
return AWS_OP_ERR; |
| 2155 |
} |
| 2156 |
|
| 2157 |
struct aws_byte_cursor date_cursor = aws_byte_cursor_from_buf(&state->date); |
| 2158 |
if (aws_byte_buf_append_dynamic(dest, &date_cursor)) { |
| 2159 |
return AWS_OP_ERR; |
| 2160 |
} |
| 2161 |
|
| 2162 |
if (aws_byte_buf_append_byte_dynamic(dest, '\n')) { |
| 2163 |
return AWS_OP_ERR; |
| 2164 |
} |
| 2165 |
|
| 2166 |
struct aws_byte_cursor credential_scope_cursor = aws_byte_cursor_from_buf(&state->credential_scope); |
| 2167 |
if (aws_byte_buf_append_dynamic(dest, &credential_scope_cursor)) { |
| 2168 |
return AWS_OP_ERR; |
| 2169 |
} |
| 2170 |
|
| 2171 |
if (aws_byte_buf_append_byte_dynamic(dest, '\n')) { |
| 2172 |
return AWS_OP_ERR; |
| 2173 |
} |
| 2174 |
|
| 2175 |
struct aws_byte_cursor sts_payload_cursor = aws_byte_cursor_from_buf(&state->string_to_sign_payload); |
| 2176 |
if (aws_byte_buf_append_dynamic(dest, &sts_payload_cursor)) { |
| 2177 |
return AWS_OP_ERR; |
| 2178 |
} |
| 2179 |
|
| 2180 |
return AWS_OP_SUCCESS; |
| 2181 |
} |
| 2182 |
|
| 2183 |
|
| 2184 |
|
| 2185 |
|
| 2186 |
|
| 2187 |
AWS_STATIC_STRING_FROM_LITERAL(s_secret_key_prefix, "AWS4"); |
| 2188 |
|
| 2189 |
|
| 2190 |
|
| 2191 |
|
| 2192 |
|
| 2193 |
static int s_compute_sigv4_signing_key(struct aws_signing_state_aws *state, struct aws_byte_buf *dest) { |
| 2194 |
|
| 2195 |
AWS_ASSERT(dest->len == 0); |
| 2196 |
|
| 2197 |
const struct aws_signing_config_aws *config = &state->config; |
| 2198 |
struct aws_allocator *allocator = state->allocator; |
| 2199 |
|
| 2200 |
int result = AWS_OP_ERR; |
| 2201 |
|
| 2202 |
struct aws_byte_buf secret_key; |
| 2203 |
AWS_ZERO_STRUCT(secret_key); |
| 2204 |
|
| 2205 |
struct aws_byte_buf output; |
| 2206 |
AWS_ZERO_STRUCT(output); |
| 2207 |
|
| 2208 |
struct aws_byte_buf date_buf; |
| 2209 |
AWS_ZERO_STRUCT(date_buf); |
| 2210 |
|
| 2211 |
struct aws_byte_cursor secret_access_key_cursor = aws_credentials_get_secret_access_key(state->config.credentials); |
| 2212 |
if (aws_byte_buf_init(&secret_key, allocator, s_secret_key_prefix->len + secret_access_key_cursor.len) || |
| 2213 |
aws_byte_buf_init(&output, allocator, AWS_SHA256_LEN) || |
| 2214 |
aws_byte_buf_init(&date_buf, allocator, AWS_DATE_TIME_STR_MAX_LEN)) { |
| 2215 |
goto cleanup; |
| 2216 |
} |
| 2217 |
|
| 2218 |
|
| 2219 |
|
| 2220 |
|
| 2221 |
struct aws_byte_cursor prefix_cursor = aws_byte_cursor_from_string(s_secret_key_prefix); |
| 2222 |
if (aws_byte_buf_append_dynamic(&secret_key, &prefix_cursor) || |
| 2223 |
aws_byte_buf_append_dynamic(&secret_key, &secret_access_key_cursor)) { |
| 2224 |
goto cleanup; |
| 2225 |
} |
| 2226 |
|
| 2227 |
|
| 2228 |
|
| 2229 |
|
| 2230 |
if (aws_date_time_to_utc_time_short_str(&config->date, AWS_DATE_FORMAT_ISO_8601_BASIC, &date_buf)) { |
| 2231 |
goto cleanup; |
| 2232 |
} |
| 2233 |
|
| 2234 |
struct aws_byte_cursor date_cursor = aws_byte_cursor_from_buf(&date_buf); |
| 2235 |
struct aws_byte_cursor secret_key_cursor = aws_byte_cursor_from_buf(&secret_key); |
| 2236 |
if (aws_sha256_hmac_compute(allocator, &secret_key_cursor, &date_cursor, &output, 0)) { |
| 2237 |
goto cleanup; |
| 2238 |
} |
| 2239 |
|
| 2240 |
struct aws_byte_cursor chained_key_cursor = aws_byte_cursor_from_buf(&output); |
| 2241 |
output.len = 0; |
| 2242 |
if (aws_sha256_hmac_compute(allocator, &chained_key_cursor, &config->region, &output, 0)) { |
| 2243 |
goto cleanup; |
| 2244 |
} |
| 2245 |
|
| 2246 |
chained_key_cursor = aws_byte_cursor_from_buf(&output); |
| 2247 |
output.len = 0; |
| 2248 |
if (aws_sha256_hmac_compute(allocator, &chained_key_cursor, &config->service, &output, 0)) { |
| 2249 |
goto cleanup; |
| 2250 |
} |
| 2251 |
|
| 2252 |
chained_key_cursor = aws_byte_cursor_from_buf(&output); |
| 2253 |
struct aws_byte_cursor scope_terminator_cursor = aws_byte_cursor_from_string(s_credential_scope_sigv4_terminator); |
| 2254 |
if (aws_sha256_hmac_compute(allocator, &chained_key_cursor, &scope_terminator_cursor, dest, 0)) { |
| 2255 |
goto cleanup; |
| 2256 |
} |
| 2257 |
|
| 2258 |
result = AWS_OP_SUCCESS; |
| 2259 |
|
| 2260 |
cleanup: |
| 2261 |
aws_byte_buf_clean_up_secure(&secret_key); |
| 2262 |
aws_byte_buf_clean_up(&output); |
| 2263 |
aws_byte_buf_clean_up(&date_buf); |
| 2264 |
|
| 2265 |
return result; |
| 2266 |
} |
| 2267 |
|
| 2268 |
|
| 2269 |
|
| 2270 |
|
| 2271 |
static int s_calculate_sigv4_signature_value(struct aws_signing_state_aws *state) { |
| 2272 |
struct aws_allocator *allocator = state->allocator; |
| 2273 |
|
| 2274 |
int result = AWS_OP_ERR; |
| 2275 |
|
| 2276 |
struct aws_byte_buf key; |
| 2277 |
AWS_ZERO_STRUCT(key); |
| 2278 |
|
| 2279 |
struct aws_byte_buf digest; |
| 2280 |
AWS_ZERO_STRUCT(digest); |
| 2281 |
|
| 2282 |
if (aws_byte_buf_init(&key, allocator, AWS_SHA256_LEN) || aws_byte_buf_init(&digest, allocator, AWS_SHA256_LEN)) { |
| 2283 |
goto cleanup; |
| 2284 |
} |
| 2285 |
|
| 2286 |
if (s_compute_sigv4_signing_key(state, &key)) { |
| 2287 |
goto cleanup; |
| 2288 |
} |
| 2289 |
|
| 2290 |
struct aws_byte_cursor key_cursor = aws_byte_cursor_from_buf(&key); |
| 2291 |
struct aws_byte_cursor string_to_sign_cursor = aws_byte_cursor_from_buf(&state->string_to_sign); |
| 2292 |
if (aws_sha256_hmac_compute(allocator, &key_cursor, &string_to_sign_cursor, &digest, 0)) { |
| 2293 |
goto cleanup; |
| 2294 |
} |
| 2295 |
|
| 2296 |
struct aws_byte_cursor digest_cursor = aws_byte_cursor_from_buf(&digest); |
| 2297 |
if (aws_hex_encode_append_dynamic(&digest_cursor, &state->signature)) { |
| 2298 |
goto cleanup; |
| 2299 |
} |
| 2300 |
|
| 2301 |
result = AWS_OP_SUCCESS; |
| 2302 |
|
| 2303 |
cleanup: |
| 2304 |
|
| 2305 |
aws_byte_buf_clean_up(&key); |
| 2306 |
aws_byte_buf_clean_up(&digest); |
| 2307 |
|
| 2308 |
return result; |
| 2309 |
} |
| 2310 |
|
| 2311 |
|
| 2312 |
|
| 2313 |
|
| 2314 |
static int s_calculate_sigv4a_signature_value(struct aws_signing_state_aws *state) { |
| 2315 |
struct aws_allocator *allocator = state->allocator; |
| 2316 |
|
| 2317 |
int result = AWS_OP_ERR; |
| 2318 |
|
| 2319 |
struct aws_byte_buf ecdsa_digest; |
| 2320 |
AWS_ZERO_STRUCT(ecdsa_digest); |
| 2321 |
|
| 2322 |
struct aws_byte_buf sha256_digest; |
| 2323 |
AWS_ZERO_STRUCT(sha256_digest); |
| 2324 |
|
| 2325 |
struct aws_ecc_key_pair *ecc_key = aws_credentials_get_ecc_key_pair(state->config.credentials); |
| 2326 |
if (ecc_key == NULL) { |
| 2327 |
return aws_raise_error(AWS_AUTH_SIGNING_INVALID_CREDENTIALS); |
| 2328 |
} |
| 2329 |
|
| 2330 |
if (aws_byte_buf_init(&ecdsa_digest, allocator, aws_ecc_key_pair_signature_length(ecc_key)) || |
| 2331 |
aws_byte_buf_init(&sha256_digest, allocator, AWS_SHA256_LEN)) { |
| 2332 |
goto cleanup; |
| 2333 |
} |
| 2334 |
|
| 2335 |
struct aws_byte_cursor string_to_sign_cursor = aws_byte_cursor_from_buf(&state->string_to_sign); |
| 2336 |
if (aws_sha256_compute(allocator, &string_to_sign_cursor, &sha256_digest, 0)) { |
| 2337 |
goto cleanup; |
| 2338 |
} |
| 2339 |
|
| 2340 |
struct aws_byte_cursor sha256_digest_cursor = aws_byte_cursor_from_buf(&sha256_digest); |
| 2341 |
if (aws_ecc_key_pair_sign_message(ecc_key, &sha256_digest_cursor, &ecdsa_digest)) { |
| 2342 |
goto cleanup; |
| 2343 |
} |
| 2344 |
|
| 2345 |
struct aws_byte_cursor ecdsa_digest_cursor = aws_byte_cursor_from_buf(&ecdsa_digest); |
| 2346 |
if (aws_hex_encode_append_dynamic(&ecdsa_digest_cursor, &state->signature)) { |
| 2347 |
goto cleanup; |
| 2348 |
} |
| 2349 |
|
| 2350 |
result = AWS_OP_SUCCESS; |
| 2351 |
|
| 2352 |
cleanup: |
| 2353 |
|
| 2354 |
aws_byte_buf_clean_up(&ecdsa_digest); |
| 2355 |
aws_byte_buf_clean_up(&sha256_digest); |
| 2356 |
|
| 2357 |
return result; |
| 2358 |
} |
| 2359 |
|
| 2360 |
|
| 2361 |
|
| 2362 |
|
| 2363 |
int s_calculate_signature_value(struct aws_signing_state_aws *state) { |
| 2364 |
switch (state->config.algorithm) { |
| 2365 |
case AWS_SIGNING_ALGORITHM_V4: |
| 2366 |
case AWS_SIGNING_ALGORITHM_V4_S3EXPRESS: |
| 2367 |
return s_calculate_sigv4_signature_value(state); |
| 2368 |
|
| 2369 |
case AWS_SIGNING_ALGORITHM_V4_ASYMMETRIC: |
| 2370 |
return s_calculate_sigv4a_signature_value(state); |
| 2371 |
|
| 2372 |
default: |
| 2373 |
return aws_raise_error(AWS_AUTH_SIGNING_UNSUPPORTED_ALGORITHM); |
| 2374 |
} |
| 2375 |
} |
| 2376 |
|
| 2377 |
static int s_add_signature_property_to_result_set(struct aws_signing_state_aws *state) { |
| 2378 |
|
| 2379 |
int result = AWS_OP_ERR; |
| 2380 |
|
| 2381 |
struct aws_byte_buf final_signature_buffer; |
| 2382 |
AWS_ZERO_STRUCT(final_signature_buffer); |
| 2383 |
|
| 2384 |
if (aws_byte_buf_init(&final_signature_buffer, state->allocator, HEX_ENCODED_SIGNATURE_OVER_ESTIMATE)) { |
| 2385 |
return AWS_OP_ERR; |
| 2386 |
} |
| 2387 |
|
| 2388 |
struct aws_byte_cursor signature_value = aws_byte_cursor_from_buf(&state->signature); |
| 2389 |
if (aws_byte_buf_append_dynamic(&final_signature_buffer, &signature_value)) { |
| 2390 |
goto cleanup; |
| 2391 |
} |
| 2392 |
|
| 2393 |
if (state->config.algorithm == AWS_SIGNING_ALGORITHM_V4_ASYMMETRIC && |
| 2394 |
(state->config.signature_type == AWS_ST_HTTP_REQUEST_CHUNK || |
| 2395 |
state->config.signature_type == AWS_ST_HTTP_REQUEST_TRAILING_HEADERS)) { |
| 2396 |
if (aws_byte_buf_reserve(&final_signature_buffer, MAX_ECDSA_P256_SIGNATURE_AS_HEX_LENGTH)) { |
| 2397 |
goto cleanup; |
| 2398 |
} |
| 2399 |
|
| 2400 |
if (signature_value.len < MAX_ECDSA_P256_SIGNATURE_AS_HEX_LENGTH) { |
| 2401 |
size_t padding_byte_count = MAX_ECDSA_P256_SIGNATURE_AS_HEX_LENGTH - signature_value.len; |
| 2402 |
if (!aws_byte_buf_write_u8_n( |
| 2403 |
&final_signature_buffer, AWS_SIGV4A_SIGNATURE_PADDING_BYTE, padding_byte_count)) { |
| 2404 |
goto cleanup; |
| 2405 |
} |
| 2406 |
} |
| 2407 |
} |
| 2408 |
|
| 2409 |
signature_value = aws_byte_cursor_from_buf(&final_signature_buffer); |
| 2410 |
if (aws_signing_result_set_property(&state->result, g_aws_signature_property_name, &signature_value)) { |
| 2411 |
return AWS_OP_ERR; |
| 2412 |
} |
| 2413 |
|
| 2414 |
result = AWS_OP_SUCCESS; |
| 2415 |
|
| 2416 |
cleanup: |
| 2417 |
|
| 2418 |
aws_byte_buf_clean_up(&final_signature_buffer); |
| 2419 |
|
| 2420 |
return result; |
| 2421 |
} |
| 2422 |
|
| 2423 |
|
| 2424 |
|
| 2425 |
|
| 2426 |
static int s_add_authorization_to_result( |
| 2427 |
struct aws_signing_state_aws *state, |
| 2428 |
struct aws_byte_buf *authorization_value) { |
| 2429 |
struct aws_byte_cursor name; |
| 2430 |
struct aws_byte_cursor value = aws_byte_cursor_from_buf(authorization_value); |
| 2431 |
|
| 2432 |
if (s_is_header_based_signature_value(state->config.signature_type)) { |
| 2433 |
name = aws_byte_cursor_from_string(g_aws_signing_authorization_header_name); |
| 2434 |
if (aws_signing_result_append_property_list( |
| 2435 |
&state->result, g_aws_http_headers_property_list_name, &name, &value)) { |
| 2436 |
return AWS_OP_ERR; |
| 2437 |
} |
| 2438 |
} |
| 2439 |
|
| 2440 |
if (s_is_query_param_based_signature_value(state->config.signature_type)) { |
| 2441 |
name = aws_byte_cursor_from_string(g_aws_signing_authorization_query_param_name); |
| 2442 |
if (aws_signing_result_append_property_list( |
| 2443 |
&state->result, g_aws_http_query_params_property_list_name, &name, &value)) { |
| 2444 |
return AWS_OP_ERR; |
| 2445 |
} |
| 2446 |
} |
| 2447 |
|
| 2448 |
|
| 2449 |
|
| 2450 |
|
| 2451 |
if (s_add_signature_property_to_result_set(state)) { |
| 2452 |
return AWS_OP_ERR; |
| 2453 |
} |
| 2454 |
|
| 2455 |
return AWS_OP_SUCCESS; |
| 2456 |
} |
| 2457 |
|
| 2458 |
AWS_STATIC_STRING_FROM_LITERAL(s_credential_prefix, " Credential="); |
| 2459 |
AWS_STATIC_STRING_FROM_LITERAL(s_signed_headers_prefix, ", SignedHeaders="); |
| 2460 |
AWS_STATIC_STRING_FROM_LITERAL(s_signature_prefix, ", Signature="); |
| 2461 |
|
| 2462 |
|
| 2463 |
|
| 2464 |
|
| 2465 |
|
| 2466 |
|
| 2467 |
|
| 2468 |
|
| 2469 |
|
| 2470 |
|
| 2471 |
static int s_append_authorization_header_preamble(struct aws_signing_state_aws *state, struct aws_byte_buf *dest) { |
| 2472 |
if (s_append_sts_signature_type(state, dest)) { |
| 2473 |
return AWS_OP_ERR; |
| 2474 |
} |
| 2475 |
|
| 2476 |
struct aws_byte_cursor credential_cursor = aws_byte_cursor_from_string(s_credential_prefix); |
| 2477 |
if (aws_byte_buf_append_dynamic(dest, &credential_cursor)) { |
| 2478 |
return AWS_OP_ERR; |
| 2479 |
} |
| 2480 |
|
| 2481 |
struct aws_byte_cursor access_key_cursor = aws_credentials_get_access_key_id(state->config.credentials); |
| 2482 |
if (aws_byte_buf_append_dynamic(dest, &access_key_cursor)) { |
| 2483 |
return AWS_OP_ERR; |
| 2484 |
} |
| 2485 |
|
| 2486 |
if (aws_byte_buf_append_byte_dynamic(dest, '/')) { |
| 2487 |
return AWS_OP_ERR; |
| 2488 |
} |
| 2489 |
|
| 2490 |
struct aws_byte_cursor credential_scope_cursor = aws_byte_cursor_from_buf(&state->credential_scope); |
| 2491 |
if (aws_byte_buf_append_dynamic(dest, &credential_scope_cursor)) { |
| 2492 |
return AWS_OP_ERR; |
| 2493 |
} |
| 2494 |
|
| 2495 |
struct aws_byte_cursor signed_headers_prefix_cursor = aws_byte_cursor_from_string(s_signed_headers_prefix); |
| 2496 |
if (aws_byte_buf_append_dynamic(dest, &signed_headers_prefix_cursor)) { |
| 2497 |
return AWS_OP_ERR; |
| 2498 |
} |
| 2499 |
|
| 2500 |
struct aws_byte_cursor signed_headers_cursor = aws_byte_cursor_from_buf(&state->signed_headers); |
| 2501 |
if (aws_byte_buf_append_dynamic(dest, &signed_headers_cursor)) { |
| 2502 |
return AWS_OP_ERR; |
| 2503 |
} |
| 2504 |
|
| 2505 |
struct aws_byte_cursor signature_prefix_cursor = aws_byte_cursor_from_string(s_signature_prefix); |
| 2506 |
if (aws_byte_buf_append_dynamic(dest, &signature_prefix_cursor)) { |
| 2507 |
return AWS_OP_ERR; |
| 2508 |
} |
| 2509 |
|
| 2510 |
return AWS_OP_SUCCESS; |
| 2511 |
} |
| 2512 |
|
| 2513 |
|
| 2514 |
|
| 2515 |
|
| 2516 |
|
| 2517 |
int aws_signing_build_authorization_value(struct aws_signing_state_aws *state) { |
| 2518 |
AWS_ASSERT(state->string_to_sign.len > 0); |
| 2519 |
AWS_ASSERT(state->credential_scope.len > 0); |
| 2520 |
|
| 2521 |
int result = AWS_OP_ERR; |
| 2522 |
|
| 2523 |
struct aws_byte_buf authorization_value; |
| 2524 |
|
| 2525 |
if (aws_byte_buf_init(&authorization_value, state->allocator, AUTHORIZATION_VALUE_STARTING_SIZE)) { |
| 2526 |
goto cleanup; |
| 2527 |
} |
| 2528 |
|
| 2529 |
if (s_is_header_based_signature_value(state->config.signature_type) && |
| 2530 |
s_append_authorization_header_preamble(state, &authorization_value)) { |
| 2531 |
goto cleanup; |
| 2532 |
} |
| 2533 |
|
| 2534 |
if (s_calculate_signature_value(state)) { |
| 2535 |
goto cleanup; |
| 2536 |
} |
| 2537 |
|
| 2538 |
struct aws_byte_cursor signature_cursor = aws_byte_cursor_from_buf(&state->signature); |
| 2539 |
if (aws_byte_buf_append_dynamic(&authorization_value, &signature_cursor)) { |
| 2540 |
goto cleanup; |
| 2541 |
} |
| 2542 |
|
| 2543 |
if (s_add_authorization_to_result(state, &authorization_value)) { |
| 2544 |
goto cleanup; |
| 2545 |
} |
| 2546 |
|
| 2547 |
AWS_LOGF_INFO( |
| 2548 |
AWS_LS_AUTH_SIGNING, |
| 2549 |
"(id=%p) Http request successfully built final authorization value via algorithm %s, with contents " |
| 2550 |
"\n" PRInSTR "\n", |
| 2551 |
(void *)state->signable, |
| 2552 |
aws_signing_algorithm_to_string(state->config.algorithm), |
| 2553 |
AWS_BYTE_BUF_PRI(authorization_value)); |
| 2554 |
|
| 2555 |
result = AWS_OP_SUCCESS; |
| 2556 |
|
| 2557 |
cleanup: |
| 2558 |
aws_byte_buf_clean_up(&authorization_value); |
| 2559 |
|
| 2560 |
return result; |
| 2561 |
} |
| 2562 |
|
| 2563 |
int aws_validate_v4a_authorization_value( |
| 2564 |
struct aws_allocator *allocator, |
| 2565 |
struct aws_ecc_key_pair *ecc_key, |
| 2566 |
struct aws_byte_cursor string_to_sign_cursor, |
| 2567 |
struct aws_byte_cursor signature_value_cursor) { |
| 2568 |
|
| 2569 |
AWS_LOGF_DEBUG( |
| 2570 |
AWS_LS_AUTH_SIGNING, |
| 2571 |
"(id=%p) Verifying v4a auth value: \n" PRInSTR "\n\nusing string-to-sign: \n" PRInSTR "\n\n", |
| 2572 |
(void *)ecc_key, |
| 2573 |
AWS_BYTE_CURSOR_PRI(signature_value_cursor), |
| 2574 |
AWS_BYTE_CURSOR_PRI(string_to_sign_cursor)); |
| 2575 |
|
| 2576 |
signature_value_cursor = aws_trim_padded_sigv4a_signature(signature_value_cursor); |
| 2577 |
|
| 2578 |
size_t binary_length = 0; |
| 2579 |
if (aws_hex_compute_decoded_len(signature_value_cursor.len, &binary_length)) { |
| 2580 |
return AWS_OP_ERR; |
| 2581 |
} |
| 2582 |
|
| 2583 |
int result = AWS_OP_ERR; |
| 2584 |
|
| 2585 |
struct aws_byte_buf binary_signature; |
| 2586 |
AWS_ZERO_STRUCT(binary_signature); |
| 2587 |
|
| 2588 |
struct aws_byte_buf sha256_digest; |
| 2589 |
AWS_ZERO_STRUCT(sha256_digest); |
| 2590 |
|
| 2591 |
if (aws_byte_buf_init(&binary_signature, allocator, binary_length) || |
| 2592 |
aws_byte_buf_init(&sha256_digest, allocator, AWS_SHA256_LEN)) { |
| 2593 |
goto done; |
| 2594 |
} |
| 2595 |
|
| 2596 |
if (aws_hex_decode(&signature_value_cursor, &binary_signature)) { |
| 2597 |
goto done; |
| 2598 |
} |
| 2599 |
|
| 2600 |
if (aws_sha256_compute(allocator, &string_to_sign_cursor, &sha256_digest, 0)) { |
| 2601 |
goto done; |
| 2602 |
} |
| 2603 |
|
| 2604 |
struct aws_byte_cursor binary_signature_cursor = |
| 2605 |
aws_byte_cursor_from_array(binary_signature.buffer, binary_signature.len); |
| 2606 |
struct aws_byte_cursor digest_cursor = aws_byte_cursor_from_buf(&sha256_digest); |
| 2607 |
if (aws_ecc_key_pair_verify_signature(ecc_key, &digest_cursor, &binary_signature_cursor)) { |
| 2608 |
goto done; |
| 2609 |
} |
| 2610 |
|
| 2611 |
result = AWS_OP_SUCCESS; |
| 2612 |
|
| 2613 |
done: |
| 2614 |
|
| 2615 |
aws_byte_buf_clean_up(&binary_signature); |
| 2616 |
aws_byte_buf_clean_up(&sha256_digest); |
| 2617 |
|
| 2618 |
return result; |
| 2619 |
} |
| 2620 |
|
| 2621 |
int aws_verify_sigv4a_signing( |
| 2622 |
struct aws_allocator *allocator, |
| 2623 |
const struct aws_signable *signable, |
| 2624 |
const struct aws_signing_config_base *base_config, |
| 2625 |
struct aws_byte_cursor expected_canonical_request_cursor, |
| 2626 |
struct aws_byte_cursor signature_cursor, |
| 2627 |
struct aws_byte_cursor ecc_key_pub_x, |
| 2628 |
struct aws_byte_cursor ecc_key_pub_y) { |
| 2629 |
|
| 2630 |
int result = AWS_OP_ERR; |
| 2631 |
|
| 2632 |
if (base_config->config_type != AWS_SIGNING_CONFIG_AWS) { |
| 2633 |
AWS_LOGF_ERROR(AWS_LS_AUTH_SIGNING, "Signing config is not an AWS signing config"); |
| 2634 |
return aws_raise_error(AWS_AUTH_SIGNING_MISMATCHED_CONFIGURATION); |
| 2635 |
} |
| 2636 |
|
| 2637 |
if (aws_validate_aws_signing_config_aws((void *)base_config)) { |
| 2638 |
AWS_LOGF_ERROR(AWS_LS_AUTH_SIGNING, "Signing config failed validation"); |
| 2639 |
return aws_raise_error(AWS_AUTH_SIGNING_INVALID_CONFIGURATION); |
| 2640 |
} |
| 2641 |
|
| 2642 |
const struct aws_signing_config_aws *config = (void *)base_config; |
| 2643 |
if (config->algorithm != AWS_SIGNING_ALGORITHM_V4_ASYMMETRIC) { |
| 2644 |
AWS_LOGF_ERROR(AWS_LS_AUTH_SIGNING, "Signing algorithm is not V4_ASYMMETRIC"); |
| 2645 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 2646 |
} |
| 2647 |
|
| 2648 |
if (config->credentials == NULL) { |
| 2649 |
AWS_LOGF_ERROR(AWS_LS_AUTH_SIGNING, "AWS credentials were not provided/null"); |
| 2650 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 2651 |
} |
| 2652 |
|
| 2653 |
struct aws_signing_state_aws *signing_state = aws_signing_state_new(allocator, config, signable, NULL, NULL); |
| 2654 |
if (!signing_state) { |
| 2655 |
AWS_LOGF_ERROR(AWS_LS_AUTH_SIGNING, "Unable to create new signing state"); |
| 2656 |
return AWS_OP_ERR; |
| 2657 |
} |
| 2658 |
|
| 2659 |
AWS_LOGF_DEBUG( |
| 2660 |
AWS_LS_AUTH_SIGNING, |
| 2661 |
"(id=%p) Verifying v4a signature: \n" PRInSTR "\n\nagainst expected canonical request: \n" PRInSTR |
| 2662 |
"\n\nusing ecc key:\n X:" PRInSTR "\n Y:" PRInSTR "\n\n", |
| 2663 |
(void *)signable, |
| 2664 |
AWS_BYTE_CURSOR_PRI(signature_cursor), |
| 2665 |
AWS_BYTE_CURSOR_PRI(expected_canonical_request_cursor), |
| 2666 |
AWS_BYTE_CURSOR_PRI(ecc_key_pub_x), |
| 2667 |
AWS_BYTE_CURSOR_PRI(ecc_key_pub_y)); |
| 2668 |
|
| 2669 |
struct aws_ecc_key_pair *verification_key = |
| 2670 |
aws_ecc_key_new_from_hex_coordinates(allocator, AWS_CAL_ECDSA_P256, ecc_key_pub_x, ecc_key_pub_y); |
| 2671 |
if (verification_key == NULL) { |
| 2672 |
AWS_LOGF_ERROR(AWS_LS_AUTH_SIGNING, "Unable to create an ECC key from provided coordinates"); |
| 2673 |
goto done; |
| 2674 |
} |
| 2675 |
|
| 2676 |
if (aws_credentials_get_ecc_key_pair(signing_state->config.credentials) == NULL) { |
| 2677 |
struct aws_credentials *ecc_credentials = |
| 2678 |
aws_credentials_new_ecc_from_aws_credentials(allocator, signing_state->config.credentials); |
| 2679 |
aws_credentials_release(signing_state->config.credentials); |
| 2680 |
signing_state->config.credentials = ecc_credentials; |
| 2681 |
if (signing_state->config.credentials == NULL) { |
| 2682 |
AWS_LOGF_ERROR(AWS_LS_AUTH_SIGNING, "Unable to create ECC from provided credentials"); |
| 2683 |
goto done; |
| 2684 |
} |
| 2685 |
} |
| 2686 |
|
| 2687 |
if (aws_signing_build_canonical_request(signing_state)) { |
| 2688 |
AWS_LOGF_ERROR(AWS_LS_AUTH_SIGNING, "Unable to canonicalize request for signing"); |
| 2689 |
goto done; |
| 2690 |
} |
| 2691 |
|
| 2692 |
struct aws_byte_cursor canonical_request_cursor = aws_byte_cursor_from_buf(&signing_state->canonical_request); |
| 2693 |
if (aws_byte_cursor_compare_lexical(&expected_canonical_request_cursor, &canonical_request_cursor) != 0) { |
| 2694 |
AWS_LOGF_ERROR(AWS_LS_AUTH_SIGNING, "Canonicalized request and expected canonical request do not match"); |
| 2695 |
aws_raise_error(AWS_AUTH_CANONICAL_REQUEST_MISMATCH); |
| 2696 |
goto done; |
| 2697 |
} |
| 2698 |
|
| 2699 |
if (aws_signing_build_string_to_sign(signing_state)) { |
| 2700 |
AWS_LOGF_ERROR(AWS_LS_AUTH_SIGNING, "Unable to build string to sign from canonical request"); |
| 2701 |
goto done; |
| 2702 |
} |
| 2703 |
|
| 2704 |
if (aws_validate_v4a_authorization_value( |
| 2705 |
allocator, verification_key, aws_byte_cursor_from_buf(&signing_state->string_to_sign), signature_cursor)) { |
| 2706 |
AWS_LOGF_ERROR(AWS_LS_AUTH_SIGNING, "Signature does not validate"); |
| 2707 |
aws_raise_error(AWS_AUTH_SIGV4A_SIGNATURE_VALIDATION_FAILURE); |
| 2708 |
goto done; |
| 2709 |
} |
| 2710 |
|
| 2711 |
result = AWS_OP_SUCCESS; |
| 2712 |
|
| 2713 |
done: |
| 2714 |
|
| 2715 |
if (verification_key) { |
| 2716 |
aws_ecc_key_pair_release(verification_key); |
| 2717 |
} |
| 2718 |
aws_signing_state_destroy(signing_state); |
| 2719 |
|
| 2720 |
return result; |
| 2721 |
} |
| 2722 |
|
| 2723 |
static bool s_is_padding_byte(uint8_t byte) { |
| 2724 |
return byte == AWS_SIGV4A_SIGNATURE_PADDING_BYTE; |
| 2725 |
} |
| 2726 |
|
| 2727 |
struct aws_byte_cursor aws_trim_padded_sigv4a_signature(struct aws_byte_cursor signature) { |
| 2728 |
return aws_byte_cursor_trim_pred(&signature, s_is_padding_byte); |
| 2729 |
} |