| 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/auth/signing_config.h> |
| 13 |
#include <aws/auth/signing_result.h> |
| 14 |
#include <aws/cal/ecc.h> |
| 15 |
#include <aws/common/condition_variable.h> |
| 16 |
#include <aws/common/encoding.h> |
| 17 |
#include <aws/common/mutex.h> |
| 18 |
#include <aws/common/string.h> |
| 19 |
#include <aws/http/connection.h> |
| 20 |
#include <aws/http/request_response.h> |
| 21 |
#include <aws/io/channel_bootstrap.h> |
| 22 |
#include <aws/io/event_loop.h> |
| 23 |
#include <aws/io/socket.h> |
| 24 |
#include <aws/io/stream.h> |
| 25 |
|
| 26 |
AWS_STATIC_STRING_FROM_LITERAL(s_event_access_key_id, "access"); |
| 27 |
AWS_STATIC_STRING_FROM_LITERAL(s_event_secret_access_key, "secret"); |
| 28 |
AWS_STATIC_STRING_FROM_LITERAL(s_event_test_region, "us-east-1"); |
| 29 |
AWS_STATIC_STRING_FROM_LITERAL(s_event_test_service, "demo"); |
| 30 |
AWS_STATIC_STRING_FROM_LITERAL(s_event_request_date, "Fri, 16 Jan 1981 06:30:00 GMT"); |
| 31 |
AWS_STATIC_STRING_FROM_LITERAL(s_event_test_date, "Fri, 16 Jan 1981 06:30:01 GMT"); |
| 32 |
AWS_STATIC_STRING_FROM_LITERAL(s_event_test_date_2, "Fri, 16 Jan 1981 06:30:02 GMT"); |
| 33 |
AWS_STATIC_STRING_FROM_LITERAL(s_event_test_date_3, "Fri, 16 Jan 1981 06:30:03 GMT"); |
| 34 |
AWS_STATIC_STRING_FROM_LITERAL(s_event_test_date_4, "Fri, 16 Jan 1981 06:30:04 GMT"); |
| 35 |
|
| 36 |
static struct aws_http_header s_host_header = { |
| 37 |
.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("host"), |
| 38 |
.value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("demo.us-east-1.amazonaws.com"), |
| 39 |
}; |
| 40 |
|
| 41 |
static struct aws_http_header s_content_encoding_header = { |
| 42 |
.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Encoding"), |
| 43 |
.value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("application/vnd.amazon.eventstream"), |
| 44 |
}; |
| 45 |
|
| 46 |
AWS_STATIC_STRING_FROM_LITERAL(s_event_test_path, "/streaming"); |
| 47 |
|
| 48 |
static struct aws_http_message *s_build_event_request(struct aws_allocator *allocator) { |
| 49 |
struct aws_http_message *request = aws_http_message_new_request(allocator); |
| 50 |
|
| 51 |
aws_http_message_add_header(request, s_host_header); |
| 52 |
aws_http_message_add_header(request, s_content_encoding_header); |
| 53 |
aws_http_message_set_request_method(request, aws_http_method_post); |
| 54 |
aws_http_message_set_request_path(request, aws_byte_cursor_from_string(s_event_test_path)); |
| 55 |
|
| 56 |
return request; |
| 57 |
} |
| 58 |
|
| 59 |
static int s_initialize_request_signing_config( |
| 60 |
struct aws_signing_config_aws *config, |
| 61 |
struct aws_credentials *credentials) { |
| 62 |
config->config_type = AWS_SIGNING_CONFIG_AWS; |
| 63 |
config->algorithm = AWS_SIGNING_ALGORITHM_V4; |
| 64 |
config->signature_type = AWS_ST_HTTP_REQUEST_HEADERS; |
| 65 |
config->region = aws_byte_cursor_from_string(s_event_test_region); |
| 66 |
config->service = aws_byte_cursor_from_string(s_event_test_service); |
| 67 |
|
| 68 |
struct aws_byte_cursor event_test_date_cursor = aws_byte_cursor_from_string(s_event_request_date); |
| 69 |
if (aws_date_time_init_from_str_cursor(&config->date, &event_test_date_cursor, AWS_DATE_FORMAT_RFC822)) { |
| 70 |
return AWS_OP_ERR; |
| 71 |
} |
| 72 |
|
| 73 |
config->flags.use_double_uri_encode = false; |
| 74 |
config->flags.should_normalize_uri_path = true; |
| 75 |
config->signed_body_value = g_aws_signed_body_value_streaming_aws4_hmac_sha256_events; |
| 76 |
config->signed_body_header = AWS_SBHT_X_AMZ_CONTENT_SHA256; |
| 77 |
config->credentials = credentials; |
| 78 |
|
| 79 |
return AWS_OP_SUCCESS; |
| 80 |
} |
| 81 |
|
| 82 |
static int s_initialize_event_signing_config( |
| 83 |
struct aws_signing_config_aws *config, |
| 84 |
struct aws_credentials *credentials) { |
| 85 |
config->config_type = AWS_SIGNING_CONFIG_AWS; |
| 86 |
config->algorithm = AWS_SIGNING_ALGORITHM_V4; |
| 87 |
config->signature_type = AWS_ST_HTTP_REQUEST_EVENT; |
| 88 |
config->region = aws_byte_cursor_from_string(s_event_test_region); |
| 89 |
config->service = aws_byte_cursor_from_string(s_event_test_service); |
| 90 |
|
| 91 |
config->flags.use_double_uri_encode = false; |
| 92 |
config->flags.should_normalize_uri_path = true; |
| 93 |
config->signed_body_header = AWS_SBHT_NONE; |
| 94 |
config->credentials = credentials; |
| 95 |
|
| 96 |
return AWS_OP_SUCCESS; |
| 97 |
} |
| 98 |
|
| 99 |
struct event_signing_tester { |
| 100 |
struct aws_credentials *credentials; |
| 101 |
struct aws_http_message *request; |
| 102 |
struct aws_signable *request_signable; |
| 103 |
struct aws_signing_config_aws request_signing_config; |
| 104 |
struct aws_signing_config_aws event_signing_config; |
| 105 |
struct aws_byte_buf chunk1; |
| 106 |
struct aws_byte_buf chunk2; |
| 107 |
struct aws_byte_buf chunk3; |
| 108 |
|
| 109 |
struct aws_input_stream *chunk1_stream; |
| 110 |
struct aws_input_stream *chunk2_stream; |
| 111 |
struct aws_input_stream *chunk3_stream; |
| 112 |
|
| 113 |
struct aws_mutex mutex; |
| 114 |
bool request_completed; |
| 115 |
|
| 116 |
struct aws_byte_buf request_authorization_header; |
| 117 |
struct aws_byte_buf last_signature; |
| 118 |
}; |
| 119 |
|
| 120 |
#define EVENT_SIZE 1 |
| 121 |
|
| 122 |
static int s_event_signing_tester_init(struct aws_allocator *allocator, struct event_signing_tester *tester) { |
| 123 |
tester->credentials = |
| 124 |
aws_credentials_new_from_string(allocator, s_event_access_key_id, s_event_secret_access_key, NULL, UINT64_MAX); |
| 125 |
|
| 126 |
tester->request = s_build_event_request(allocator); |
| 127 |
tester->request_signable = aws_signable_new_http_request(allocator, tester->request); |
| 128 |
|
| 129 |
AWS_ZERO_STRUCT(tester->request_signing_config); |
| 130 |
ASSERT_SUCCESS(s_initialize_request_signing_config(&tester->request_signing_config, tester->credentials)); |
| 131 |
ASSERT_SUCCESS(s_initialize_event_signing_config(&tester->event_signing_config, tester->credentials)); |
| 132 |
|
| 133 |
ASSERT_SUCCESS(aws_byte_buf_init(&tester->request_authorization_header, allocator, 512)); |
| 134 |
ASSERT_SUCCESS(aws_byte_buf_init(&tester->last_signature, allocator, 128)); |
| 135 |
|
| 136 |
ASSERT_SUCCESS(aws_byte_buf_init(&tester->chunk1, allocator, EVENT_SIZE)); |
| 137 |
ASSERT_TRUE(aws_byte_buf_write_u8_n(&tester->chunk1, 'A', EVENT_SIZE)); |
| 138 |
|
| 139 |
ASSERT_SUCCESS(aws_byte_buf_init(&tester->chunk2, allocator, EVENT_SIZE)); |
| 140 |
ASSERT_TRUE(aws_byte_buf_write_u8_n(&tester->chunk2, 'B', EVENT_SIZE)); |
| 141 |
|
| 142 |
ASSERT_SUCCESS(aws_byte_buf_init(&tester->chunk3, allocator, EVENT_SIZE)); |
| 143 |
ASSERT_TRUE(aws_byte_buf_write_u8_n(&tester->chunk3, 'C', EVENT_SIZE)); |
| 144 |
|
| 145 |
struct aws_byte_cursor chunk1_cursor = aws_byte_cursor_from_buf(&tester->chunk1); |
| 146 |
tester->chunk1_stream = aws_input_stream_new_from_cursor(allocator, &chunk1_cursor); |
| 147 |
|
| 148 |
struct aws_byte_cursor chunk2_cursor = aws_byte_cursor_from_buf(&tester->chunk2); |
| 149 |
tester->chunk2_stream = aws_input_stream_new_from_cursor(allocator, &chunk2_cursor); |
| 150 |
|
| 151 |
struct aws_byte_cursor chunk3_cursor = aws_byte_cursor_from_buf(&tester->chunk3); |
| 152 |
tester->chunk3_stream = aws_input_stream_new_from_cursor(allocator, &chunk3_cursor); |
| 153 |
|
| 154 |
aws_mutex_init(&tester->mutex); |
| 155 |
tester->request_completed = false; |
| 156 |
|
| 157 |
return AWS_OP_SUCCESS; |
| 158 |
} |
| 159 |
|
| 160 |
static void s_event_signing_tester_cleanup(struct event_signing_tester *tester) { |
| 161 |
aws_signable_destroy(tester->request_signable); |
| 162 |
aws_http_message_release(tester->request); |
| 163 |
aws_credentials_release(tester->credentials); |
| 164 |
aws_byte_buf_clean_up(&tester->request_authorization_header); |
| 165 |
aws_byte_buf_clean_up(&tester->last_signature); |
| 166 |
aws_byte_buf_clean_up(&tester->chunk1); |
| 167 |
aws_byte_buf_clean_up(&tester->chunk2); |
| 168 |
aws_byte_buf_clean_up(&tester->chunk3); |
| 169 |
|
| 170 |
aws_input_stream_destroy(tester->chunk1_stream); |
| 171 |
aws_input_stream_destroy(tester->chunk2_stream); |
| 172 |
aws_input_stream_destroy(tester->chunk3_stream); |
| 173 |
|
| 174 |
aws_mutex_clean_up(&tester->mutex); |
| 175 |
} |
| 176 |
|
| 177 |
static void s_on_request_signing_complete(struct aws_signing_result *result, int error_code, void *userdata) { |
| 178 |
AWS_FATAL_ASSERT(error_code == 0); |
| 179 |
struct event_signing_tester *tester = userdata; |
| 180 |
|
| 181 |
struct aws_array_list *headers = NULL; |
| 182 |
aws_signing_result_get_property_list(result, g_aws_http_headers_property_list_name, &headers); |
| 183 |
|
| 184 |
struct aws_byte_cursor auth_header_name = aws_byte_cursor_from_string(g_aws_signing_authorization_header_name); |
| 185 |
struct aws_byte_cursor auth_header_value; |
| 186 |
AWS_ZERO_STRUCT(auth_header_value); |
| 187 |
for (size_t i = 0; i < aws_array_list_length(headers); ++i) { |
| 188 |
struct aws_signing_result_property pair; |
| 189 |
AWS_ZERO_STRUCT(pair); |
| 190 |
if (aws_array_list_get_at(headers, &pair, i)) { |
| 191 |
continue; |
| 192 |
} |
| 193 |
|
| 194 |
if (pair.name == NULL) { |
| 195 |
continue; |
| 196 |
} |
| 197 |
|
| 198 |
struct aws_byte_cursor pair_name_cursor = aws_byte_cursor_from_string(pair.name); |
| 199 |
if (aws_byte_cursor_eq_ignore_case(&pair_name_cursor, &auth_header_name)) { |
| 200 |
auth_header_value = aws_byte_cursor_from_string(pair.value); |
| 201 |
break; |
| 202 |
} |
| 203 |
} |
| 204 |
aws_byte_buf_append_dynamic(&tester->request_authorization_header, &auth_header_value); |
| 205 |
|
| 206 |
struct aws_string *signature = NULL; |
| 207 |
aws_signing_result_get_property(result, g_aws_signature_property_name, &signature); |
| 208 |
|
| 209 |
struct aws_byte_cursor signature_cursor = aws_byte_cursor_from_string(signature); |
| 210 |
aws_byte_buf_append_dynamic(&tester->last_signature, &signature_cursor); |
| 211 |
} |
| 212 |
|
| 213 |
static void s_on_event_signing_complete(struct aws_signing_result *result, int error_code, void *userdata) { |
| 214 |
(void)error_code; |
| 215 |
|
| 216 |
struct event_signing_tester *tester = userdata; |
| 217 |
|
| 218 |
tester->last_signature.len = 0; |
| 219 |
|
| 220 |
struct aws_string *signature = NULL; |
| 221 |
aws_signing_result_get_property(result, g_aws_signature_property_name, &signature); |
| 222 |
|
| 223 |
struct aws_byte_cursor signature_cursor = aws_byte_cursor_from_string(signature); |
| 224 |
aws_byte_buf_append_dynamic(&tester->last_signature, &signature_cursor); |
| 225 |
} |
| 226 |
|
| 227 |
AWS_STATIC_STRING_FROM_LITERAL( |
| 228 |
s_expected_request_authorization_header, |
| 229 |
"AWS4-HMAC-SHA256 Credential=access/19810116/us-east-1/demo/aws4_request, " |
| 230 |
"SignedHeaders=content-encoding;host;x-amz-content-sha256;x-amz-date, " |
| 231 |
"Signature=e1d8e8c8815e60969f2a34765c9a15945ffc0badbaa4b7e3b163ea19131e949b"); |
| 232 |
AWS_STATIC_STRING_FROM_LITERAL( |
| 233 |
s_expected_request_signature, |
| 234 |
"e1d8e8c8815e60969f2a34765c9a15945ffc0badbaa4b7e3b163ea19131e949b"); |
| 235 |
AWS_STATIC_STRING_FROM_LITERAL( |
| 236 |
s_expected_first_chunk_signature, |
| 237 |
"7aabf85b765e6a4d0d500b6e968657b14726fa3e1eb7e839302728ffd77629a5"); |
| 238 |
AWS_STATIC_STRING_FROM_LITERAL( |
| 239 |
s_expected_second_chunk_signature, |
| 240 |
"f72aa9642f571d24a6e1ae42f10f073ad9448d8a028b6bcd82da081335adda02"); |
| 241 |
AWS_STATIC_STRING_FROM_LITERAL( |
| 242 |
s_expected_third_chunk_signature, |
| 243 |
"632af120435b57ec241d8bfbb12e496dfd5e2730a1a02ac0ab6eaa230ae02e9a"); |
| 244 |
AWS_STATIC_STRING_FROM_LITERAL( |
| 245 |
s_expected_final_chunk_signature, |
| 246 |
"c6f679ddb3af68f5e82f0cf6761244cb2338cf11e7d01a24130aea1b7c17e53e"); |
| 247 |
|
| 248 |
static int s_sigv4_event_signing_test(struct aws_allocator *allocator, void *ctx) { |
| 249 |
(void)ctx; |
| 250 |
|
| 251 |
aws_auth_library_init(allocator); |
| 252 |
|
| 253 |
struct event_signing_tester tester; |
| 254 |
AWS_ZERO_STRUCT(tester); |
| 255 |
ASSERT_SUCCESS(s_event_signing_tester_init(allocator, &tester)); |
| 256 |
|
| 257 |
|
| 258 |
ASSERT_SUCCESS(aws_sign_request_aws( |
| 259 |
allocator, |
| 260 |
tester.request_signable, |
| 261 |
(void *)&tester.request_signing_config, |
| 262 |
s_on_request_signing_complete, |
| 263 |
&tester)); |
| 264 |
|
| 265 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 266 |
s_expected_request_authorization_header->bytes, |
| 267 |
s_expected_request_authorization_header->len, |
| 268 |
tester.request_authorization_header.buffer, |
| 269 |
tester.request_authorization_header.len); |
| 270 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 271 |
s_expected_request_signature->bytes, |
| 272 |
s_expected_request_signature->len, |
| 273 |
tester.last_signature.buffer, |
| 274 |
tester.last_signature.len); |
| 275 |
|
| 276 |
|
| 277 |
struct aws_signable *first_chunk_signable = |
| 278 |
aws_signable_new_chunk(allocator, tester.chunk1_stream, aws_byte_cursor_from_buf(&tester.last_signature)); |
| 279 |
struct aws_byte_cursor event_test_date_cursor = aws_byte_cursor_from_string(s_event_test_date); |
| 280 |
ASSERT_SUCCESS(aws_date_time_init_from_str_cursor( |
| 281 |
&tester.event_signing_config.date, &event_test_date_cursor, AWS_DATE_FORMAT_RFC822)); |
| 282 |
ASSERT_SUCCESS(aws_sign_request_aws( |
| 283 |
allocator, first_chunk_signable, (void *)&tester.event_signing_config, s_on_event_signing_complete, &tester)); |
| 284 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 285 |
s_expected_first_chunk_signature->bytes, |
| 286 |
s_expected_first_chunk_signature->len, |
| 287 |
tester.last_signature.buffer, |
| 288 |
tester.last_signature.len); |
| 289 |
aws_signable_destroy(first_chunk_signable); |
| 290 |
|
| 291 |
|
| 292 |
struct aws_signable *second_chunk_signable = |
| 293 |
aws_signable_new_chunk(allocator, tester.chunk2_stream, aws_byte_cursor_from_buf(&tester.last_signature)); |
| 294 |
event_test_date_cursor = aws_byte_cursor_from_string(s_event_test_date_2); |
| 295 |
ASSERT_SUCCESS(aws_date_time_init_from_str_cursor( |
| 296 |
&tester.event_signing_config.date, &event_test_date_cursor, AWS_DATE_FORMAT_RFC822)); |
| 297 |
ASSERT_SUCCESS(aws_sign_request_aws( |
| 298 |
allocator, second_chunk_signable, (void *)&tester.event_signing_config, s_on_event_signing_complete, &tester)); |
| 299 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 300 |
s_expected_second_chunk_signature->bytes, |
| 301 |
s_expected_second_chunk_signature->len, |
| 302 |
tester.last_signature.buffer, |
| 303 |
tester.last_signature.len); |
| 304 |
aws_signable_destroy(second_chunk_signable); |
| 305 |
|
| 306 |
|
| 307 |
struct aws_signable *third_chunk_signable = |
| 308 |
aws_signable_new_chunk(allocator, tester.chunk3_stream, aws_byte_cursor_from_buf(&tester.last_signature)); |
| 309 |
event_test_date_cursor = aws_byte_cursor_from_string(s_event_test_date_3); |
| 310 |
ASSERT_SUCCESS(aws_date_time_init_from_str_cursor( |
| 311 |
&tester.event_signing_config.date, &event_test_date_cursor, AWS_DATE_FORMAT_RFC822)); |
| 312 |
ASSERT_SUCCESS(aws_sign_request_aws( |
| 313 |
allocator, third_chunk_signable, (void *)&tester.event_signing_config, s_on_event_signing_complete, &tester)); |
| 314 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 315 |
s_expected_third_chunk_signature->bytes, |
| 316 |
s_expected_third_chunk_signature->len, |
| 317 |
tester.last_signature.buffer, |
| 318 |
tester.last_signature.len); |
| 319 |
aws_signable_destroy(third_chunk_signable); |
| 320 |
|
| 321 |
|
| 322 |
struct aws_signable *final_chunk_signable = |
| 323 |
aws_signable_new_chunk(allocator, NULL, aws_byte_cursor_from_buf(&tester.last_signature)); |
| 324 |
event_test_date_cursor = aws_byte_cursor_from_string(s_event_test_date_4); |
| 325 |
ASSERT_SUCCESS(aws_date_time_init_from_str_cursor( |
| 326 |
&tester.event_signing_config.date, &event_test_date_cursor, AWS_DATE_FORMAT_RFC822)); |
| 327 |
ASSERT_SUCCESS(aws_sign_request_aws( |
| 328 |
allocator, final_chunk_signable, (void *)&tester.event_signing_config, s_on_event_signing_complete, &tester)); |
| 329 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 330 |
s_expected_final_chunk_signature->bytes, |
| 331 |
s_expected_final_chunk_signature->len, |
| 332 |
tester.last_signature.buffer, |
| 333 |
tester.last_signature.len); |
| 334 |
aws_signable_destroy(final_chunk_signable); |
| 335 |
|
| 336 |
s_event_signing_tester_cleanup(&tester); |
| 337 |
|
| 338 |
aws_auth_library_clean_up(); |
| 339 |
|
| 340 |
return AWS_OP_SUCCESS; |
| 341 |
} |
| 342 |
|
| 343 |
AWS_TEST_CASE(sigv4_event_signing_test, s_sigv4_event_signing_test); |