| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/testing/aws_test_harness.h> |
| 7 |
|
| 8 |
#include <aws/auth/credentials.h> |
| 9 |
#include <aws/auth/private/aws_signing.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/common/condition_variable.h> |
| 15 |
#include <aws/common/encoding.h> |
| 16 |
#include <aws/common/environment.h> |
| 17 |
#include <aws/common/json.h> |
| 18 |
#include <aws/common/string.h> |
| 19 |
#include <aws/http/request_response.h> |
| 20 |
#include <aws/io/file_utils.h> |
| 21 |
#include <aws/io/stream.h> |
| 22 |
#include <aws/io/uri.h> |
| 23 |
|
| 24 |
#include <aws/auth/signing_config.h> |
| 25 |
#include <ctype.h> |
| 26 |
|
| 27 |
#include "credentials_provider_utils.h" |
| 28 |
#include "test_signable.h" |
| 29 |
|
| 30 |
#if defined(_MSC_VER) |
| 31 |
# pragma warning(disable : 4996) |
| 32 |
#endif |
| 33 |
|
| 34 |
AWS_STATIC_STRING_FROM_LITERAL(s_header_canonical_request_filename, "header-canonical-request.txt"); |
| 35 |
AWS_STATIC_STRING_FROM_LITERAL(s_header_string_to_sign_filename, "header-string-to-sign.txt"); |
| 36 |
AWS_STATIC_STRING_FROM_LITERAL(s_header_signed_request_filename, "header-signed-request.txt"); |
| 37 |
AWS_STATIC_STRING_FROM_LITERAL(s_header_signature_filename, "header-signature.txt"); |
| 38 |
AWS_STATIC_STRING_FROM_LITERAL(s_query_canonical_request_filename, "query-canonical-request.txt"); |
| 39 |
AWS_STATIC_STRING_FROM_LITERAL(s_query_string_to_sign_filename, "query-string-to-sign.txt"); |
| 40 |
AWS_STATIC_STRING_FROM_LITERAL(s_query_signed_request_filename, "query-signed-request.txt"); |
| 41 |
AWS_STATIC_STRING_FROM_LITERAL(s_query_signature_filename, "query-signature.txt"); |
| 42 |
|
| 43 |
AWS_STATIC_STRING_FROM_LITERAL(s_public_key_filename, "public-key.json"); |
| 44 |
|
| 45 |
static const struct aws_string *s_get_canonical_request_filename(enum aws_signature_type signature_type) { |
| 46 |
switch (signature_type) { |
| 47 |
case AWS_ST_HTTP_REQUEST_HEADERS: |
| 48 |
return s_header_canonical_request_filename; |
| 49 |
case AWS_ST_HTTP_REQUEST_QUERY_PARAMS: |
| 50 |
return s_query_canonical_request_filename; |
| 51 |
|
| 52 |
default: |
| 53 |
return NULL; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
static const struct aws_string *s_get_string_to_sign_filename(enum aws_signature_type signature_type) { |
| 58 |
switch (signature_type) { |
| 59 |
case AWS_ST_HTTP_REQUEST_HEADERS: |
| 60 |
return s_header_string_to_sign_filename; |
| 61 |
case AWS_ST_HTTP_REQUEST_QUERY_PARAMS: |
| 62 |
return s_query_string_to_sign_filename; |
| 63 |
|
| 64 |
default: |
| 65 |
return NULL; |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
static const struct aws_string *s_get_signed_request_filename(enum aws_signature_type signature_type) { |
| 70 |
switch (signature_type) { |
| 71 |
case AWS_ST_HTTP_REQUEST_HEADERS: |
| 72 |
return s_header_signed_request_filename; |
| 73 |
case AWS_ST_HTTP_REQUEST_QUERY_PARAMS: |
| 74 |
return s_query_signed_request_filename; |
| 75 |
|
| 76 |
default: |
| 77 |
return NULL; |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
static const struct aws_string *s_get_signature_filename(enum aws_signature_type signature_type) { |
| 82 |
switch (signature_type) { |
| 83 |
case AWS_ST_HTTP_REQUEST_HEADERS: |
| 84 |
return s_header_signature_filename; |
| 85 |
case AWS_ST_HTTP_REQUEST_QUERY_PARAMS: |
| 86 |
return s_query_signature_filename; |
| 87 |
|
| 88 |
default: |
| 89 |
return NULL; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
struct v4_test_case_contents { |
| 94 |
struct aws_allocator *allocator; |
| 95 |
struct aws_byte_buf context; |
| 96 |
struct aws_byte_buf request; |
| 97 |
struct aws_byte_buf public_key; |
| 98 |
struct aws_byte_buf expected_canonical_request; |
| 99 |
struct aws_byte_buf expected_string_to_sign; |
| 100 |
struct aws_byte_buf sample_signed_request; |
| 101 |
struct aws_byte_buf sample_signature; |
| 102 |
}; |
| 103 |
|
| 104 |
static void s_strip_windows_line_endings(struct aws_byte_buf *buffer) { |
| 105 |
size_t write_index = 0; |
| 106 |
for (size_t read_index = 0; read_index < buffer->len; ++read_index) { |
| 107 |
char current = buffer->buffer[read_index]; |
| 108 |
if (current != '\r') { |
| 109 |
buffer->buffer[write_index++] = current; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
buffer->len = write_index; |
| 114 |
} |
| 115 |
|
| 116 |
static int s_load_test_case_file( |
| 117 |
struct aws_allocator *allocator, |
| 118 |
const char *parent_folder, |
| 119 |
const char *test_name, |
| 120 |
const char *filename, |
| 121 |
struct aws_byte_buf *buffer) { |
| 122 |
char path[1024]; |
| 123 |
snprintf(path, AWS_ARRAY_SIZE(path), "./%s/%s/%s", parent_folder, test_name, filename); |
| 124 |
return aws_byte_buf_init_from_file(buffer, allocator, path); |
| 125 |
} |
| 126 |
|
| 127 |
static int s_v4_test_case_context_init_from_file_set( |
| 128 |
struct v4_test_case_contents *contents, |
| 129 |
struct aws_allocator *allocator, |
| 130 |
const char *parent_folder, |
| 131 |
const char *test_name, |
| 132 |
enum aws_signature_type signature_type) { |
| 133 |
|
| 134 |
AWS_ZERO_STRUCT(*contents); |
| 135 |
contents->allocator = allocator; |
| 136 |
|
| 137 |
|
| 138 |
if (s_load_test_case_file(allocator, parent_folder, test_name, "request.txt", &contents->request) || |
| 139 |
s_load_test_case_file(allocator, parent_folder, test_name, "context.json", &contents->context)) { |
| 140 |
return AWS_OP_ERR; |
| 141 |
} |
| 142 |
|
| 143 |
s_strip_windows_line_endings(&contents->request); |
| 144 |
|
| 145 |
s_load_test_case_file( |
| 146 |
allocator, parent_folder, test_name, (const char *)s_public_key_filename->bytes, &contents->public_key); |
| 147 |
|
| 148 |
s_load_test_case_file( |
| 149 |
allocator, |
| 150 |
parent_folder, |
| 151 |
test_name, |
| 152 |
aws_string_c_str(s_get_canonical_request_filename(signature_type)), |
| 153 |
&contents->expected_canonical_request); |
| 154 |
|
| 155 |
s_strip_windows_line_endings(&contents->expected_canonical_request); |
| 156 |
|
| 157 |
s_load_test_case_file( |
| 158 |
allocator, |
| 159 |
parent_folder, |
| 160 |
test_name, |
| 161 |
aws_string_c_str(s_get_string_to_sign_filename(signature_type)), |
| 162 |
&contents->expected_string_to_sign); |
| 163 |
|
| 164 |
s_strip_windows_line_endings(&contents->expected_string_to_sign); |
| 165 |
|
| 166 |
s_load_test_case_file( |
| 167 |
allocator, |
| 168 |
parent_folder, |
| 169 |
test_name, |
| 170 |
aws_string_c_str(s_get_signed_request_filename(signature_type)), |
| 171 |
&contents->sample_signed_request); |
| 172 |
|
| 173 |
s_strip_windows_line_endings(&contents->sample_signed_request); |
| 174 |
|
| 175 |
s_load_test_case_file( |
| 176 |
allocator, |
| 177 |
parent_folder, |
| 178 |
test_name, |
| 179 |
aws_string_c_str(s_get_signature_filename(signature_type)), |
| 180 |
&contents->sample_signature); |
| 181 |
|
| 182 |
s_strip_windows_line_endings(&contents->sample_signature); |
| 183 |
|
| 184 |
return AWS_OP_SUCCESS; |
| 185 |
} |
| 186 |
|
| 187 |
static void s_v4_test_case_contents_clean_up(struct v4_test_case_contents *contents) { |
| 188 |
if (contents->allocator) { |
| 189 |
aws_byte_buf_clean_up(&contents->request); |
| 190 |
aws_byte_buf_clean_up(&contents->context); |
| 191 |
aws_byte_buf_clean_up(&contents->public_key); |
| 192 |
aws_byte_buf_clean_up(&contents->expected_canonical_request); |
| 193 |
aws_byte_buf_clean_up(&contents->expected_string_to_sign); |
| 194 |
aws_byte_buf_clean_up(&contents->sample_signed_request); |
| 195 |
aws_byte_buf_clean_up(&contents->sample_signature); |
| 196 |
|
| 197 |
contents->allocator = NULL; |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
struct v4_test_context { |
| 202 |
struct aws_allocator *allocator; |
| 203 |
enum aws_signing_algorithm algorithm; |
| 204 |
struct v4_test_case_contents test_case_data; |
| 205 |
|
| 206 |
struct aws_string *region_config; |
| 207 |
struct aws_string *service; |
| 208 |
struct aws_string *timestamp; |
| 209 |
struct aws_credentials *credentials; |
| 210 |
bool should_normalize; |
| 211 |
bool should_sign_body; |
| 212 |
uint64_t expiration_in_seconds; |
| 213 |
struct aws_input_stream *payload_stream; |
| 214 |
struct aws_ecc_key_pair *signing_key; |
| 215 |
struct aws_ecc_key_pair *verification_key; |
| 216 |
|
| 217 |
struct aws_signable *signable; |
| 218 |
struct aws_signing_config_aws *config; |
| 219 |
|
| 220 |
struct aws_mutex lock; |
| 221 |
struct aws_condition_variable signal; |
| 222 |
|
| 223 |
bool done; |
| 224 |
|
| 225 |
struct aws_signing_state_aws *signing_state; |
| 226 |
struct aws_http_message *request; |
| 227 |
|
| 228 |
bool should_generate_test_case; |
| 229 |
|
| 230 |
struct aws_string *canonical_signing_auth_value; |
| 231 |
bool omit_session_token; |
| 232 |
}; |
| 233 |
|
| 234 |
static void s_v4_test_context_clean_up(struct v4_test_context *context) { |
| 235 |
s_v4_test_case_contents_clean_up(&context->test_case_data); |
| 236 |
|
| 237 |
aws_http_message_release(context->request); |
| 238 |
aws_input_stream_destroy(context->payload_stream); |
| 239 |
aws_ecc_key_pair_release(context->signing_key); |
| 240 |
aws_ecc_key_pair_release(context->verification_key); |
| 241 |
|
| 242 |
aws_string_destroy(context->region_config); |
| 243 |
aws_string_destroy(context->service); |
| 244 |
aws_string_destroy(context->timestamp); |
| 245 |
aws_credentials_release(context->credentials); |
| 246 |
|
| 247 |
aws_mutex_clean_up(&context->lock); |
| 248 |
aws_condition_variable_clean_up(&context->signal); |
| 249 |
|
| 250 |
aws_signing_state_destroy(context->signing_state); |
| 251 |
|
| 252 |
aws_mem_release(context->allocator, context->config); |
| 253 |
aws_signable_destroy(context->signable); |
| 254 |
|
| 255 |
aws_string_destroy(context->canonical_signing_auth_value); |
| 256 |
} |
| 257 |
|
| 258 |
AWS_STATIC_STRING_FROM_LITERAL(s_empty_empty_string, "\0"); |
| 259 |
AWS_STATIC_STRING_FROM_LITERAL(s_credentials_name, "credentials"); |
| 260 |
AWS_STATIC_STRING_FROM_LITERAL(s_access_key_id_name, "access_key_id"); |
| 261 |
AWS_STATIC_STRING_FROM_LITERAL(s_secret_access_key_name, "secret_access_key"); |
| 262 |
AWS_STATIC_STRING_FROM_LITERAL(s_session_token_name, "token"); |
| 263 |
AWS_STATIC_STRING_FROM_LITERAL(s_region_name, "region"); |
| 264 |
AWS_STATIC_STRING_FROM_LITERAL(s_service_name, "service"); |
| 265 |
AWS_STATIC_STRING_FROM_LITERAL(s_timestamp_name, "timestamp"); |
| 266 |
AWS_STATIC_STRING_FROM_LITERAL(s_normalize_name, "normalize"); |
| 267 |
AWS_STATIC_STRING_FROM_LITERAL(s_body_name, "sign_body"); |
| 268 |
AWS_STATIC_STRING_FROM_LITERAL(s_expiration_name, "expiration_in_seconds"); |
| 269 |
AWS_STATIC_STRING_FROM_LITERAL(s_omit_token_name, "omit_session_token"); |
| 270 |
|
| 271 |
static int s_v4_test_context_parse_context_file(struct v4_test_context *context) { |
| 272 |
struct aws_byte_buf *document = &context->test_case_data.context; |
| 273 |
struct aws_json_value *document_root = NULL; |
| 274 |
int result = AWS_OP_ERR; |
| 275 |
|
| 276 |
struct aws_byte_cursor null_terminator_cursor = aws_byte_cursor_from_string(s_empty_empty_string); |
| 277 |
if (aws_byte_buf_append_dynamic(document, &null_terminator_cursor)) { |
| 278 |
goto done; |
| 279 |
} |
| 280 |
|
| 281 |
struct aws_byte_cursor document_buffer_cursor = aws_byte_cursor_from_buf(document); |
| 282 |
document_root = aws_json_value_new_from_string(aws_default_allocator(), document_buffer_cursor); |
| 283 |
if (document_root == NULL) { |
| 284 |
goto done; |
| 285 |
} |
| 286 |
|
| 287 |
struct aws_json_value *credentials_node = |
| 288 |
aws_json_value_get_from_object(document_root, aws_byte_cursor_from_string(s_credentials_name)); |
| 289 |
AWS_FATAL_ASSERT(credentials_node != NULL); |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
struct aws_json_value *access_key_id = |
| 295 |
aws_json_value_get_from_object(credentials_node, aws_byte_cursor_from_string(s_access_key_id_name)); |
| 296 |
struct aws_json_value *secret_access_key = |
| 297 |
aws_json_value_get_from_object(credentials_node, aws_byte_cursor_from_string(s_secret_access_key_name)); |
| 298 |
struct aws_json_value *session_token = |
| 299 |
aws_json_value_get_from_object(credentials_node, aws_byte_cursor_from_string(s_session_token_name)); |
| 300 |
|
| 301 |
struct aws_byte_cursor access_key_id_cursor; |
| 302 |
if (!aws_json_value_is_string(access_key_id) || |
| 303 |
aws_json_value_get_string(access_key_id, &access_key_id_cursor) == AWS_OP_ERR) { |
| 304 |
goto done; |
| 305 |
} |
| 306 |
|
| 307 |
struct aws_byte_cursor secret_access_key_cursor; |
| 308 |
AWS_ZERO_STRUCT(secret_access_key_cursor); |
| 309 |
struct aws_byte_cursor session_token_cursor; |
| 310 |
AWS_ZERO_STRUCT(session_token_cursor); |
| 311 |
|
| 312 |
if (aws_json_value_is_string(session_token)) { |
| 313 |
aws_json_value_get_string(session_token, &session_token_cursor); |
| 314 |
} |
| 315 |
if (aws_json_value_is_string(secret_access_key)) { |
| 316 |
aws_json_value_get_string(secret_access_key, &secret_access_key_cursor); |
| 317 |
} |
| 318 |
|
| 319 |
if (context->signing_key == NULL) { |
| 320 |
context->credentials = aws_credentials_new( |
| 321 |
context->allocator, access_key_id_cursor, secret_access_key_cursor, session_token_cursor, UINT64_MAX); |
| 322 |
context->signing_key = |
| 323 |
aws_ecc_key_pair_new_ecdsa_p256_key_from_aws_credentials(context->allocator, context->credentials); |
| 324 |
} else { |
| 325 |
context->credentials = aws_credentials_new_ecc( |
| 326 |
context->allocator, access_key_id_cursor, context->signing_key, session_token_cursor, UINT64_MAX); |
| 327 |
context->signing_key = aws_credentials_get_ecc_key_pair(context->credentials); |
| 328 |
aws_ecc_key_pair_acquire(context->signing_key); |
| 329 |
} |
| 330 |
|
| 331 |
AWS_FATAL_ASSERT(context->credentials != NULL); |
| 332 |
|
| 333 |
struct aws_json_value *region_node = |
| 334 |
aws_json_value_get_from_object(document_root, aws_byte_cursor_from_string(s_region_name)); |
| 335 |
struct aws_byte_cursor region_node_cursor; |
| 336 |
if (region_node == NULL || !aws_json_value_is_string(region_node) || |
| 337 |
aws_json_value_get_string(region_node, ®ion_node_cursor) == AWS_OP_ERR) { |
| 338 |
goto done; |
| 339 |
} |
| 340 |
|
| 341 |
context->region_config = aws_string_new_from_cursor(context->allocator, ®ion_node_cursor); |
| 342 |
if (context->region_config == NULL) { |
| 343 |
goto done; |
| 344 |
} |
| 345 |
|
| 346 |
struct aws_json_value *service_node = |
| 347 |
aws_json_value_get_from_object(document_root, aws_byte_cursor_from_string(s_service_name)); |
| 348 |
struct aws_byte_cursor service_node_cursor; |
| 349 |
if (service_node == NULL || !aws_json_value_is_string(service_node) || |
| 350 |
aws_json_value_get_string(service_node, &service_node_cursor) == AWS_OP_ERR) { |
| 351 |
goto done; |
| 352 |
} |
| 353 |
|
| 354 |
context->service = aws_string_new_from_cursor(context->allocator, &service_node_cursor); |
| 355 |
if (context->service == NULL) { |
| 356 |
goto done; |
| 357 |
} |
| 358 |
|
| 359 |
struct aws_json_value *timestamp_node = |
| 360 |
aws_json_value_get_from_object(document_root, aws_byte_cursor_from_string(s_timestamp_name)); |
| 361 |
struct aws_byte_cursor timestamp_node_cursor; |
| 362 |
if (timestamp_node == NULL || !aws_json_value_is_string(timestamp_node) || |
| 363 |
aws_json_value_get_string(timestamp_node, ×tamp_node_cursor) == AWS_OP_ERR) { |
| 364 |
goto done; |
| 365 |
} |
| 366 |
|
| 367 |
context->timestamp = aws_string_new_from_cursor(context->allocator, ×tamp_node_cursor); |
| 368 |
if (context->timestamp == NULL) { |
| 369 |
goto done; |
| 370 |
} |
| 371 |
|
| 372 |
struct aws_json_value *normalize_node = |
| 373 |
aws_json_value_get_from_object(document_root, aws_byte_cursor_from_string(s_normalize_name)); |
| 374 |
if (normalize_node == NULL || !aws_json_value_is_boolean(normalize_node)) { |
| 375 |
goto done; |
| 376 |
} |
| 377 |
|
| 378 |
aws_json_value_get_boolean(normalize_node, &context->should_normalize); |
| 379 |
|
| 380 |
struct aws_json_value *body_node = |
| 381 |
aws_json_value_get_from_object(document_root, aws_byte_cursor_from_string(s_body_name)); |
| 382 |
if (body_node == NULL || !aws_json_value_is_boolean(body_node)) { |
| 383 |
goto done; |
| 384 |
} |
| 385 |
|
| 386 |
aws_json_value_get_boolean(body_node, &context->should_sign_body); |
| 387 |
|
| 388 |
struct aws_json_value *expiration_node = |
| 389 |
aws_json_value_get_from_object(document_root, aws_byte_cursor_from_string(s_expiration_name)); |
| 390 |
if (expiration_node == NULL || !aws_json_value_is_number(expiration_node)) { |
| 391 |
goto done; |
| 392 |
} |
| 393 |
|
| 394 |
double expiration_in_seconds_double = 0; |
| 395 |
aws_json_value_get_number(expiration_node, &expiration_in_seconds_double); |
| 396 |
context->expiration_in_seconds = (uint64_t)expiration_in_seconds_double; |
| 397 |
|
| 398 |
struct aws_json_value *omit_token_node = |
| 399 |
aws_json_value_get_from_object(document_root, aws_byte_cursor_from_string(s_omit_token_name)); |
| 400 |
if (omit_token_node != NULL && aws_json_value_is_boolean(omit_token_node)) { |
| 401 |
aws_json_value_get_boolean(omit_token_node, &context->omit_session_token); |
| 402 |
} |
| 403 |
|
| 404 |
result = AWS_OP_SUCCESS; |
| 405 |
|
| 406 |
done: |
| 407 |
|
| 408 |
if (document_root != NULL) { |
| 409 |
aws_json_value_destroy(document_root); |
| 410 |
} |
| 411 |
|
| 412 |
return result; |
| 413 |
} |
| 414 |
|
| 415 |
static int s_parse_request( |
| 416 |
struct aws_allocator *allocator, |
| 417 |
struct aws_byte_cursor request_cursor, |
| 418 |
struct aws_http_message **out_request, |
| 419 |
struct aws_input_stream **out_body_stream) { |
| 420 |
int result = AWS_OP_ERR; |
| 421 |
|
| 422 |
*out_request = NULL; |
| 423 |
*out_body_stream = NULL; |
| 424 |
|
| 425 |
struct aws_array_list request_lines; |
| 426 |
AWS_ZERO_STRUCT(request_lines); |
| 427 |
if (aws_array_list_init_dynamic(&request_lines, allocator, 10, sizeof(struct aws_byte_cursor))) { |
| 428 |
return AWS_OP_ERR; |
| 429 |
} |
| 430 |
|
| 431 |
struct aws_input_stream *body_stream = NULL; |
| 432 |
struct aws_http_message *request = aws_http_message_new_request(allocator); |
| 433 |
if (request == NULL) { |
| 434 |
goto done; |
| 435 |
} |
| 436 |
|
| 437 |
struct aws_array_list header_set; |
| 438 |
AWS_ZERO_STRUCT(header_set); |
| 439 |
if (aws_array_list_init_dynamic(&header_set, allocator, 10, sizeof(struct aws_signable_property_list_pair))) { |
| 440 |
goto done; |
| 441 |
} |
| 442 |
|
| 443 |
if (aws_byte_cursor_split_on_char(&request_cursor, '\n', &request_lines)) { |
| 444 |
goto done; |
| 445 |
} |
| 446 |
|
| 447 |
size_t line_count = aws_array_list_length(&request_lines); |
| 448 |
if (line_count == 0) { |
| 449 |
goto done; |
| 450 |
} |
| 451 |
|
| 452 |
struct aws_byte_cursor first_line; |
| 453 |
AWS_ZERO_STRUCT(first_line); |
| 454 |
if (aws_array_list_get_at(&request_lines, &first_line, 0)) { |
| 455 |
goto done; |
| 456 |
} |
| 457 |
|
| 458 |
struct aws_byte_cursor method_cursor; |
| 459 |
AWS_ZERO_STRUCT(method_cursor); |
| 460 |
if (!aws_byte_cursor_next_split(&first_line, ' ', &method_cursor)) { |
| 461 |
goto done; |
| 462 |
} |
| 463 |
|
| 464 |
aws_http_message_set_request_method(request, method_cursor); |
| 465 |
|
| 466 |
aws_byte_cursor_advance(&first_line, method_cursor.len + 1); |
| 467 |
|
| 468 |
|
| 469 |
struct aws_byte_cursor uri_cursor = first_line; |
| 470 |
uri_cursor.len -= 9; |
| 471 |
|
| 472 |
aws_http_message_set_request_path(request, uri_cursor); |
| 473 |
|
| 474 |
|
| 475 |
size_t line_index = 1; |
| 476 |
for (; line_index < line_count; ++line_index) { |
| 477 |
struct aws_byte_cursor current_line; |
| 478 |
AWS_ZERO_STRUCT(current_line); |
| 479 |
if (aws_array_list_get_at(&request_lines, ¤t_line, line_index)) { |
| 480 |
goto done; |
| 481 |
} |
| 482 |
|
| 483 |
if (current_line.len == 0) { |
| 484 |
|
| 485 |
break; |
| 486 |
} |
| 487 |
|
| 488 |
if (isspace(*current_line.ptr)) { |
| 489 |
|
| 490 |
size_t current_header_count = aws_array_list_length(&header_set); |
| 491 |
AWS_FATAL_ASSERT(current_header_count > 0); |
| 492 |
|
| 493 |
struct aws_signable_property_list_pair *current_header; |
| 494 |
if (aws_array_list_get_at_ptr(&header_set, (void **)¤t_header, current_header_count - 1)) { |
| 495 |
goto done; |
| 496 |
} |
| 497 |
|
| 498 |
current_header->value.len = (current_line.ptr + current_line.len) - current_header->value.ptr; |
| 499 |
} else { |
| 500 |
|
| 501 |
struct aws_signable_property_list_pair current_header; |
| 502 |
AWS_ZERO_STRUCT(current_header); |
| 503 |
if (!aws_byte_cursor_next_split(¤t_line, ':', ¤t_header.name)) { |
| 504 |
goto done; |
| 505 |
} |
| 506 |
|
| 507 |
aws_byte_cursor_advance(¤t_line, current_header.name.len + 1); |
| 508 |
current_header.value = current_line; |
| 509 |
|
| 510 |
aws_array_list_push_back(&header_set, ¤t_header); |
| 511 |
} |
| 512 |
} |
| 513 |
|
| 514 |
size_t header_count = aws_array_list_length(&header_set); |
| 515 |
for (size_t i = 0; i < header_count; ++i) { |
| 516 |
struct aws_signable_property_list_pair property_header; |
| 517 |
aws_array_list_get_at(&header_set, &property_header, i); |
| 518 |
|
| 519 |
struct aws_http_header header = { |
| 520 |
.name = property_header.name, |
| 521 |
.value = property_header.value, |
| 522 |
}; |
| 523 |
|
| 524 |
aws_http_message_add_header(request, header); |
| 525 |
} |
| 526 |
|
| 527 |
|
| 528 |
struct aws_byte_cursor body_cursor; |
| 529 |
AWS_ZERO_STRUCT(body_cursor); |
| 530 |
if (line_index + 1 < line_count) { |
| 531 |
if (aws_array_list_get_at(&request_lines, &body_cursor, line_index + 1)) { |
| 532 |
goto done; |
| 533 |
} |
| 534 |
|
| 535 |
|
| 536 |
body_cursor.len = (request_cursor.ptr + request_cursor.len - body_cursor.ptr); |
| 537 |
|
| 538 |
body_stream = aws_input_stream_new_from_cursor(allocator, &body_cursor); |
| 539 |
if (body_stream == NULL) { |
| 540 |
goto done; |
| 541 |
} |
| 542 |
|
| 543 |
aws_http_message_set_body_stream(request, body_stream); |
| 544 |
} |
| 545 |
|
| 546 |
result = AWS_OP_SUCCESS; |
| 547 |
|
| 548 |
done: |
| 549 |
|
| 550 |
aws_array_list_clean_up(&request_lines); |
| 551 |
aws_array_list_clean_up(&header_set); |
| 552 |
|
| 553 |
if (result == AWS_OP_ERR) { |
| 554 |
aws_http_message_release(request); |
| 555 |
aws_input_stream_destroy(body_stream); |
| 556 |
} else { |
| 557 |
*out_request = request; |
| 558 |
*out_body_stream = body_stream; |
| 559 |
} |
| 560 |
|
| 561 |
return result; |
| 562 |
} |
| 563 |
|
| 564 |
static int s_v4_test_context_init_signing_config( |
| 565 |
struct v4_test_context *context, |
| 566 |
enum aws_signature_type signature_type) { |
| 567 |
|
| 568 |
context->signable = aws_signable_new_http_request(context->allocator, context->request); |
| 569 |
|
| 570 |
context->config = aws_mem_calloc(context->allocator, 1, sizeof(struct aws_signing_config_aws)); |
| 571 |
if (context->config == NULL) { |
| 572 |
return AWS_OP_ERR; |
| 573 |
} |
| 574 |
|
| 575 |
context->config->config_type = AWS_SIGNING_CONFIG_AWS; |
| 576 |
context->config->algorithm = context->algorithm; |
| 577 |
context->config->signature_type = signature_type; |
| 578 |
context->config->region = aws_byte_cursor_from_string(context->region_config); |
| 579 |
context->config->service = aws_byte_cursor_from_string(context->service); |
| 580 |
context->config->flags.use_double_uri_encode = true; |
| 581 |
context->config->flags.should_normalize_uri_path = context->should_normalize; |
| 582 |
context->config->flags.omit_session_token = context->omit_session_token; |
| 583 |
|
| 584 |
|
| 585 |
if (context->should_sign_body) { |
| 586 |
context->config->signed_body_header = AWS_SBHT_X_AMZ_CONTENT_SHA256; |
| 587 |
} else { |
| 588 |
context->config->signed_body_value = g_aws_signed_body_value_empty_sha256; |
| 589 |
} |
| 590 |
|
| 591 |
context->config->credentials = context->credentials; |
| 592 |
context->config->expiration_in_seconds = context->expiration_in_seconds; |
| 593 |
|
| 594 |
struct aws_byte_cursor date_cursor = aws_byte_cursor_from_string(context->timestamp); |
| 595 |
if (aws_date_time_init_from_str_cursor(&context->config->date, &date_cursor, AWS_DATE_FORMAT_ISO_8601)) { |
| 596 |
return AWS_OP_ERR; |
| 597 |
} |
| 598 |
|
| 599 |
context->signing_state = aws_signing_state_new(context->allocator, context->config, context->signable, NULL, NULL); |
| 600 |
if (context->signing_state == NULL) { |
| 601 |
return AWS_OP_ERR; |
| 602 |
} |
| 603 |
|
| 604 |
return AWS_OP_SUCCESS; |
| 605 |
} |
| 606 |
|
| 607 |
static int s_v4_test_context_parse_verification_key(struct v4_test_context *context) { |
| 608 |
AWS_FATAL_ASSERT(context->signing_key != NULL); |
| 609 |
|
| 610 |
if (context->test_case_data.public_key.len == 0) { |
| 611 |
context->verification_key = context->signing_key; |
| 612 |
aws_ecc_key_pair_acquire(context->signing_key); |
| 613 |
aws_ecc_key_pair_derive_public_key(context->verification_key); |
| 614 |
return AWS_OP_SUCCESS; |
| 615 |
} |
| 616 |
|
| 617 |
struct aws_byte_buf pub_x_buffer; |
| 618 |
AWS_ZERO_STRUCT(pub_x_buffer); |
| 619 |
struct aws_byte_buf pub_y_buffer; |
| 620 |
AWS_ZERO_STRUCT(pub_y_buffer); |
| 621 |
|
| 622 |
struct aws_byte_buf *document = &context->test_case_data.public_key; |
| 623 |
struct aws_json_value *document_root = NULL; |
| 624 |
int result = AWS_OP_ERR; |
| 625 |
|
| 626 |
struct aws_byte_cursor null_terminator_cursor = aws_byte_cursor_from_string(s_empty_empty_string); |
| 627 |
if (aws_byte_buf_append_dynamic(document, &null_terminator_cursor)) { |
| 628 |
goto done; |
| 629 |
} |
| 630 |
|
| 631 |
struct aws_byte_cursor document_cursor = aws_byte_cursor_from_buf(document); |
| 632 |
document_root = aws_json_value_new_from_string(aws_default_allocator(), document_cursor); |
| 633 |
if (document_root == NULL) { |
| 634 |
goto done; |
| 635 |
} |
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
struct aws_json_value *pub_x = aws_json_value_get_from_object(document_root, aws_byte_cursor_from_c_str("X")); |
| 641 |
struct aws_json_value *pub_y = aws_json_value_get_from_object(document_root, aws_byte_cursor_from_c_str("Y")); |
| 642 |
if (!aws_json_value_is_string(pub_x) || !aws_json_value_is_string(pub_y)) { |
| 643 |
goto done; |
| 644 |
} |
| 645 |
|
| 646 |
struct aws_byte_cursor pub_x_hex_cursor; |
| 647 |
struct aws_byte_cursor pub_y_hex_cursor; |
| 648 |
aws_json_value_get_string(pub_x, &pub_x_hex_cursor); |
| 649 |
aws_json_value_get_string(pub_y, &pub_y_hex_cursor); |
| 650 |
|
| 651 |
size_t pub_x_length = 0; |
| 652 |
size_t pub_y_length = 0; |
| 653 |
if (aws_hex_compute_decoded_len(pub_x_hex_cursor.len, &pub_x_length) || |
| 654 |
aws_hex_compute_decoded_len(pub_y_hex_cursor.len, &pub_y_length)) { |
| 655 |
goto done; |
| 656 |
} |
| 657 |
|
| 658 |
if (aws_byte_buf_init(&pub_x_buffer, context->allocator, pub_x_length) || |
| 659 |
aws_byte_buf_init(&pub_y_buffer, context->allocator, pub_y_length)) { |
| 660 |
goto done; |
| 661 |
} |
| 662 |
|
| 663 |
if (aws_hex_decode(&pub_x_hex_cursor, &pub_x_buffer) || aws_hex_decode(&pub_y_hex_cursor, &pub_y_buffer)) { |
| 664 |
goto done; |
| 665 |
} |
| 666 |
|
| 667 |
struct aws_byte_cursor pub_x_cursor = aws_byte_cursor_from_buf(&pub_x_buffer); |
| 668 |
struct aws_byte_cursor pub_y_cursor = aws_byte_cursor_from_buf(&pub_y_buffer); |
| 669 |
|
| 670 |
context->verification_key = |
| 671 |
aws_ecc_key_pair_new_from_public_key(context->allocator, AWS_CAL_ECDSA_P256, &pub_x_cursor, &pub_y_cursor); |
| 672 |
|
| 673 |
AWS_FATAL_ASSERT(context->verification_key != NULL); |
| 674 |
|
| 675 |
result = AWS_OP_SUCCESS; |
| 676 |
|
| 677 |
done: |
| 678 |
|
| 679 |
if (document_root) { |
| 680 |
aws_json_value_destroy(document_root); |
| 681 |
} |
| 682 |
|
| 683 |
aws_byte_buf_clean_up(&pub_x_buffer); |
| 684 |
aws_byte_buf_clean_up(&pub_y_buffer); |
| 685 |
|
| 686 |
return result; |
| 687 |
} |
| 688 |
|
| 689 |
AWS_STATIC_STRING_FROM_LITERAL(s_generate_test_env_var_name, "GENERATE_TEST_CASES"); |
| 690 |
|
| 691 |
static int s_v4_test_context_init( |
| 692 |
struct v4_test_context *context, |
| 693 |
struct aws_allocator *allocator, |
| 694 |
const char *parent_folder, |
| 695 |
const char *test_name, |
| 696 |
enum aws_signing_algorithm algorithm, |
| 697 |
enum aws_signature_type signature_type) { |
| 698 |
|
| 699 |
AWS_ZERO_STRUCT(*context); |
| 700 |
context->allocator = allocator; |
| 701 |
context->algorithm = algorithm; |
| 702 |
|
| 703 |
struct aws_string *should_generate = NULL; |
| 704 |
ASSERT_SUCCESS(aws_get_environment_value(allocator, s_generate_test_env_var_name, &should_generate)); |
| 705 |
|
| 706 |
context->should_generate_test_case = should_generate != NULL; |
| 707 |
aws_string_destroy(should_generate); |
| 708 |
|
| 709 |
if (s_v4_test_case_context_init_from_file_set( |
| 710 |
&context->test_case_data, allocator, parent_folder, test_name, signature_type)) { |
| 711 |
return AWS_OP_ERR; |
| 712 |
} |
| 713 |
|
| 714 |
if (s_v4_test_context_parse_context_file(context)) { |
| 715 |
return AWS_OP_ERR; |
| 716 |
} |
| 717 |
|
| 718 |
if (s_parse_request( |
| 719 |
context->allocator, |
| 720 |
aws_byte_cursor_from_buf(&context->test_case_data.request), |
| 721 |
&context->request, |
| 722 |
&context->payload_stream)) { |
| 723 |
return AWS_OP_ERR; |
| 724 |
} |
| 725 |
|
| 726 |
if (s_v4_test_context_init_signing_config(context, signature_type)) { |
| 727 |
return AWS_OP_ERR; |
| 728 |
} |
| 729 |
|
| 730 |
if (s_v4_test_context_parse_verification_key(context)) { |
| 731 |
return AWS_OP_ERR; |
| 732 |
} |
| 733 |
|
| 734 |
if (aws_mutex_init(&context->lock)) { |
| 735 |
return AWS_OP_ERR; |
| 736 |
} |
| 737 |
|
| 738 |
if (aws_condition_variable_init(&context->signal)) { |
| 739 |
return AWS_OP_ERR; |
| 740 |
} |
| 741 |
|
| 742 |
context->done = false; |
| 743 |
|
| 744 |
return AWS_OP_SUCCESS; |
| 745 |
} |
| 746 |
|
| 747 |
bool s_is_signing_complete_predicate(void *userdata) { |
| 748 |
struct v4_test_context *context = userdata; |
| 749 |
return context->done; |
| 750 |
} |
| 751 |
|
| 752 |
void s_wait_on_signing_complete(struct v4_test_context *context) { |
| 753 |
aws_mutex_lock(&context->lock); |
| 754 |
if (!context->done) { |
| 755 |
aws_condition_variable_wait_pred(&context->signal, &context->lock, s_is_signing_complete_predicate, context); |
| 756 |
} |
| 757 |
aws_mutex_unlock(&context->lock); |
| 758 |
} |
| 759 |
|
| 760 |
static void s_on_signing_complete(struct aws_signing_result *result, int error_code, void *userdata) { |
| 761 |
|
| 762 |
AWS_FATAL_ASSERT(error_code == AWS_ERROR_SUCCESS); |
| 763 |
|
| 764 |
struct v4_test_context *context = userdata; |
| 765 |
|
| 766 |
aws_apply_signing_result_to_http_request(context->request, context->allocator, result); |
| 767 |
|
| 768 |
struct aws_string *auth_value = NULL; |
| 769 |
aws_signing_result_get_property(result, g_aws_signature_property_name, &auth_value); |
| 770 |
struct aws_byte_cursor auth_value_cursor = |
| 771 |
aws_trim_padded_sigv4a_signature(aws_byte_cursor_from_string(auth_value)); |
| 772 |
context->canonical_signing_auth_value = aws_string_new_from_cursor(context->allocator, &auth_value_cursor); |
| 773 |
|
| 774 |
|
| 775 |
aws_mutex_lock(&context->lock); |
| 776 |
context->done = true; |
| 777 |
aws_condition_variable_notify_one(&context->signal); |
| 778 |
aws_mutex_unlock(&context->lock); |
| 779 |
} |
| 780 |
|
| 781 |
static int s_write_test_file( |
| 782 |
const char *parent_folder, |
| 783 |
const char *test_name, |
| 784 |
const struct aws_string *filename, |
| 785 |
const struct aws_byte_buf *contents) { |
| 786 |
char path[1024]; |
| 787 |
snprintf(path, AWS_ARRAY_SIZE(path), "./%s/%s/%s", parent_folder, test_name, aws_string_c_str(filename)); |
| 788 |
|
| 789 |
FILE *fp = fopen(path, "w"); |
| 790 |
if (fp == NULL) { |
| 791 |
return AWS_OP_ERR; |
| 792 |
} |
| 793 |
|
| 794 |
struct aws_byte_cursor cursor = aws_byte_cursor_from_array(contents->buffer, contents->len); |
| 795 |
fprintf(fp, PRInSTR, AWS_BYTE_CURSOR_PRI(cursor)); |
| 796 |
|
| 797 |
fclose(fp); |
| 798 |
|
| 799 |
return AWS_OP_SUCCESS; |
| 800 |
} |
| 801 |
|
| 802 |
AWS_STATIC_STRING_FROM_LITERAL(s_public_key_prefix_json, "{\n"); |
| 803 |
AWS_STATIC_STRING_FROM_LITERAL(s_public_key_x_json, " \"X\":\""); |
| 804 |
AWS_STATIC_STRING_FROM_LITERAL(s_public_key_y_json, "\",\n \"Y\":\""); |
| 805 |
AWS_STATIC_STRING_FROM_LITERAL(s_public_key_suffix_json, "\"\n}\n"); |
| 806 |
|
| 807 |
int s_write_public_key_file(struct v4_test_context *context, const char *parent_folder, const char *test_name) { |
| 808 |
AWS_FATAL_ASSERT(context->verification_key != NULL); |
| 809 |
int result = AWS_OP_ERR; |
| 810 |
struct aws_byte_buf key_buffer; |
| 811 |
AWS_ZERO_STRUCT(key_buffer); |
| 812 |
|
| 813 |
if (aws_byte_buf_init(&key_buffer, context->allocator, 256)) { |
| 814 |
goto done; |
| 815 |
} |
| 816 |
|
| 817 |
struct aws_byte_cursor pub_x_cursor; |
| 818 |
AWS_ZERO_STRUCT(pub_x_cursor); |
| 819 |
struct aws_byte_cursor pub_y_cursor; |
| 820 |
AWS_ZERO_STRUCT(pub_y_cursor); |
| 821 |
|
| 822 |
aws_ecc_key_pair_derive_public_key(context->verification_key); |
| 823 |
aws_ecc_key_pair_get_public_key(context->verification_key, &pub_x_cursor, &pub_y_cursor); |
| 824 |
|
| 825 |
struct aws_byte_cursor prefix_cursor = aws_byte_cursor_from_string(s_public_key_prefix_json); |
| 826 |
aws_byte_buf_append_dynamic(&key_buffer, &prefix_cursor); |
| 827 |
|
| 828 |
struct aws_byte_cursor x_cursor = aws_byte_cursor_from_string(s_public_key_x_json); |
| 829 |
aws_byte_buf_append_dynamic(&key_buffer, &x_cursor); |
| 830 |
|
| 831 |
aws_hex_encode_append_dynamic(&pub_x_cursor, &key_buffer); |
| 832 |
|
| 833 |
struct aws_byte_cursor y_cursor = aws_byte_cursor_from_string(s_public_key_y_json); |
| 834 |
aws_byte_buf_append_dynamic(&key_buffer, &y_cursor); |
| 835 |
|
| 836 |
aws_hex_encode_append_dynamic(&pub_y_cursor, &key_buffer); |
| 837 |
|
| 838 |
struct aws_byte_cursor suffix_cursor = aws_byte_cursor_from_string(s_public_key_suffix_json); |
| 839 |
aws_byte_buf_append_dynamic(&key_buffer, &suffix_cursor); |
| 840 |
|
| 841 |
result = s_write_test_file(parent_folder, test_name, s_public_key_filename, &key_buffer); |
| 842 |
|
| 843 |
done: |
| 844 |
|
| 845 |
aws_byte_buf_clean_up(&key_buffer); |
| 846 |
|
| 847 |
return result; |
| 848 |
} |
| 849 |
|
| 850 |
static int s_generate_test_case( |
| 851 |
struct v4_test_context *test_context, |
| 852 |
const char *parent_folder, |
| 853 |
const char *test_name) { |
| 854 |
{ |
| 855 |
struct aws_signing_state_aws *signing_state = test_context->signing_state; |
| 856 |
|
| 857 |
|
| 858 |
if (test_context->algorithm == AWS_SIGNING_ALGORITHM_V4_ASYMMETRIC && |
| 859 |
test_context->config->signature_type == AWS_ST_HTTP_REQUEST_HEADERS) { |
| 860 |
ASSERT_SUCCESS(s_write_public_key_file(test_context, parent_folder, test_name)); |
| 861 |
} |
| 862 |
|
| 863 |
|
| 864 |
ASSERT_TRUE(aws_signing_build_canonical_request(signing_state) == AWS_OP_SUCCESS); |
| 865 |
ASSERT_SUCCESS(s_write_test_file( |
| 866 |
parent_folder, |
| 867 |
test_name, |
| 868 |
s_get_canonical_request_filename(test_context->config->signature_type), |
| 869 |
&signing_state->canonical_request)); |
| 870 |
|
| 871 |
|
| 872 |
ASSERT_TRUE(aws_signing_build_string_to_sign(signing_state) == AWS_OP_SUCCESS); |
| 873 |
ASSERT_SUCCESS(s_write_test_file( |
| 874 |
parent_folder, |
| 875 |
test_name, |
| 876 |
s_get_string_to_sign_filename(test_context->config->signature_type), |
| 877 |
&signing_state->string_to_sign)); |
| 878 |
} |
| 879 |
|
| 880 |
return AWS_OP_SUCCESS; |
| 881 |
} |
| 882 |
|
| 883 |
static int s_check_piecewise_test_case(struct v4_test_context *test_context) { |
| 884 |
struct aws_signing_state_aws *signing_state = test_context->signing_state; |
| 885 |
|
| 886 |
|
| 887 |
ASSERT_TRUE(aws_signing_build_canonical_request(signing_state) == AWS_OP_SUCCESS); |
| 888 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 889 |
test_context->test_case_data.expected_canonical_request.buffer, |
| 890 |
test_context->test_case_data.expected_canonical_request.len, |
| 891 |
signing_state->canonical_request.buffer, |
| 892 |
signing_state->canonical_request.len); |
| 893 |
|
| 894 |
|
| 895 |
ASSERT_TRUE(aws_signing_build_string_to_sign(signing_state) == AWS_OP_SUCCESS); |
| 896 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 897 |
test_context->test_case_data.expected_string_to_sign.buffer, |
| 898 |
test_context->test_case_data.expected_string_to_sign.len, |
| 899 |
signing_state->string_to_sign.buffer, |
| 900 |
signing_state->string_to_sign.len); |
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
return AWS_OP_SUCCESS; |
| 905 |
} |
| 906 |
|
| 907 |
static int s_do_sigv4_test_piecewise( |
| 908 |
struct aws_allocator *allocator, |
| 909 |
const char *parent_folder, |
| 910 |
const char *test_name, |
| 911 |
enum aws_signing_algorithm algorithm, |
| 912 |
enum aws_signature_type signature_type) { |
| 913 |
|
| 914 |
struct v4_test_context test_context; |
| 915 |
AWS_ZERO_STRUCT(test_context); |
| 916 |
|
| 917 |
ASSERT_SUCCESS( |
| 918 |
s_v4_test_context_init(&test_context, allocator, parent_folder, test_name, algorithm, signature_type)); |
| 919 |
|
| 920 |
if (test_context.should_generate_test_case) { |
| 921 |
ASSERT_SUCCESS(s_generate_test_case(&test_context, parent_folder, test_name)); |
| 922 |
} else { |
| 923 |
ASSERT_SUCCESS(s_check_piecewise_test_case(&test_context)); |
| 924 |
} |
| 925 |
|
| 926 |
s_v4_test_context_clean_up(&test_context); |
| 927 |
|
| 928 |
return AWS_OP_SUCCESS; |
| 929 |
} |
| 930 |
|
| 931 |
static int s_write_signed_request_to_file( |
| 932 |
struct v4_test_context *test_context, |
| 933 |
const char *parent_folder, |
| 934 |
const char *test_name, |
| 935 |
const struct aws_string *filename) { |
| 936 |
char path[1024]; |
| 937 |
snprintf(path, AWS_ARRAY_SIZE(path), "./%s/%s/%s", parent_folder, test_name, aws_string_c_str(filename)); |
| 938 |
|
| 939 |
FILE *fp = fopen(path, "w"); |
| 940 |
if (fp == NULL) { |
| 941 |
return AWS_OP_ERR; |
| 942 |
} |
| 943 |
|
| 944 |
struct aws_byte_cursor method_cursor; |
| 945 |
ASSERT_SUCCESS(aws_http_message_get_request_method(test_context->request, &method_cursor)); |
| 946 |
|
| 947 |
struct aws_byte_cursor path_cursor; |
| 948 |
ASSERT_SUCCESS(aws_http_message_get_request_path(test_context->request, &path_cursor)); |
| 949 |
|
| 950 |
fprintf( |
| 951 |
fp, PRInSTR " " PRInSTR " HTTP/1.1\n", AWS_BYTE_CURSOR_PRI(method_cursor), AWS_BYTE_CURSOR_PRI(path_cursor)); |
| 952 |
size_t header_count = aws_http_message_get_header_count(test_context->request); |
| 953 |
for (size_t i = 0; i < header_count; ++i) { |
| 954 |
struct aws_http_header header; |
| 955 |
AWS_ZERO_STRUCT(header); |
| 956 |
ASSERT_SUCCESS(aws_http_message_get_header(test_context->request, &header, i)); |
| 957 |
fprintf(fp, PRInSTR ":" PRInSTR "\n", AWS_BYTE_CURSOR_PRI(header.name), AWS_BYTE_CURSOR_PRI(header.value)); |
| 958 |
} |
| 959 |
|
| 960 |
fprintf(fp, "\n"); |
| 961 |
|
| 962 |
if (test_context->payload_stream) { |
| 963 |
int64_t stream_length = 0; |
| 964 |
ASSERT_SUCCESS(aws_input_stream_get_length(test_context->payload_stream, &stream_length)); |
| 965 |
|
| 966 |
struct aws_byte_buf stream_buf; |
| 967 |
ASSERT_SUCCESS(aws_byte_buf_init(&stream_buf, test_context->allocator, (size_t)stream_length)); |
| 968 |
|
| 969 |
ASSERT_SUCCESS(aws_input_stream_seek(test_context->payload_stream, 0, AWS_SSB_BEGIN)); |
| 970 |
ASSERT_SUCCESS(aws_input_stream_read(test_context->payload_stream, &stream_buf)); |
| 971 |
ASSERT_TRUE(stream_buf.len == (size_t)stream_length); |
| 972 |
|
| 973 |
fprintf(fp, PRInSTR, AWS_BYTE_BUF_PRI(stream_buf)); |
| 974 |
|
| 975 |
aws_byte_buf_clean_up(&stream_buf); |
| 976 |
} |
| 977 |
|
| 978 |
fclose(fp); |
| 979 |
|
| 980 |
return AWS_OP_SUCCESS; |
| 981 |
} |
| 982 |
|
| 983 |
static int s_check_header_value(struct aws_http_message *request, struct aws_http_header *expected_header) { |
| 984 |
size_t header_count = aws_http_message_get_header_count(request); |
| 985 |
for (size_t i = 0; i < header_count; ++i) { |
| 986 |
struct aws_http_header header; |
| 987 |
AWS_ZERO_STRUCT(header); |
| 988 |
|
| 989 |
ASSERT_SUCCESS(aws_http_message_get_header(request, &header, i)); |
| 990 |
|
| 991 |
if (aws_byte_cursor_eq_ignore_case(&header.name, &expected_header->name)) { |
| 992 |
if (aws_byte_cursor_eq(&header.value, &expected_header->value)) { |
| 993 |
aws_http_message_erase_header(request, i); |
| 994 |
return AWS_OP_SUCCESS; |
| 995 |
} |
| 996 |
} |
| 997 |
} |
| 998 |
|
| 999 |
ASSERT_TRUE(false); |
| 1000 |
} |
| 1001 |
|
| 1002 |
static int s_check_query_authorization( |
| 1003 |
struct v4_test_context *test_context, |
| 1004 |
struct aws_byte_cursor signed_path, |
| 1005 |
struct aws_byte_cursor expected_path) { |
| 1006 |
|
| 1007 |
struct aws_uri signed_uri; |
| 1008 |
ASSERT_SUCCESS(aws_uri_init_parse(&signed_uri, test_context->allocator, &signed_path)); |
| 1009 |
|
| 1010 |
struct aws_uri expected_uri; |
| 1011 |
ASSERT_SUCCESS(aws_uri_init_parse(&expected_uri, test_context->allocator, &expected_path)); |
| 1012 |
|
| 1013 |
ASSERT_BIN_ARRAYS_EQUALS(signed_uri.path.ptr, signed_uri.path.len, expected_uri.path.ptr, expected_uri.path.len); |
| 1014 |
|
| 1015 |
struct aws_array_list signed_params; |
| 1016 |
ASSERT_SUCCESS( |
| 1017 |
aws_array_list_init_dynamic(&signed_params, test_context->allocator, 10, sizeof(struct aws_uri_param))); |
| 1018 |
ASSERT_SUCCESS(aws_uri_query_string_params(&signed_uri, &signed_params)); |
| 1019 |
|
| 1020 |
struct aws_array_list expected_params; |
| 1021 |
ASSERT_SUCCESS( |
| 1022 |
aws_array_list_init_dynamic(&expected_params, test_context->allocator, 10, sizeof(struct aws_uri_param))); |
| 1023 |
ASSERT_SUCCESS(aws_uri_query_string_params(&expected_uri, &expected_params)); |
| 1024 |
|
| 1025 |
ASSERT_TRUE(aws_array_list_length(&signed_params) == aws_array_list_length(&expected_params)); |
| 1026 |
|
| 1027 |
struct aws_byte_cursor signature_cursor = aws_byte_cursor_from_string(g_aws_signing_authorization_query_param_name); |
| 1028 |
|
| 1029 |
size_t signed_param_count = aws_array_list_length(&signed_params); |
| 1030 |
for (size_t i = 0; i < signed_param_count; ++i) { |
| 1031 |
struct aws_uri_param signed_param; |
| 1032 |
aws_array_list_get_at(&signed_params, &signed_param, i); |
| 1033 |
|
| 1034 |
if (aws_byte_cursor_eq_ignore_case(&signed_param.key, &signature_cursor) && |
| 1035 |
test_context->algorithm == AWS_SIGNING_ALGORITHM_V4_ASYMMETRIC) { |
| 1036 |
ASSERT_SUCCESS(aws_validate_v4a_authorization_value( |
| 1037 |
test_context->allocator, |
| 1038 |
test_context->verification_key, |
| 1039 |
aws_byte_cursor_from_buf(&test_context->test_case_data.expected_string_to_sign), |
| 1040 |
signed_param.value)); |
| 1041 |
} else { |
| 1042 |
bool found = false; |
| 1043 |
for (size_t j = 0; j < signed_param_count; ++j) { |
| 1044 |
struct aws_uri_param expected_param; |
| 1045 |
aws_array_list_get_at(&expected_params, &expected_param, j); |
| 1046 |
if (aws_byte_cursor_eq_ignore_case(&signed_param.key, &expected_param.key)) { |
| 1047 |
ASSERT_TRUE(aws_byte_cursor_eq_ignore_case(&signed_param.value, &expected_param.value)); |
| 1048 |
found = true; |
| 1049 |
break; |
| 1050 |
} |
| 1051 |
} |
| 1052 |
|
| 1053 |
ASSERT_TRUE(found); |
| 1054 |
} |
| 1055 |
} |
| 1056 |
|
| 1057 |
aws_uri_clean_up(&signed_uri); |
| 1058 |
aws_uri_clean_up(&expected_uri); |
| 1059 |
aws_array_list_clean_up(&signed_params); |
| 1060 |
aws_array_list_clean_up(&expected_params); |
| 1061 |
|
| 1062 |
return AWS_OP_SUCCESS; |
| 1063 |
} |
| 1064 |
|
| 1065 |
static int s_get_authorization_pair( |
| 1066 |
const struct aws_byte_cursor *authorization_value, |
| 1067 |
const struct aws_byte_cursor name, |
| 1068 |
struct aws_byte_cursor value_end, |
| 1069 |
struct aws_byte_cursor *value_out) { |
| 1070 |
struct aws_byte_cursor value_start_cursor; |
| 1071 |
AWS_ZERO_STRUCT(value_start_cursor); |
| 1072 |
ASSERT_SUCCESS(aws_byte_cursor_find_exact(authorization_value, &name, &value_start_cursor)); |
| 1073 |
aws_byte_cursor_advance(&value_start_cursor, name.len); |
| 1074 |
|
| 1075 |
struct aws_byte_cursor value_end_cursor; |
| 1076 |
AWS_ZERO_STRUCT(value_end_cursor); |
| 1077 |
ASSERT_SUCCESS(aws_byte_cursor_find_exact(&value_start_cursor, &value_end, &value_end_cursor)); |
| 1078 |
|
| 1079 |
*value_out = value_start_cursor; |
| 1080 |
value_out->len = value_end_cursor.ptr - value_start_cursor.ptr; |
| 1081 |
|
| 1082 |
return AWS_OP_SUCCESS; |
| 1083 |
} |
| 1084 |
|
| 1085 |
static int s_compare_authorization_pair( |
| 1086 |
const struct aws_byte_cursor *signed_value, |
| 1087 |
const struct aws_byte_cursor *expected_value, |
| 1088 |
const struct aws_byte_cursor name) { |
| 1089 |
struct aws_byte_cursor signed_pair_value; |
| 1090 |
AWS_ZERO_STRUCT(signed_pair_value); |
| 1091 |
ASSERT_SUCCESS(s_get_authorization_pair(signed_value, name, aws_byte_cursor_from_c_str(", "), &signed_pair_value)); |
| 1092 |
|
| 1093 |
struct aws_byte_cursor expected_pair_value; |
| 1094 |
AWS_ZERO_STRUCT(expected_pair_value); |
| 1095 |
ASSERT_SUCCESS( |
| 1096 |
s_get_authorization_pair(expected_value, name, aws_byte_cursor_from_c_str(", "), &expected_pair_value)); |
| 1097 |
|
| 1098 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 1099 |
signed_pair_value.ptr, signed_pair_value.len, expected_pair_value.ptr, expected_pair_value.len); |
| 1100 |
|
| 1101 |
return AWS_OP_SUCCESS; |
| 1102 |
} |
| 1103 |
|
| 1104 |
static int s_check_header_authorization( |
| 1105 |
struct v4_test_context *test_context, |
| 1106 |
struct aws_http_header *header, |
| 1107 |
struct aws_http_message *expected_request) { |
| 1108 |
struct aws_byte_cursor signed_authorization_value = header->value; |
| 1109 |
|
| 1110 |
struct aws_byte_cursor expected_authorization_value; |
| 1111 |
AWS_ZERO_STRUCT(expected_authorization_value); |
| 1112 |
|
| 1113 |
size_t expected_header_count = aws_http_message_get_header_count(expected_request); |
| 1114 |
for (size_t i = 0; i < expected_header_count; ++i) { |
| 1115 |
struct aws_http_header expected_header; |
| 1116 |
AWS_ZERO_STRUCT(expected_header); |
| 1117 |
|
| 1118 |
if (aws_http_message_get_header(expected_request, &expected_header, i)) { |
| 1119 |
continue; |
| 1120 |
} |
| 1121 |
|
| 1122 |
if (aws_byte_cursor_eq_c_str_ignore_case(&expected_header.name, "Authorization")) { |
| 1123 |
expected_authorization_value = expected_header.value; |
| 1124 |
break; |
| 1125 |
} |
| 1126 |
} |
| 1127 |
|
| 1128 |
ASSERT_TRUE(expected_authorization_value.len > 0); |
| 1129 |
|
| 1130 |
struct aws_byte_cursor space_cursor = aws_byte_cursor_from_c_str(" "); |
| 1131 |
|
| 1132 |
struct aws_byte_cursor signed_space_cursor; |
| 1133 |
AWS_ZERO_STRUCT(signed_space_cursor); |
| 1134 |
ASSERT_SUCCESS(aws_byte_cursor_find_exact(&signed_authorization_value, &space_cursor, &signed_space_cursor)); |
| 1135 |
struct aws_byte_cursor signed_algorithm_cursor = { |
| 1136 |
.ptr = signed_authorization_value.ptr, |
| 1137 |
.len = signed_space_cursor.ptr - signed_authorization_value.ptr, |
| 1138 |
}; |
| 1139 |
|
| 1140 |
struct aws_byte_cursor expected_space_cursor; |
| 1141 |
AWS_ZERO_STRUCT(expected_space_cursor); |
| 1142 |
ASSERT_SUCCESS(aws_byte_cursor_find_exact(&expected_authorization_value, &space_cursor, &expected_space_cursor)); |
| 1143 |
struct aws_byte_cursor expected_algorithm_cursor = { |
| 1144 |
.ptr = expected_authorization_value.ptr, |
| 1145 |
.len = expected_space_cursor.ptr - expected_authorization_value.ptr, |
| 1146 |
}; |
| 1147 |
|
| 1148 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 1149 |
signed_algorithm_cursor.ptr, |
| 1150 |
signed_algorithm_cursor.len, |
| 1151 |
expected_algorithm_cursor.ptr, |
| 1152 |
expected_algorithm_cursor.len); |
| 1153 |
|
| 1154 |
ASSERT_SUCCESS(s_compare_authorization_pair( |
| 1155 |
&signed_authorization_value, &expected_authorization_value, aws_byte_cursor_from_c_str("Credential="))); |
| 1156 |
ASSERT_SUCCESS(s_compare_authorization_pair( |
| 1157 |
&signed_authorization_value, &expected_authorization_value, aws_byte_cursor_from_c_str("SignedHeaders="))); |
| 1158 |
|
| 1159 |
struct aws_byte_buf *string_to_sign = &test_context->test_case_data.expected_string_to_sign; |
| 1160 |
struct aws_byte_cursor signature_key_cursor = aws_byte_cursor_from_c_str("Signature="); |
| 1161 |
|
| 1162 |
struct aws_byte_cursor signed_signature_value; |
| 1163 |
AWS_ZERO_STRUCT(signed_signature_value); |
| 1164 |
ASSERT_SUCCESS( |
| 1165 |
aws_byte_cursor_find_exact(&signed_authorization_value, &signature_key_cursor, &signed_signature_value)); |
| 1166 |
aws_byte_cursor_advance(&signed_signature_value, signature_key_cursor.len); |
| 1167 |
ASSERT_SUCCESS(aws_validate_v4a_authorization_value( |
| 1168 |
test_context->allocator, |
| 1169 |
test_context->verification_key, |
| 1170 |
aws_byte_cursor_from_buf(string_to_sign), |
| 1171 |
signed_signature_value)); |
| 1172 |
|
| 1173 |
struct aws_byte_cursor expected_signature_value; |
| 1174 |
AWS_ZERO_STRUCT(expected_signature_value); |
| 1175 |
ASSERT_SUCCESS( |
| 1176 |
aws_byte_cursor_find_exact(&expected_authorization_value, &signature_key_cursor, &expected_signature_value)); |
| 1177 |
aws_byte_cursor_advance(&expected_signature_value, signature_key_cursor.len); |
| 1178 |
ASSERT_SUCCESS(aws_validate_v4a_authorization_value( |
| 1179 |
test_context->allocator, |
| 1180 |
test_context->verification_key, |
| 1181 |
aws_byte_cursor_from_buf(string_to_sign), |
| 1182 |
expected_signature_value)); |
| 1183 |
|
| 1184 |
return AWS_OP_SUCCESS; |
| 1185 |
} |
| 1186 |
|
| 1187 |
static int s_check_signed_request(struct v4_test_context *test_context, struct aws_byte_buf *expected_request_buffer) { |
| 1188 |
|
| 1189 |
struct aws_http_message *expected_request = NULL; |
| 1190 |
struct aws_input_stream *body_stream = NULL; |
| 1191 |
|
| 1192 |
ASSERT_SUCCESS(s_parse_request( |
| 1193 |
test_context->allocator, aws_byte_cursor_from_buf(expected_request_buffer), &expected_request, &body_stream)); |
| 1194 |
ASSERT_NOT_NULL(expected_request); |
| 1195 |
|
| 1196 |
|
| 1197 |
struct aws_byte_cursor signed_method; |
| 1198 |
AWS_ZERO_STRUCT(signed_method); |
| 1199 |
aws_http_message_get_request_method(test_context->request, &signed_method); |
| 1200 |
|
| 1201 |
struct aws_byte_cursor expected_method; |
| 1202 |
AWS_ZERO_STRUCT(expected_method); |
| 1203 |
aws_http_message_get_request_method(expected_request, &expected_method); |
| 1204 |
|
| 1205 |
ASSERT_BIN_ARRAYS_EQUALS(expected_method.ptr, expected_method.len, signed_method.ptr, signed_method.len); |
| 1206 |
|
| 1207 |
|
| 1208 |
struct aws_byte_cursor signed_path; |
| 1209 |
AWS_ZERO_STRUCT(signed_path); |
| 1210 |
aws_http_message_get_request_path(test_context->request, &signed_path); |
| 1211 |
|
| 1212 |
struct aws_byte_cursor expected_path; |
| 1213 |
AWS_ZERO_STRUCT(expected_path); |
| 1214 |
aws_http_message_get_request_path(expected_request, &expected_path); |
| 1215 |
|
| 1216 |
if (test_context->config->signature_type == AWS_ST_HTTP_REQUEST_QUERY_PARAMS) { |
| 1217 |
ASSERT_SUCCESS(s_check_query_authorization(test_context, signed_path, expected_path)); |
| 1218 |
} else { |
| 1219 |
ASSERT_BIN_ARRAYS_EQUALS(expected_path.ptr, expected_path.len, signed_path.ptr, signed_path.len); |
| 1220 |
} |
| 1221 |
|
| 1222 |
|
| 1223 |
size_t signed_header_count = aws_http_message_get_header_count(test_context->request); |
| 1224 |
size_t expected_header_count = aws_http_message_get_header_count(expected_request); |
| 1225 |
ASSERT_TRUE(signed_header_count == expected_header_count); |
| 1226 |
|
| 1227 |
for (size_t i = 0; i < signed_header_count; ++i) { |
| 1228 |
struct aws_http_header header; |
| 1229 |
AWS_ZERO_STRUCT(header); |
| 1230 |
|
| 1231 |
if (aws_http_message_get_header(test_context->request, &header, i)) { |
| 1232 |
continue; |
| 1233 |
} |
| 1234 |
|
| 1235 |
if (test_context->config->signature_type == AWS_ST_HTTP_REQUEST_HEADERS && |
| 1236 |
aws_byte_cursor_eq_c_str_ignore_case(&header.name, "Authorization") && |
| 1237 |
test_context->algorithm == AWS_SIGNING_ALGORITHM_V4_ASYMMETRIC) { |
| 1238 |
ASSERT_SUCCESS(s_check_header_authorization(test_context, &header, expected_request)); |
| 1239 |
} else { |
| 1240 |
ASSERT_SUCCESS(s_check_header_value(expected_request, &header)); |
| 1241 |
} |
| 1242 |
} |
| 1243 |
|
| 1244 |
aws_http_message_release(expected_request); |
| 1245 |
aws_input_stream_destroy(body_stream); |
| 1246 |
|
| 1247 |
return AWS_OP_SUCCESS; |
| 1248 |
} |
| 1249 |
|
| 1250 |
static int s_do_sigv4_test_end_to_end( |
| 1251 |
struct aws_allocator *allocator, |
| 1252 |
const char *parent_folder, |
| 1253 |
const char *test_name, |
| 1254 |
enum aws_signing_algorithm algorithm, |
| 1255 |
enum aws_signature_type signature_type) { |
| 1256 |
|
| 1257 |
struct v4_test_context test_context; |
| 1258 |
AWS_ZERO_STRUCT(test_context); |
| 1259 |
|
| 1260 |
ASSERT_SUCCESS( |
| 1261 |
s_v4_test_context_init(&test_context, allocator, parent_folder, test_name, algorithm, signature_type)); |
| 1262 |
|
| 1263 |
ASSERT_SUCCESS(aws_sign_request_aws( |
| 1264 |
allocator, test_context.signable, (void *)test_context.config, s_on_signing_complete, &test_context)); |
| 1265 |
|
| 1266 |
s_wait_on_signing_complete(&test_context); |
| 1267 |
|
| 1268 |
if (test_context.should_generate_test_case) { |
| 1269 |
ASSERT_SUCCESS(s_write_signed_request_to_file( |
| 1270 |
&test_context, parent_folder, test_name, s_get_signed_request_filename(signature_type))); |
| 1271 |
} else { |
| 1272 |
ASSERT_SUCCESS(s_check_signed_request(&test_context, &test_context.test_case_data.sample_signed_request)); |
| 1273 |
} |
| 1274 |
|
| 1275 |
s_v4_test_context_clean_up(&test_context); |
| 1276 |
|
| 1277 |
return AWS_OP_SUCCESS; |
| 1278 |
} |
| 1279 |
|
| 1280 |
static int s_write_signature_to_file( |
| 1281 |
struct v4_test_context *test_context, |
| 1282 |
const char *parent_folder, |
| 1283 |
const char *test_name, |
| 1284 |
const struct aws_string *filename) { |
| 1285 |
char path[1024]; |
| 1286 |
snprintf(path, AWS_ARRAY_SIZE(path), "./%s/%s/%s", parent_folder, test_name, aws_string_c_str(filename)); |
| 1287 |
|
| 1288 |
FILE *fp = fopen(path, "w"); |
| 1289 |
if (fp == NULL) { |
| 1290 |
return AWS_OP_ERR; |
| 1291 |
} |
| 1292 |
|
| 1293 |
struct aws_byte_cursor signature_cursor = aws_byte_cursor_from_string(test_context->canonical_signing_auth_value); |
| 1294 |
fprintf(fp, PRInSTR, AWS_BYTE_CURSOR_PRI(signature_cursor)); |
| 1295 |
|
| 1296 |
fclose(fp); |
| 1297 |
|
| 1298 |
return AWS_OP_SUCCESS; |
| 1299 |
} |
| 1300 |
|
| 1301 |
static int s_do_sigv4_test_canonical_only( |
| 1302 |
struct aws_allocator *allocator, |
| 1303 |
const char *parent_folder, |
| 1304 |
const char *test_name, |
| 1305 |
enum aws_signing_algorithm algorithm, |
| 1306 |
enum aws_signature_type signature_type) { |
| 1307 |
|
| 1308 |
struct v4_test_context test_context; |
| 1309 |
AWS_ZERO_STRUCT(test_context); |
| 1310 |
|
| 1311 |
ASSERT_SUCCESS( |
| 1312 |
s_v4_test_context_init(&test_context, allocator, parent_folder, test_name, algorithm, signature_type)); |
| 1313 |
|
| 1314 |
|
| 1315 |
aws_signable_destroy(test_context.signable); |
| 1316 |
test_context.signable = aws_signable_new_canonical_request( |
| 1317 |
allocator, aws_byte_cursor_from_buf(&test_context.test_case_data.expected_canonical_request)); |
| 1318 |
if (signature_type == AWS_ST_HTTP_REQUEST_QUERY_PARAMS) { |
| 1319 |
test_context.config->signature_type = AWS_ST_CANONICAL_REQUEST_QUERY_PARAMS; |
| 1320 |
} else { |
| 1321 |
test_context.config->signature_type = AWS_ST_CANONICAL_REQUEST_HEADERS; |
| 1322 |
} |
| 1323 |
|
| 1324 |
ASSERT_SUCCESS(aws_sign_request_aws( |
| 1325 |
allocator, test_context.signable, (void *)test_context.config, s_on_signing_complete, &test_context)); |
| 1326 |
|
| 1327 |
s_wait_on_signing_complete(&test_context); |
| 1328 |
|
| 1329 |
if (test_context.should_generate_test_case) { |
| 1330 |
ASSERT_SUCCESS(s_write_signature_to_file( |
| 1331 |
&test_context, parent_folder, test_name, s_get_signature_filename(signature_type))); |
| 1332 |
} else { |
| 1333 |
if (algorithm == AWS_SIGNING_ALGORITHM_V4) { |
| 1334 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 1335 |
test_context.test_case_data.sample_signature.buffer, |
| 1336 |
test_context.test_case_data.sample_signature.len, |
| 1337 |
test_context.canonical_signing_auth_value->bytes, |
| 1338 |
test_context.canonical_signing_auth_value->len); |
| 1339 |
} else { |
| 1340 |
ASSERT_SUCCESS(aws_validate_v4a_authorization_value( |
| 1341 |
test_context.allocator, |
| 1342 |
test_context.verification_key, |
| 1343 |
aws_byte_cursor_from_buf(&test_context.test_case_data.expected_string_to_sign), |
| 1344 |
aws_byte_cursor_from_string(test_context.canonical_signing_auth_value))); |
| 1345 |
} |
| 1346 |
} |
| 1347 |
|
| 1348 |
s_v4_test_context_clean_up(&test_context); |
| 1349 |
|
| 1350 |
return AWS_OP_SUCCESS; |
| 1351 |
} |
| 1352 |
|
| 1353 |
static int s_do_sigv4_test_signing( |
| 1354 |
struct aws_allocator *allocator, |
| 1355 |
const char *parent_folder, |
| 1356 |
const char *test_name, |
| 1357 |
enum aws_signing_algorithm algorithm, |
| 1358 |
enum aws_signature_type signature_type) { |
| 1359 |
|
| 1360 |
ASSERT_SUCCESS(s_do_sigv4_test_piecewise(allocator, parent_folder, test_name, algorithm, signature_type)); |
| 1361 |
|
| 1362 |
ASSERT_SUCCESS(s_do_sigv4_test_end_to_end(allocator, parent_folder, test_name, algorithm, signature_type)); |
| 1363 |
|
| 1364 |
ASSERT_SUCCESS(s_do_sigv4_test_canonical_only(allocator, parent_folder, test_name, algorithm, signature_type)); |
| 1365 |
|
| 1366 |
return AWS_OP_SUCCESS; |
| 1367 |
} |
| 1368 |
|
| 1369 |
static int s_do_sigv4a_test_case(struct aws_allocator *allocator, const char *test_name, const char *parent_folder) { |
| 1370 |
|
| 1371 |
|
| 1372 |
aws_auth_library_init(allocator); |
| 1373 |
|
| 1374 |
ASSERT_SUCCESS(s_do_sigv4_test_signing( |
| 1375 |
allocator, parent_folder, test_name, AWS_SIGNING_ALGORITHM_V4_ASYMMETRIC, AWS_ST_HTTP_REQUEST_HEADERS)); |
| 1376 |
ASSERT_SUCCESS(s_do_sigv4_test_signing( |
| 1377 |
allocator, parent_folder, test_name, AWS_SIGNING_ALGORITHM_V4_ASYMMETRIC, AWS_ST_HTTP_REQUEST_QUERY_PARAMS)); |
| 1378 |
|
| 1379 |
aws_auth_library_clean_up(); |
| 1380 |
|
| 1381 |
return AWS_OP_SUCCESS; |
| 1382 |
} |
| 1383 |
|
| 1384 |
#define DECLARE_SIGV4A_TEST_SUITE_CASE(test_name, test_name_string) \ |
| 1385 |
static int s_sigv4a_##test_name##_test(struct aws_allocator *allocator, void *ctx) { \ |
| 1386 |
(void)ctx; \ |
| 1387 |
return s_do_sigv4a_test_case(allocator, test_name_string, "./v4a"); \ |
| 1388 |
} \ |
| 1389 |
AWS_TEST_CASE(sigv4a_##test_name##_test, s_sigv4a_##test_name##_test); |
| 1390 |
|
| 1391 |
DECLARE_SIGV4A_TEST_SUITE_CASE(get_header_key_duplicate, "get-header-key-duplicate"); |
| 1392 |
DECLARE_SIGV4A_TEST_SUITE_CASE(get_header_value_multiline, "get-header-value-multiline"); |
| 1393 |
DECLARE_SIGV4A_TEST_SUITE_CASE(get_header_value_order, "get-header-value-order"); |
| 1394 |
DECLARE_SIGV4A_TEST_SUITE_CASE(get_header_value_trim, "get-header-value-trim"); |
| 1395 |
DECLARE_SIGV4A_TEST_SUITE_CASE(get_unreserved, "get-unreserved"); |
| 1396 |
DECLARE_SIGV4A_TEST_SUITE_CASE(get_utf8, "get-utf8"); |
| 1397 |
DECLARE_SIGV4A_TEST_SUITE_CASE(get_vanilla, "get-vanilla"); |
| 1398 |
DECLARE_SIGV4A_TEST_SUITE_CASE(get_vanilla_empty_query_key, "get-vanilla-empty-query-key"); |
| 1399 |
DECLARE_SIGV4A_TEST_SUITE_CASE(get_vanilla_query, "get-vanilla-query"); |
| 1400 |
DECLARE_SIGV4A_TEST_SUITE_CASE(get_vanilla_query_order_key_case, "get-vanilla-query-order-key-case"); |
| 1401 |
DECLARE_SIGV4A_TEST_SUITE_CASE(get_vanilla_query_order_encoded, "get-vanilla-query-order-encoded"); |
| 1402 |
DECLARE_SIGV4A_TEST_SUITE_CASE(get_vanilla_unreserved, "get-vanilla-query-unreserved"); |
| 1403 |
DECLARE_SIGV4A_TEST_SUITE_CASE(get_vanilla_utf8_query, "get-vanilla-utf8-query"); |
| 1404 |
DECLARE_SIGV4A_TEST_SUITE_CASE(post_header_key_case, "post-header-key-case"); |
| 1405 |
DECLARE_SIGV4A_TEST_SUITE_CASE(post_header_key_sort, "post-header-key-sort"); |
| 1406 |
DECLARE_SIGV4A_TEST_SUITE_CASE(post_header_value_case, "post-header-value-case"); |
| 1407 |
DECLARE_SIGV4A_TEST_SUITE_CASE(post_vanilla, "post-vanilla"); |
| 1408 |
DECLARE_SIGV4A_TEST_SUITE_CASE(post_vanilla_empty_query_value, "post-vanilla-empty-query-value"); |
| 1409 |
DECLARE_SIGV4A_TEST_SUITE_CASE(post_vanilla_query, "post-vanilla-query"); |
| 1410 |
DECLARE_SIGV4A_TEST_SUITE_CASE(post_x_www_form_urlencoded, "post-x-www-form-urlencoded"); |
| 1411 |
DECLARE_SIGV4A_TEST_SUITE_CASE(post_x_www_form_urlencoded_parameters, "post-x-www-form-urlencoded-parameters"); |
| 1412 |
DECLARE_SIGV4A_TEST_SUITE_CASE(get_vanilla_with_session_token, "get-vanilla-with-session-token"); |
| 1413 |
|
| 1414 |
DECLARE_SIGV4A_TEST_SUITE_CASE(post_sts_header_after, "post-sts-header-after"); |
| 1415 |
DECLARE_SIGV4A_TEST_SUITE_CASE(post_sts_header_before, "post-sts-header-before"); |
| 1416 |
|
| 1417 |
DECLARE_SIGV4A_TEST_SUITE_CASE(get_relative_normalized, "get-relative-normalized"); |
| 1418 |
DECLARE_SIGV4A_TEST_SUITE_CASE(get_relative_unnormalized, "get-relative-unnormalized"); |
| 1419 |
DECLARE_SIGV4A_TEST_SUITE_CASE(get_relative_relative_normalized, "get-relative-relative-normalized"); |
| 1420 |
DECLARE_SIGV4A_TEST_SUITE_CASE(get_relative_relative_unnormalized, "get-relative-relative-unnormalized"); |
| 1421 |
DECLARE_SIGV4A_TEST_SUITE_CASE(get_slash_normalized, "get-slash-normalized"); |
| 1422 |
DECLARE_SIGV4A_TEST_SUITE_CASE(get_slash_unnormalized, "get-slash-unnormalized"); |
| 1423 |
DECLARE_SIGV4A_TEST_SUITE_CASE(get_slash_dot_slash_normalized, "get-slash-dot-slash-normalized"); |
| 1424 |
DECLARE_SIGV4A_TEST_SUITE_CASE(get_slash_dot_slash_unnormalized, "get-slash-dot-slash-unnormalized"); |
| 1425 |
DECLARE_SIGV4A_TEST_SUITE_CASE(get_slash_pointless_dot_normalized, "get-slash-pointless-dot-normalized"); |
| 1426 |
DECLARE_SIGV4A_TEST_SUITE_CASE(get_slash_pointless_dot_unnormalized, "get-slash-pointless-dot-unnormalized"); |
| 1427 |
DECLARE_SIGV4A_TEST_SUITE_CASE(get_slashes_normalized, "get-slashes-normalized"); |
| 1428 |
DECLARE_SIGV4A_TEST_SUITE_CASE(get_slashes_unnormalized, "get-slashes-unnormalized"); |
| 1429 |
DECLARE_SIGV4A_TEST_SUITE_CASE(get_space_normalized, "get-space-normalized"); |
| 1430 |
DECLARE_SIGV4A_TEST_SUITE_CASE(get_space_unnormalized, "get-space-unnormalized"); |
| 1431 |
|
| 1432 |
static int s_do_sigv4_test_case(struct aws_allocator *allocator, const char *test_name, const char *parent_folder) { |
| 1433 |
|
| 1434 |
|
| 1435 |
aws_auth_library_init(allocator); |
| 1436 |
|
| 1437 |
ASSERT_SUCCESS(s_do_sigv4_test_signing( |
| 1438 |
allocator, parent_folder, test_name, AWS_SIGNING_ALGORITHM_V4, AWS_ST_HTTP_REQUEST_HEADERS)); |
| 1439 |
ASSERT_SUCCESS(s_do_sigv4_test_signing( |
| 1440 |
allocator, parent_folder, test_name, AWS_SIGNING_ALGORITHM_V4, AWS_ST_HTTP_REQUEST_QUERY_PARAMS)); |
| 1441 |
|
| 1442 |
aws_auth_library_clean_up(); |
| 1443 |
|
| 1444 |
return AWS_OP_SUCCESS; |
| 1445 |
} |
| 1446 |
|
| 1447 |
#define DECLARE_SIGV4_TEST_SUITE_CASE(test_name, test_name_string) \ |
| 1448 |
static int s_sigv4_##test_name##_test(struct aws_allocator *allocator, void *ctx) { \ |
| 1449 |
(void)ctx; \ |
| 1450 |
return s_do_sigv4_test_case(allocator, test_name_string, "./v4"); \ |
| 1451 |
} \ |
| 1452 |
AWS_TEST_CASE(sigv4_##test_name##_test, s_sigv4_##test_name##_test); |
| 1453 |
|
| 1454 |
DECLARE_SIGV4_TEST_SUITE_CASE(get_header_key_duplicate, "get-header-key-duplicate"); |
| 1455 |
DECLARE_SIGV4_TEST_SUITE_CASE(get_header_value_multiline, "get-header-value-multiline"); |
| 1456 |
DECLARE_SIGV4_TEST_SUITE_CASE(get_header_value_order, "get-header-value-order"); |
| 1457 |
DECLARE_SIGV4_TEST_SUITE_CASE(get_header_value_trim, "get-header-value-trim"); |
| 1458 |
DECLARE_SIGV4_TEST_SUITE_CASE(get_unreserved, "get-unreserved"); |
| 1459 |
DECLARE_SIGV4_TEST_SUITE_CASE(get_utf8, "get-utf8"); |
| 1460 |
DECLARE_SIGV4_TEST_SUITE_CASE(get_vanilla, "get-vanilla"); |
| 1461 |
DECLARE_SIGV4_TEST_SUITE_CASE(get_vanilla_empty_query_key, "get-vanilla-empty-query-key"); |
| 1462 |
DECLARE_SIGV4_TEST_SUITE_CASE(get_vanilla_query, "get-vanilla-query"); |
| 1463 |
DECLARE_SIGV4_TEST_SUITE_CASE(get_vanilla_query_order_key_case, "get-vanilla-query-order-key-case"); |
| 1464 |
DECLARE_SIGV4_TEST_SUITE_CASE(get_vanilla_query_order_encoded, "get-vanilla-query-order-encoded"); |
| 1465 |
DECLARE_SIGV4_TEST_SUITE_CASE(get_vanilla_unreserved, "get-vanilla-query-unreserved"); |
| 1466 |
DECLARE_SIGV4_TEST_SUITE_CASE(get_vanilla_utf8_query, "get-vanilla-utf8-query"); |
| 1467 |
DECLARE_SIGV4_TEST_SUITE_CASE(post_header_key_case, "post-header-key-case"); |
| 1468 |
DECLARE_SIGV4_TEST_SUITE_CASE(post_header_key_sort, "post-header-key-sort"); |
| 1469 |
DECLARE_SIGV4_TEST_SUITE_CASE(post_header_value_case, "post-header-value-case"); |
| 1470 |
DECLARE_SIGV4_TEST_SUITE_CASE(post_vanilla, "post-vanilla"); |
| 1471 |
DECLARE_SIGV4_TEST_SUITE_CASE(post_vanilla_empty_query_value, "post-vanilla-empty-query-value"); |
| 1472 |
DECLARE_SIGV4_TEST_SUITE_CASE(post_vanilla_query, "post-vanilla-query"); |
| 1473 |
DECLARE_SIGV4_TEST_SUITE_CASE(post_x_www_form_urlencoded, "post-x-www-form-urlencoded"); |
| 1474 |
DECLARE_SIGV4_TEST_SUITE_CASE(post_x_www_form_urlencoded_parameters, "post-x-www-form-urlencoded-parameters"); |
| 1475 |
DECLARE_SIGV4_TEST_SUITE_CASE(get_vanilla_with_session_token, "get-vanilla-with-session-token"); |
| 1476 |
|
| 1477 |
DECLARE_SIGV4_TEST_SUITE_CASE(post_sts_header_after, "post-sts-header-after"); |
| 1478 |
DECLARE_SIGV4_TEST_SUITE_CASE(post_sts_header_before, "post-sts-header-before"); |
| 1479 |
|
| 1480 |
DECLARE_SIGV4_TEST_SUITE_CASE(get_relative_normalized, "get-relative-normalized"); |
| 1481 |
DECLARE_SIGV4_TEST_SUITE_CASE(get_relative_unnormalized, "get-relative-unnormalized"); |
| 1482 |
DECLARE_SIGV4_TEST_SUITE_CASE(get_relative_relative_normalized, "get-relative-relative-normalized"); |
| 1483 |
DECLARE_SIGV4_TEST_SUITE_CASE(get_relative_relative_unnormalized, "get-relative-relative-unnormalized"); |
| 1484 |
DECLARE_SIGV4_TEST_SUITE_CASE(get_slash_normalized, "get-slash-normalized"); |
| 1485 |
DECLARE_SIGV4_TEST_SUITE_CASE(get_slash_unnormalized, "get-slash-unnormalized"); |
| 1486 |
DECLARE_SIGV4_TEST_SUITE_CASE(get_slash_dot_slash_normalized, "get-slash-dot-slash-normalized"); |
| 1487 |
DECLARE_SIGV4_TEST_SUITE_CASE(get_slash_dot_slash_unnormalized, "get-slash-dot-slash-unnormalized"); |
| 1488 |
DECLARE_SIGV4_TEST_SUITE_CASE(get_slash_pointless_dot_normalized, "get-slash-pointless-dot-normalized"); |
| 1489 |
DECLARE_SIGV4_TEST_SUITE_CASE(get_slash_pointless_dot_unnormalized, "get-slash-pointless-dot-unnormalized"); |
| 1490 |
DECLARE_SIGV4_TEST_SUITE_CASE(get_slashes_normalized, "get-slashes-normalized"); |
| 1491 |
DECLARE_SIGV4_TEST_SUITE_CASE(get_slashes_unnormalized, "get-slashes-unnormalized"); |
| 1492 |
DECLARE_SIGV4_TEST_SUITE_CASE(get_space_normalized, "get-space-normalized"); |
| 1493 |
DECLARE_SIGV4_TEST_SUITE_CASE(get_space_unnormalized, "get-space-unnormalized"); |
| 1494 |
|
| 1495 |
AWS_STATIC_STRING_FROM_LITERAL(s_test_suite_service, "service"); |
| 1496 |
AWS_STATIC_STRING_FROM_LITERAL(s_test_suite_region, "us-east-1"); |
| 1497 |
AWS_STATIC_STRING_FROM_LITERAL(s_test_suite_access_key_id, "AKIDEXAMPLE"); |
| 1498 |
AWS_STATIC_STRING_FROM_LITERAL(s_test_suite_secret_access_key, "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY"); |
| 1499 |
AWS_STATIC_STRING_FROM_LITERAL(s_test_suite_date, "2015-08-30T12:36:00Z"); |
| 1500 |
|
| 1501 |
static int s_do_header_skip_test( |
| 1502 |
struct aws_allocator *allocator, |
| 1503 |
aws_should_sign_header_fn *should_sign, |
| 1504 |
const struct aws_string *request_contents, |
| 1505 |
const struct aws_string *expected_canonical_request) { |
| 1506 |
|
| 1507 |
aws_auth_library_init(allocator); |
| 1508 |
|
| 1509 |
struct aws_credentials *credentials = aws_credentials_new_from_string( |
| 1510 |
allocator, s_test_suite_access_key_id, s_test_suite_secret_access_key, NULL, UINT64_MAX); |
| 1511 |
ASSERT_NOT_NULL(credentials); |
| 1512 |
|
| 1513 |
struct aws_signing_config_aws config; |
| 1514 |
AWS_ZERO_STRUCT(config); |
| 1515 |
config.credentials = credentials; |
| 1516 |
config.algorithm = AWS_SIGNING_ALGORITHM_V4; |
| 1517 |
config.signature_type = AWS_ST_HTTP_REQUEST_HEADERS; |
| 1518 |
config.region = aws_byte_cursor_from_string(s_test_suite_region); |
| 1519 |
config.service = aws_byte_cursor_from_string(s_test_suite_service); |
| 1520 |
config.should_sign_header = should_sign; |
| 1521 |
|
| 1522 |
struct aws_byte_cursor date_cursor = aws_byte_cursor_from_string(s_test_suite_date); |
| 1523 |
ASSERT_SUCCESS(aws_date_time_init_from_str_cursor(&config.date, &date_cursor, AWS_DATE_FORMAT_ISO_8601)); |
| 1524 |
|
| 1525 |
struct aws_http_message *message = NULL; |
| 1526 |
struct aws_input_stream *body_stream = NULL; |
| 1527 |
ASSERT_SUCCESS(s_parse_request(allocator, aws_byte_cursor_from_string(request_contents), &message, &body_stream)); |
| 1528 |
struct aws_signable *signable = aws_signable_new_http_request(allocator, message); |
| 1529 |
|
| 1530 |
|
| 1531 |
aws_http_message_release(message); |
| 1532 |
|
| 1533 |
struct aws_signing_state_aws *signing_state = aws_signing_state_new(allocator, &config, signable, NULL, NULL); |
| 1534 |
ASSERT_NOT_NULL(signing_state); |
| 1535 |
|
| 1536 |
ASSERT_SUCCESS(aws_signing_build_canonical_request(signing_state)); |
| 1537 |
|
| 1538 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 1539 |
expected_canonical_request->bytes, |
| 1540 |
expected_canonical_request->len, |
| 1541 |
signing_state->canonical_request.buffer, |
| 1542 |
signing_state->canonical_request.len); |
| 1543 |
|
| 1544 |
aws_input_stream_destroy(body_stream); |
| 1545 |
aws_signing_state_destroy(signing_state); |
| 1546 |
aws_credentials_release(credentials); |
| 1547 |
aws_signable_destroy(signable); |
| 1548 |
|
| 1549 |
aws_auth_library_clean_up(); |
| 1550 |
|
| 1551 |
return AWS_OP_SUCCESS; |
| 1552 |
} |
| 1553 |
|
| 1554 |
AWS_STATIC_STRING_FROM_LITERAL( |
| 1555 |
s_skip_xray_header_request, |
| 1556 |
"GET / HTTP/1.1\n" |
| 1557 |
"Host:example.amazonaws.com\n" |
| 1558 |
"x-amzn-trace-id:fsdbofdshfdsjkjhfs\n\n"); |
| 1559 |
|
| 1560 |
AWS_STATIC_STRING_FROM_LITERAL( |
| 1561 |
s_skip_xray_header_expected_canonical_request, |
| 1562 |
"GET\n" |
| 1563 |
"/\n" |
| 1564 |
"\n" |
| 1565 |
"host:example.amazonaws.com\n" |
| 1566 |
"x-amz-date:20150830T123600Z\n" |
| 1567 |
"\n" |
| 1568 |
"host;x-amz-date\n" |
| 1569 |
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"); |
| 1570 |
|
| 1571 |
static int s_sigv4_skip_xray_header_test(struct aws_allocator *allocator, void *ctx) { |
| 1572 |
(void)ctx; |
| 1573 |
return s_do_header_skip_test( |
| 1574 |
allocator, NULL, s_skip_xray_header_request, s_skip_xray_header_expected_canonical_request); |
| 1575 |
} |
| 1576 |
AWS_TEST_CASE(sigv4_skip_xray_header_test, s_sigv4_skip_xray_header_test); |
| 1577 |
|
| 1578 |
AWS_STATIC_STRING_FROM_LITERAL( |
| 1579 |
s_skip_user_agent_header_request, |
| 1580 |
"GET / HTTP/1.1\n" |
| 1581 |
"User-agent:c sdk v1.0\n" |
| 1582 |
"Host:example.amazonaws.com\n\n"); |
| 1583 |
|
| 1584 |
AWS_STATIC_STRING_FROM_LITERAL( |
| 1585 |
s_skip_user_agent_header_expected_canonical_request, |
| 1586 |
"GET\n" |
| 1587 |
"/\n" |
| 1588 |
"\n" |
| 1589 |
"host:example.amazonaws.com\n" |
| 1590 |
"x-amz-date:20150830T123600Z\n" |
| 1591 |
"\n" |
| 1592 |
"host;x-amz-date\n" |
| 1593 |
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"); |
| 1594 |
|
| 1595 |
static int s_sigv4_skip_user_agent_header_test(struct aws_allocator *allocator, void *ctx) { |
| 1596 |
(void)ctx; |
| 1597 |
return s_do_header_skip_test( |
| 1598 |
allocator, NULL, s_skip_user_agent_header_request, s_skip_user_agent_header_expected_canonical_request); |
| 1599 |
} |
| 1600 |
AWS_TEST_CASE(sigv4_skip_user_agent_header_test, s_sigv4_skip_user_agent_header_test); |
| 1601 |
|
| 1602 |
AWS_STATIC_STRING_FROM_LITERAL( |
| 1603 |
s_skip_transfer_encoding_header_request, |
| 1604 |
"GET / HTTP/1.1\n" |
| 1605 |
"Transfer-Encoding: chunked\n" |
| 1606 |
"Host:example.amazonaws.com\n\n"); |
| 1607 |
|
| 1608 |
AWS_STATIC_STRING_FROM_LITERAL( |
| 1609 |
s_skip_transfer_encoding_header_expected_canonical_request, |
| 1610 |
"GET\n" |
| 1611 |
"/\n" |
| 1612 |
"\n" |
| 1613 |
"host:example.amazonaws.com\n" |
| 1614 |
"x-amz-date:20150830T123600Z\n" |
| 1615 |
"\n" |
| 1616 |
"host;x-amz-date\n" |
| 1617 |
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"); |
| 1618 |
|
| 1619 |
static int s_sigv4_skip_transfer_encoding_header_test(struct aws_allocator *allocator, void *ctx) { |
| 1620 |
(void)ctx; |
| 1621 |
return s_do_header_skip_test( |
| 1622 |
allocator, |
| 1623 |
NULL, |
| 1624 |
s_skip_transfer_encoding_header_request, |
| 1625 |
s_skip_transfer_encoding_header_expected_canonical_request); |
| 1626 |
} |
| 1627 |
AWS_TEST_CASE(sigv4_skip_transfer_encoding_header_test, s_sigv4_skip_transfer_encoding_header_test); |
| 1628 |
|
| 1629 |
AWS_STATIC_STRING_FROM_LITERAL( |
| 1630 |
s_skip_custom_header_request, |
| 1631 |
"GET / HTTP/1.1\n" |
| 1632 |
"MyHeader:Blahblah\n" |
| 1633 |
"Host:example.amazonaws.com\n" |
| 1634 |
"AnotherHeader:Oof\n\n"); |
| 1635 |
|
| 1636 |
AWS_STATIC_STRING_FROM_LITERAL( |
| 1637 |
s_skip_custom_header_expected_canonical_request, |
| 1638 |
"GET\n" |
| 1639 |
"/\n" |
| 1640 |
"\n" |
| 1641 |
"host:example.amazonaws.com\n" |
| 1642 |
"x-amz-date:20150830T123600Z\n" |
| 1643 |
"\n" |
| 1644 |
"host;x-amz-date\n" |
| 1645 |
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"); |
| 1646 |
|
| 1647 |
static bool s_should_sign_header(const struct aws_byte_cursor *name, void *userdata) { |
| 1648 |
(void)userdata; |
| 1649 |
|
| 1650 |
struct aws_byte_cursor my_header_cursor = aws_byte_cursor_from_c_str("myheader"); |
| 1651 |
struct aws_byte_cursor another_header_cursor = aws_byte_cursor_from_c_str("anOtherHeader"); |
| 1652 |
|
| 1653 |
if (aws_byte_cursor_eq_ignore_case(name, &my_header_cursor) || |
| 1654 |
aws_byte_cursor_eq_ignore_case(name, &another_header_cursor)) { |
| 1655 |
return false; |
| 1656 |
} |
| 1657 |
|
| 1658 |
return true; |
| 1659 |
} |
| 1660 |
|
| 1661 |
static int s_sigv4_skip_custom_header_test(struct aws_allocator *allocator, void *ctx) { |
| 1662 |
(void)ctx; |
| 1663 |
return s_do_header_skip_test( |
| 1664 |
allocator, s_should_sign_header, s_skip_custom_header_request, s_skip_custom_header_expected_canonical_request); |
| 1665 |
} |
| 1666 |
AWS_TEST_CASE(sigv4_skip_custom_header_test, s_sigv4_skip_custom_header_test); |
| 1667 |
|
| 1668 |
static int s_do_forbidden_header_param_test( |
| 1669 |
struct aws_allocator *allocator, |
| 1670 |
const struct aws_string *request_contents, |
| 1671 |
enum aws_auth_errors expected_error) { |
| 1672 |
|
| 1673 |
aws_auth_library_init(allocator); |
| 1674 |
|
| 1675 |
struct aws_credentials *credentials = aws_credentials_new_from_string( |
| 1676 |
allocator, s_test_suite_access_key_id, s_test_suite_secret_access_key, NULL, UINT64_MAX); |
| 1677 |
ASSERT_NOT_NULL(credentials); |
| 1678 |
|
| 1679 |
struct aws_signing_config_aws config; |
| 1680 |
AWS_ZERO_STRUCT(config); |
| 1681 |
config.credentials = credentials; |
| 1682 |
config.algorithm = AWS_SIGNING_ALGORITHM_V4; |
| 1683 |
config.signature_type = AWS_ST_HTTP_REQUEST_HEADERS; |
| 1684 |
config.region = aws_byte_cursor_from_string(s_test_suite_region); |
| 1685 |
config.service = aws_byte_cursor_from_string(s_test_suite_service); |
| 1686 |
|
| 1687 |
struct aws_byte_cursor date_cursor = aws_byte_cursor_from_string(s_test_suite_date); |
| 1688 |
ASSERT_SUCCESS(aws_date_time_init_from_str_cursor(&config.date, &date_cursor, AWS_DATE_FORMAT_ISO_8601)); |
| 1689 |
|
| 1690 |
struct aws_http_message *message = NULL; |
| 1691 |
struct aws_input_stream *body_stream = NULL; |
| 1692 |
ASSERT_SUCCESS(s_parse_request(allocator, aws_byte_cursor_from_string(request_contents), &message, &body_stream)); |
| 1693 |
struct aws_signable *signable = aws_signable_new_http_request(allocator, message); |
| 1694 |
|
| 1695 |
struct aws_signing_state_aws *signing_state = aws_signing_state_new(allocator, &config, signable, NULL, NULL); |
| 1696 |
ASSERT_NOT_NULL(signing_state); |
| 1697 |
|
| 1698 |
ASSERT_FAILS(aws_signing_build_canonical_request(signing_state)); |
| 1699 |
ASSERT_TRUE(aws_last_error() == expected_error); |
| 1700 |
|
| 1701 |
aws_input_stream_destroy(body_stream); |
| 1702 |
aws_http_message_release(message); |
| 1703 |
|
| 1704 |
aws_signing_state_destroy(signing_state); |
| 1705 |
aws_credentials_release(credentials); |
| 1706 |
aws_signable_destroy(signable); |
| 1707 |
|
| 1708 |
aws_auth_library_clean_up(); |
| 1709 |
|
| 1710 |
return AWS_OP_SUCCESS; |
| 1711 |
} |
| 1712 |
|
| 1713 |
AWS_STATIC_STRING_FROM_LITERAL( |
| 1714 |
s_amz_date_header_request, |
| 1715 |
"GET / HTTP/1.1\n" |
| 1716 |
"Host:example.amazonaws.com\n" |
| 1717 |
"X-Amz-Date:20150830T123600Z"); |
| 1718 |
|
| 1719 |
static int s_sigv4_fail_date_header_test(struct aws_allocator *allocator, void *ctx) { |
| 1720 |
(void)ctx; |
| 1721 |
return s_do_forbidden_header_param_test( |
| 1722 |
allocator, s_amz_date_header_request, AWS_AUTH_SIGNING_ILLEGAL_REQUEST_HEADER); |
| 1723 |
} |
| 1724 |
AWS_TEST_CASE(sigv4_fail_date_header_test, s_sigv4_fail_date_header_test); |
| 1725 |
|
| 1726 |
AWS_STATIC_STRING_FROM_LITERAL( |
| 1727 |
s_amz_content_sha256_header_request, |
| 1728 |
"GET / HTTP/1.1\n" |
| 1729 |
"Host:example.amazonaws.com\n" |
| 1730 |
"x-amz-content-sha256:lieslieslies"); |
| 1731 |
|
| 1732 |
static int s_sigv4_fail_content_header_test(struct aws_allocator *allocator, void *ctx) { |
| 1733 |
(void)ctx; |
| 1734 |
return s_do_forbidden_header_param_test( |
| 1735 |
allocator, s_amz_content_sha256_header_request, AWS_AUTH_SIGNING_ILLEGAL_REQUEST_HEADER); |
| 1736 |
} |
| 1737 |
AWS_TEST_CASE(sigv4_fail_content_header_test, s_sigv4_fail_content_header_test); |
| 1738 |
|
| 1739 |
AWS_STATIC_STRING_FROM_LITERAL( |
| 1740 |
s_authorization_header_request, |
| 1741 |
"GET / HTTP/1.1\n" |
| 1742 |
"Host:example.amazonaws.com\n" |
| 1743 |
"Authorization:lieslieslies"); |
| 1744 |
|
| 1745 |
static int s_sigv4_fail_authorization_header_test(struct aws_allocator *allocator, void *ctx) { |
| 1746 |
(void)ctx; |
| 1747 |
return s_do_forbidden_header_param_test( |
| 1748 |
allocator, s_authorization_header_request, AWS_AUTH_SIGNING_ILLEGAL_REQUEST_HEADER); |
| 1749 |
} |
| 1750 |
AWS_TEST_CASE(sigv4_fail_authorization_header_test, s_sigv4_fail_authorization_header_test); |
| 1751 |
|
| 1752 |
AWS_STATIC_STRING_FROM_LITERAL( |
| 1753 |
s_amz_signature_param_request, |
| 1754 |
"GET /?X-Amz-Signature=Something HTTP/1.1\n" |
| 1755 |
"Host:example.amazonaws.com\n"); |
| 1756 |
|
| 1757 |
static int s_sigv4_fail_signature_param_test(struct aws_allocator *allocator, void *ctx) { |
| 1758 |
(void)ctx; |
| 1759 |
return s_do_forbidden_header_param_test( |
| 1760 |
allocator, s_amz_signature_param_request, AWS_AUTH_SIGNING_ILLEGAL_REQUEST_QUERY_PARAM); |
| 1761 |
} |
| 1762 |
AWS_TEST_CASE(sigv4_fail_signature_param_test, s_sigv4_fail_signature_param_test); |
| 1763 |
|
| 1764 |
AWS_STATIC_STRING_FROM_LITERAL( |
| 1765 |
s_amz_date_param_request, |
| 1766 |
"GET /?X-Amz-Date=Tomorrow HTTP/1.1\n" |
| 1767 |
"Host:example.amazonaws.com\n"); |
| 1768 |
|
| 1769 |
static int s_sigv4_fail_date_param_test(struct aws_allocator *allocator, void *ctx) { |
| 1770 |
(void)ctx; |
| 1771 |
return s_do_forbidden_header_param_test( |
| 1772 |
allocator, s_amz_date_param_request, AWS_AUTH_SIGNING_ILLEGAL_REQUEST_QUERY_PARAM); |
| 1773 |
} |
| 1774 |
AWS_TEST_CASE(sigv4_fail_date_param_test, s_sigv4_fail_date_param_test); |
| 1775 |
|
| 1776 |
AWS_STATIC_STRING_FROM_LITERAL( |
| 1777 |
s_amz_credential_param_request, |
| 1778 |
"GET /?X-Amz-Credential=TopSekrit HTTP/1.1\n" |
| 1779 |
"Host:example.amazonaws.com\n"); |
| 1780 |
|
| 1781 |
static int s_sigv4_fail_credential_param_test(struct aws_allocator *allocator, void *ctx) { |
| 1782 |
(void)ctx; |
| 1783 |
return s_do_forbidden_header_param_test( |
| 1784 |
allocator, s_amz_credential_param_request, AWS_AUTH_SIGNING_ILLEGAL_REQUEST_QUERY_PARAM); |
| 1785 |
} |
| 1786 |
AWS_TEST_CASE(sigv4_fail_credential_param_test, s_sigv4_fail_credential_param_test); |
| 1787 |
|
| 1788 |
AWS_STATIC_STRING_FROM_LITERAL( |
| 1789 |
s_amz_algorithm_param_request, |
| 1790 |
"GET /?X-Amz-Algorithm=BubbleSort HTTP/1.1\n" |
| 1791 |
"Host:example.amazonaws.com\n"); |
| 1792 |
|
| 1793 |
static int s_sigv4_fail_algorithm_param_test(struct aws_allocator *allocator, void *ctx) { |
| 1794 |
(void)ctx; |
| 1795 |
return s_do_forbidden_header_param_test( |
| 1796 |
allocator, s_amz_algorithm_param_request, AWS_AUTH_SIGNING_ILLEGAL_REQUEST_QUERY_PARAM); |
| 1797 |
} |
| 1798 |
AWS_TEST_CASE(sigv4_fail_algorithm_param_test, s_sigv4_fail_algorithm_param_test); |
| 1799 |
|
| 1800 |
AWS_STATIC_STRING_FROM_LITERAL( |
| 1801 |
s_amz_signed_headers_param_request, |
| 1802 |
"GET /?X-Amz-SignedHeaders=User-Agent HTTP/1.1\n" |
| 1803 |
"Host:example.amazonaws.com\n"); |
| 1804 |
|
| 1805 |
static int s_sigv4_fail_signed_headers_param_test(struct aws_allocator *allocator, void *ctx) { |
| 1806 |
(void)ctx; |
| 1807 |
return s_do_forbidden_header_param_test( |
| 1808 |
allocator, s_amz_signed_headers_param_request, AWS_AUTH_SIGNING_ILLEGAL_REQUEST_QUERY_PARAM); |
| 1809 |
} |
| 1810 |
AWS_TEST_CASE(sigv4_fail_signed_headers_param_test, s_sigv4_fail_signed_headers_param_test); |
| 1811 |
|
| 1812 |
struct null_credentials_state { |
| 1813 |
struct aws_signing_result *result; |
| 1814 |
int error_code; |
| 1815 |
}; |
| 1816 |
|
| 1817 |
static void s_null_credentials_on_signing_complete(struct aws_signing_result *result, int error_code, void *userdata) { |
| 1818 |
|
| 1819 |
struct null_credentials_state *state = userdata; |
| 1820 |
state->result = result; |
| 1821 |
state->error_code = error_code; |
| 1822 |
} |
| 1823 |
|
| 1824 |
static int s_signer_null_credentials_test(struct aws_allocator *allocator, void *ctx) { |
| 1825 |
(void)ctx; |
| 1826 |
|
| 1827 |
struct get_credentials_mock_result results = { |
| 1828 |
.credentials = NULL, |
| 1829 |
.error_code = AWS_AUTH_SIGNING_NO_CREDENTIALS, |
| 1830 |
}; |
| 1831 |
|
| 1832 |
struct aws_http_message *request = aws_http_message_new_request(allocator); |
| 1833 |
struct aws_signable *signable = aws_signable_new_http_request(allocator, request); |
| 1834 |
|
| 1835 |
struct aws_signing_config_aws config = { |
| 1836 |
.config_type = AWS_SIGNING_CONFIG_AWS, |
| 1837 |
.algorithm = AWS_SIGNING_ALGORITHM_V4, |
| 1838 |
.signature_type = AWS_ST_HTTP_REQUEST_HEADERS, |
| 1839 |
.region = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("us-east-1"), |
| 1840 |
.service = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("elasticdohickeyservice"), |
| 1841 |
}; |
| 1842 |
config.credentials_provider = aws_credentials_provider_new_mock(allocator, &results, 1, NULL); |
| 1843 |
aws_date_time_init_now(&config.date); |
| 1844 |
|
| 1845 |
struct null_credentials_state state; |
| 1846 |
AWS_ZERO_STRUCT(state); |
| 1847 |
|
| 1848 |
ASSERT_SUCCESS(aws_sign_request_aws( |
| 1849 |
allocator, |
| 1850 |
signable, |
| 1851 |
(struct aws_signing_config_base *)&config, |
| 1852 |
s_null_credentials_on_signing_complete, |
| 1853 |
&state)); |
| 1854 |
|
| 1855 |
ASSERT_PTR_EQUALS(NULL, state.result); |
| 1856 |
ASSERT_INT_EQUALS(AWS_AUTH_SIGNING_NO_CREDENTIALS, state.error_code); |
| 1857 |
|
| 1858 |
aws_credentials_provider_release(config.credentials_provider); |
| 1859 |
aws_signable_destroy(signable); |
| 1860 |
aws_http_message_release(request); |
| 1861 |
|
| 1862 |
return AWS_OP_SUCCESS; |
| 1863 |
} |
| 1864 |
AWS_TEST_CASE(signer_null_credentials_test, s_signer_null_credentials_test); |
| 1865 |
|
| 1866 |
static void s_anonymous_credentials_on_signing_complete( |
| 1867 |
struct aws_signing_result *result, |
| 1868 |
int error_code, |
| 1869 |
void *userdata) { |
| 1870 |
|
| 1871 |
struct null_credentials_state *state = userdata; |
| 1872 |
state->result = result; |
| 1873 |
state->error_code = error_code; |
| 1874 |
AWS_FATAL_ASSERT(result->properties.p_impl != NULL); |
| 1875 |
AWS_FATAL_ASSERT(result->property_lists.p_impl != NULL); |
| 1876 |
} |
| 1877 |
|
| 1878 |
static int s_signer_anonymous_credentials_test(struct aws_allocator *allocator, void *ctx) { |
| 1879 |
(void)ctx; |
| 1880 |
|
| 1881 |
struct aws_credentials *credentials = aws_credentials_new_anonymous(allocator); |
| 1882 |
ASSERT_NOT_NULL(credentials); |
| 1883 |
|
| 1884 |
struct aws_http_message *request = aws_http_message_new_request(allocator); |
| 1885 |
struct aws_signable *signable = aws_signable_new_http_request(allocator, request); |
| 1886 |
|
| 1887 |
struct aws_signing_config_aws config = { |
| 1888 |
.config_type = AWS_SIGNING_CONFIG_AWS, |
| 1889 |
.algorithm = AWS_SIGNING_ALGORITHM_V4, |
| 1890 |
.signature_type = AWS_ST_HTTP_REQUEST_HEADERS, |
| 1891 |
.region = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("us-east-1"), |
| 1892 |
.service = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("elasticdohickeyservice"), |
| 1893 |
.credentials = credentials, |
| 1894 |
}; |
| 1895 |
|
| 1896 |
struct null_credentials_state state; |
| 1897 |
AWS_ZERO_STRUCT(state); |
| 1898 |
|
| 1899 |
ASSERT_SUCCESS(aws_sign_request_aws( |
| 1900 |
allocator, |
| 1901 |
signable, |
| 1902 |
(struct aws_signing_config_base *)&config, |
| 1903 |
s_anonymous_credentials_on_signing_complete, |
| 1904 |
&state)); |
| 1905 |
|
| 1906 |
ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, state.error_code); |
| 1907 |
ASSERT_NOT_NULL(state.result); |
| 1908 |
|
| 1909 |
aws_credentials_release(credentials); |
| 1910 |
aws_signable_destroy(signable); |
| 1911 |
aws_http_message_release(request); |
| 1912 |
|
| 1913 |
return AWS_OP_SUCCESS; |
| 1914 |
} |
| 1915 |
AWS_TEST_CASE(signer_anonymous_credentials_test, s_signer_anonymous_credentials_test); |
| 1916 |
|
| 1917 |
static int s_signer_anonymous_credentials_provider_test(struct aws_allocator *allocator, void *ctx) { |
| 1918 |
(void)ctx; |
| 1919 |
|
| 1920 |
struct get_credentials_mock_result mock_result = { |
| 1921 |
.credentials = aws_credentials_new_anonymous(allocator), |
| 1922 |
.error_code = AWS_ERROR_SUCCESS, |
| 1923 |
}; |
| 1924 |
|
| 1925 |
struct aws_http_message *request = aws_http_message_new_request(allocator); |
| 1926 |
struct aws_signable *signable = aws_signable_new_http_request(allocator, request); |
| 1927 |
|
| 1928 |
struct aws_signing_config_aws config = { |
| 1929 |
.config_type = AWS_SIGNING_CONFIG_AWS, |
| 1930 |
.algorithm = AWS_SIGNING_ALGORITHM_V4, |
| 1931 |
.signature_type = AWS_ST_HTTP_REQUEST_HEADERS, |
| 1932 |
.region = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("us-east-1"), |
| 1933 |
.service = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("elasticdohickeyservice"), |
| 1934 |
}; |
| 1935 |
|
| 1936 |
config.credentials_provider = aws_credentials_provider_new_mock(allocator, &mock_result, 1, NULL); |
| 1937 |
|
| 1938 |
struct null_credentials_state state; |
| 1939 |
AWS_ZERO_STRUCT(state); |
| 1940 |
|
| 1941 |
ASSERT_SUCCESS(aws_sign_request_aws( |
| 1942 |
allocator, |
| 1943 |
signable, |
| 1944 |
(struct aws_signing_config_base *)&config, |
| 1945 |
s_anonymous_credentials_on_signing_complete, |
| 1946 |
&state)); |
| 1947 |
|
| 1948 |
ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, state.error_code); |
| 1949 |
ASSERT_NOT_NULL(state.result); |
| 1950 |
|
| 1951 |
aws_credentials_release(mock_result.credentials); |
| 1952 |
aws_credentials_provider_release(config.credentials_provider); |
| 1953 |
aws_signable_destroy(signable); |
| 1954 |
aws_http_message_release(request); |
| 1955 |
|
| 1956 |
return AWS_OP_SUCCESS; |
| 1957 |
} |
| 1958 |
AWS_TEST_CASE(signer_anonymous_credentials_provider_test, s_signer_anonymous_credentials_provider_test); |