Debian Codegraph @

crt/aws-c-auth/tests/test_chunked_signing.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
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 /*
27 * The chunked signing test is built using the complete chunked signing example in the s3 docs:
28 * https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-streaming.html
29 *
30 */
31
32 AWS_STATIC_STRING_FROM_LITERAL(s_integration_chunked_access_key_id, "example");
33 AWS_STATIC_STRING_FROM_LITERAL(s_integration_chunked_secret_access_key, "example");
34
35 AWS_STATIC_STRING_FROM_LITERAL(s_chunked_access_key_id, "AKIAIOSFODNN7EXAMPLE");
36 AWS_STATIC_STRING_FROM_LITERAL(s_chunked_secret_access_key, "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY");
37 AWS_STATIC_STRING_FROM_LITERAL(s_chunked_test_region, "us-east-1");
38 AWS_STATIC_STRING_FROM_LITERAL(s_chunked_test_service, "s3");
39 AWS_STATIC_STRING_FROM_LITERAL(s_chunked_test_date, "Fri, 24 May 2013 00:00:00 GMT");
40
41 static struct aws_http_header s_host_header = {
42 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("host"),
43 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("s3.amazonaws.com"),
44 };
45
46 static struct aws_http_header s_integration_host_header = {
47 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("host"),
48 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("example_bucket"),
49 };
50
51 static struct aws_http_header s_storage_class_header = {
52 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-storage-class"),
53 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("REDUCED_REDUNDANCY"),
54 };
55
56 static struct aws_http_header s_content_encoding_header = {
57 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Encoding"),
58 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("aws-chunked"),
59 };
60
61 static struct aws_http_header s_decoded_length_header = {
62 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-decoded-content-length"),
63 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("66560"),
64 };
65
66 static struct aws_http_header s_integration_decoded_length_header = {
67 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-decoded-content-length"),
68 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("1"),
69 };
70
71 static struct aws_http_header s_content_length_header = {
72 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Length"),
73 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("66824"),
74 };
75 static struct aws_http_header s_trailer_header = {
76 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-trailer"),
77 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("first,second,third"),
78 };
79
80 static struct aws_http_header s_integration_content_length_header = {
81 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Length"),
82 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("535"),
83 };
84
85 static struct aws_http_header s_integration_trailer_header = {
86 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-trailer"),
87 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-checksum-crc32c"),
88 };
89
90 AWS_STATIC_STRING_FROM_LITERAL(s_chunked_test_path, "/examplebucket/chunkObject.txt");
91 AWS_STATIC_STRING_FROM_LITERAL(s_integration_test_path, "/john_is_the_bees_knees_1_byte");
92
93 static struct aws_http_message *s_build_chunked_test_request(struct aws_allocator *allocator) {
94 struct aws_http_message *request = aws_http_message_new_request(allocator);
95
96 aws_http_message_add_header(request, s_host_header);
97 aws_http_message_add_header(request, s_storage_class_header);
98 aws_http_message_add_header(request, s_content_encoding_header);
99 aws_http_message_add_header(request, s_decoded_length_header);
100 aws_http_message_add_header(request, s_content_length_header);
101 aws_http_message_set_request_method(request, aws_http_method_put);
102 aws_http_message_set_request_path(request, aws_byte_cursor_from_string(s_chunked_test_path));
103
104 return request;
105 }
106
107 static struct aws_http_message *s_build_trailing_headers_test_request(struct aws_allocator *allocator) {
108 struct aws_http_message *request = aws_http_message_new_request(allocator);
109
110 aws_http_message_add_header(request, s_host_header);
111 aws_http_message_add_header(request, s_storage_class_header);
112 aws_http_message_add_header(request, s_content_encoding_header);
113 aws_http_message_add_header(request, s_decoded_length_header);
114 aws_http_message_add_header(request, s_content_length_header);
115 aws_http_message_add_header(request, s_trailer_header);
116 aws_http_message_set_request_method(request, aws_http_method_put);
117 aws_http_message_set_request_path(request, aws_byte_cursor_from_string(s_chunked_test_path));
118
119 return request;
120 }
121 static struct aws_http_message *s_build_integration_test_request(struct aws_allocator *allocator) {
122 struct aws_http_message *request = aws_http_message_new_request(allocator);
123
124 aws_http_message_add_header(request, s_content_encoding_header);
125 aws_http_message_add_header(request, s_integration_host_header);
126 aws_http_message_add_header(request, s_integration_decoded_length_header);
127 aws_http_message_add_header(request, s_integration_content_length_header);
128 aws_http_message_add_header(request, s_integration_trailer_header);
129 aws_http_message_set_request_method(request, aws_http_method_put);
130 aws_http_message_set_request_path(request, aws_byte_cursor_from_string(s_integration_test_path));
131
132 return request;
133 }
134
135 static int s_initialize_request_signing_config(
136 struct aws_signing_config_aws *config,
137 struct aws_credentials *credentials) {
138 config->config_type = AWS_SIGNING_CONFIG_AWS;
139 config->algorithm = AWS_SIGNING_ALGORITHM_V4;
140 config->signature_type = AWS_ST_HTTP_REQUEST_HEADERS;
141 config->region = aws_byte_cursor_from_string(s_chunked_test_region);
142 config->service = aws_byte_cursor_from_string(s_chunked_test_service);
143
144 struct aws_byte_cursor chunked_test_date_cursor = aws_byte_cursor_from_string(s_chunked_test_date);
145 if (aws_date_time_init_from_str_cursor(&config->date, &chunked_test_date_cursor, AWS_DATE_FORMAT_RFC822)) {
146 return AWS_OP_ERR;
147 }
148
149 config->flags.use_double_uri_encode = false;
150 config->flags.should_normalize_uri_path = true;
151 config->signed_body_value = g_aws_signed_body_value_streaming_aws4_hmac_sha256_payload;
152 config->signed_body_header = AWS_SBHT_X_AMZ_CONTENT_SHA256;
153 config->credentials = credentials;
154
155 return AWS_OP_SUCCESS;
156 }
157
158 static int s_initialize_chunk_signing_config(
159 struct aws_signing_config_aws *config,
160 struct aws_credentials *credentials) {
161 config->config_type = AWS_SIGNING_CONFIG_AWS;
162 config->algorithm = AWS_SIGNING_ALGORITHM_V4;
163 config->signature_type = AWS_ST_HTTP_REQUEST_CHUNK;
164 config->region = aws_byte_cursor_from_string(s_chunked_test_region);
165 config->service = aws_byte_cursor_from_string(s_chunked_test_service);
166
167 struct aws_byte_cursor chunked_test_date_cursor = aws_byte_cursor_from_string(s_chunked_test_date);
168 if (aws_date_time_init_from_str_cursor(&config->date, &chunked_test_date_cursor, AWS_DATE_FORMAT_RFC822)) {
169 return AWS_OP_ERR;
170 }
171
172 config->flags.use_double_uri_encode = false;
173 config->flags.should_normalize_uri_path = true;
174 config->signed_body_header = AWS_SBHT_NONE;
175 config->credentials = credentials;
176
177 return AWS_OP_SUCCESS;
178 }
179
180 static int s_initialize_trailing_headers_signing_config(
181 struct aws_signing_config_aws *config,
182 struct aws_credentials *credentials) {
183 config->config_type = AWS_SIGNING_CONFIG_AWS;
184 config->algorithm = AWS_SIGNING_ALGORITHM_V4;
185 config->signature_type = AWS_ST_HTTP_REQUEST_TRAILING_HEADERS;
186 config->region = aws_byte_cursor_from_string(s_chunked_test_region);
187 config->service = aws_byte_cursor_from_string(s_chunked_test_service);
188
189 struct aws_byte_cursor chunked_test_date_cursor = aws_byte_cursor_from_string(s_chunked_test_date);
190 if (aws_date_time_init_from_str_cursor(&config->date, &chunked_test_date_cursor, AWS_DATE_FORMAT_RFC822)) {
191 return AWS_OP_ERR;
192 }
193
194 config->flags.use_double_uri_encode = false;
195 config->flags.should_normalize_uri_path = true;
196 config->signed_body_header = AWS_SBHT_NONE;
197 config->credentials = credentials;
198
199 return AWS_OP_SUCCESS;
200 }
201
202 struct chunked_signing_tester {
203 struct aws_credentials *credentials;
204 struct aws_ecc_key_pair *verification_key;
205 struct aws_http_message *request;
206 struct aws_signable *request_signable;
207 struct aws_http_message *integration_request;
208 struct aws_signable *integration_request_signable;
209 struct aws_http_message *trailing_request;
210 struct aws_signable *trailing_request_signable;
211 struct aws_signing_config_aws request_signing_config;
212 struct aws_signing_config_aws chunk_signing_config;
213 struct aws_signing_config_aws trailing_headers_signing_config;
214 struct aws_byte_buf chunk1;
215 struct aws_byte_buf chunk2;
216 struct aws_byte_buf integration_chunk;
217 struct aws_input_stream *chunk1_stream;
218 struct aws_input_stream *chunk2_stream;
219 struct aws_input_stream *integration_chunk_stream;
220 struct aws_http_headers *trailing_headers;
221 struct aws_http_headers *integration_trailing_headers;
222 struct aws_mutex mutex;
223 bool request_completed;
224 struct aws_condition_variable c_var;
225
226 struct aws_byte_buf request_authorization_header;
227 struct aws_byte_buf last_signature;
228 };
229
230 #define CHUNK1_SIZE 65536
231 #define CHUNK2_SIZE 1024
232
233 AWS_STATIC_STRING_FROM_LITERAL(
234 s_chunked_test_ecc_pub_x,
235 "18b7d04643359f6ec270dcbab8dce6d169d66ddc9778c75cfb08dfdb701637ab");
236 AWS_STATIC_STRING_FROM_LITERAL(
237 s_chunked_test_ecc_pub_y,
238 "fa36b35e4fe67e3112261d2e17a956ef85b06e44712d2850bcd3c2161e9993f2");
239
240 static struct aws_http_headers *s_trailing_headers_new(struct aws_allocator *allocator) {
241 struct aws_http_headers *trailing_headers = aws_http_headers_new(allocator);
242 const struct aws_http_header trailer1 = {
243 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("first"),
244 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("1st"),
245 };
246 const struct aws_http_header trailer2 = {
247 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("second"),
248 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("2nd"),
249 };
250 const struct aws_http_header trailer3 = {
251 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("third"),
252 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("3rd"),
253 };
254 aws_http_headers_add_header(trailing_headers, &trailer1);
255 aws_http_headers_add_header(trailing_headers, &trailer2);
256 aws_http_headers_add_header(trailing_headers, &trailer3);
257 return trailing_headers;
258 }
259
260 static int s_chunked_signing_tester_init(struct aws_allocator *allocator, struct chunked_signing_tester *tester) {
261 tester->credentials = aws_credentials_new_from_string(
262 allocator, s_chunked_access_key_id, s_chunked_secret_access_key, NULL, UINT64_MAX);
263 tester->verification_key = aws_ecc_key_new_from_hex_coordinates(
264 allocator,
265 AWS_CAL_ECDSA_P256,
266 aws_byte_cursor_from_string(s_chunked_test_ecc_pub_x),
267 aws_byte_cursor_from_string(s_chunked_test_ecc_pub_y));
268
269 tester->request = s_build_chunked_test_request(allocator);
270 tester->request_signable = aws_signable_new_http_request(allocator, tester->request);
271
272 tester->trailing_request = s_build_trailing_headers_test_request(allocator);
273 tester->trailing_request_signable = aws_signable_new_http_request(allocator, tester->trailing_request);
274
275 tester->integration_request = s_build_integration_test_request(allocator);
276 tester->integration_request_signable = aws_signable_new_http_request(allocator, tester->integration_request);
277
278 AWS_ZERO_STRUCT(tester->request_signing_config);
279 ASSERT_SUCCESS(s_initialize_request_signing_config(&tester->request_signing_config, tester->credentials));
280 ASSERT_SUCCESS(s_initialize_chunk_signing_config(&tester->chunk_signing_config, tester->credentials));
281 ASSERT_SUCCESS(
282 s_initialize_trailing_headers_signing_config(&tester->trailing_headers_signing_config, tester->credentials));
283
284 ASSERT_SUCCESS(aws_byte_buf_init(&tester->request_authorization_header, allocator, 512));
285 ASSERT_SUCCESS(aws_byte_buf_init(&tester->last_signature, allocator, 128));
286
287 ASSERT_SUCCESS(aws_byte_buf_init(&tester->chunk1, allocator, CHUNK1_SIZE));
288 ASSERT_TRUE(aws_byte_buf_write_u8_n(&tester->chunk1, 'a', CHUNK1_SIZE));
289
290 ASSERT_SUCCESS(aws_byte_buf_init(&tester->chunk2, allocator, CHUNK2_SIZE));
291 ASSERT_TRUE(aws_byte_buf_write_u8_n(&tester->chunk2, 'a', CHUNK2_SIZE));
292
293 ASSERT_SUCCESS(aws_byte_buf_init(&tester->integration_chunk, allocator, 1));
294 ASSERT_TRUE(aws_byte_buf_write_u8_n(&tester->integration_chunk, 'a', 1));
295
296 tester->trailing_headers = s_trailing_headers_new(allocator);
297 tester->integration_trailing_headers = aws_http_headers_new(allocator);
298 const struct aws_http_header checksum = {
299 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-checksum-crc32c"),
300 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("wdBDMA=="),
301 };
302 aws_http_headers_add_header(tester->integration_trailing_headers, &checksum);
303
304 struct aws_byte_cursor chunk1_cursor = aws_byte_cursor_from_buf(&tester->chunk1);
305 tester->chunk1_stream = aws_input_stream_new_from_cursor(allocator, &chunk1_cursor);
306
307 struct aws_byte_cursor chunk2_cursor = aws_byte_cursor_from_buf(&tester->chunk2);
308 tester->chunk2_stream = aws_input_stream_new_from_cursor(allocator, &chunk2_cursor);
309
310 struct aws_byte_cursor integration_chunk_cursor = aws_byte_cursor_from_buf(&tester->integration_chunk);
311 tester->integration_chunk_stream = aws_input_stream_new_from_cursor(allocator, &integration_chunk_cursor);
312 aws_mutex_init(&tester->mutex);
313 tester->c_var = (struct aws_condition_variable)AWS_CONDITION_VARIABLE_INIT;
314 tester->request_completed = false;
315
316 return AWS_OP_SUCCESS;
317 }
318
319 static void s_chunked_signing_tester_cleanup(struct chunked_signing_tester *tester) {
320 aws_signable_destroy(tester->request_signable);
321 aws_http_message_release(tester->request);
322 aws_signable_destroy(tester->integration_request_signable);
323 aws_signable_destroy(tester->trailing_request_signable);
324 aws_http_message_release(tester->trailing_request);
325 aws_http_message_release(tester->integration_request);
326 aws_credentials_release(tester->credentials);
327 aws_ecc_key_pair_release(tester->verification_key);
328 aws_byte_buf_clean_up(&tester->request_authorization_header);
329 aws_byte_buf_clean_up(&tester->last_signature);
330 aws_byte_buf_clean_up(&tester->integration_chunk);
331 aws_byte_buf_clean_up(&tester->chunk1);
332 aws_byte_buf_clean_up(&tester->chunk2);
333 aws_http_headers_release(tester->integration_trailing_headers);
334 aws_http_headers_release(tester->trailing_headers);
335 aws_input_stream_destroy(tester->integration_chunk_stream);
336 aws_input_stream_destroy(tester->chunk1_stream);
337 aws_input_stream_destroy(tester->chunk2_stream);
338 aws_mutex_clean_up(&tester->mutex);
339 }
340
341 static void s_on_request_signing_complete(struct aws_signing_result *result, int error_code, void *userdata) {
342 (void)error_code;
343
344 struct chunked_signing_tester *tester = userdata;
345
346 struct aws_array_list *headers = NULL;
347 aws_signing_result_get_property_list(result, g_aws_http_headers_property_list_name, &headers);
348
349 struct aws_byte_cursor auth_header_name = aws_byte_cursor_from_string(g_aws_signing_authorization_header_name);
350 struct aws_byte_cursor auth_header_value;
351 AWS_ZERO_STRUCT(auth_header_value);
352 for (size_t i = 0; i < aws_array_list_length(headers); ++i) {
353 struct aws_signing_result_property pair;
354 AWS_ZERO_STRUCT(pair);
355 if (aws_array_list_get_at(headers, &pair, i)) {
356 continue;
357 }
358
359 if (pair.name == NULL) {
360 continue;
361 }
362
363 struct aws_byte_cursor pair_name_cursor = aws_byte_cursor_from_string(pair.name);
364 if (aws_byte_cursor_eq_ignore_case(&pair_name_cursor, &auth_header_name)) {
365 auth_header_value = aws_byte_cursor_from_string(pair.value);
366 break;
367 }
368 }
369 aws_byte_buf_append_dynamic(&tester->request_authorization_header, &auth_header_value);
370
371 struct aws_string *signature = NULL;
372 aws_signing_result_get_property(result, g_aws_signature_property_name, &signature);
373
374 struct aws_byte_cursor signature_cursor = aws_byte_cursor_from_string(signature);
375 aws_byte_buf_append_dynamic(&tester->last_signature, &signature_cursor);
376 }
377
378 static void s_on_integration_request_signing_complete(
379 struct aws_signing_result *result,
380 int error_code,
381 void *userdata) {
382 (void)error_code;
383
384 struct chunked_signing_tester *tester = userdata;
385
386 struct aws_array_list *headers = NULL;
387 aws_signing_result_get_property_list(result, g_aws_http_headers_property_list_name, &headers);
388
389 struct aws_byte_cursor auth_header_name = aws_byte_cursor_from_string(g_aws_signing_authorization_header_name);
390 struct aws_byte_cursor auth_header_value;
391 AWS_ZERO_STRUCT(auth_header_value);
392 for (size_t i = 0; i < aws_array_list_length(headers); ++i) {
393 struct aws_signing_result_property pair;
394 AWS_ZERO_STRUCT(pair);
395 if (aws_array_list_get_at(headers, &pair, i)) {
396 continue;
397 }
398
399 if (pair.name == NULL) {
400 continue;
401 }
402
403 struct aws_byte_cursor pair_name_cursor = aws_byte_cursor_from_string(pair.name);
404 if (aws_byte_cursor_eq_ignore_case(&pair_name_cursor, &auth_header_name)) {
405 auth_header_value = aws_byte_cursor_from_string(pair.value);
406 break;
407 }
408 }
409 aws_byte_buf_append_dynamic(&tester->request_authorization_header, &auth_header_value);
410
411 struct aws_string *signature = NULL;
412 aws_signing_result_get_property(result, g_aws_signature_property_name, &signature);
413
414 struct aws_byte_cursor signature_cursor = aws_byte_cursor_from_string(signature);
415 aws_byte_buf_append_dynamic(&tester->last_signature, &signature_cursor);
416
417 aws_apply_signing_result_to_http_request(tester->integration_request, aws_default_allocator(), result);
418 }
419
420 static void s_on_chunk_signing_complete(struct aws_signing_result *result, int error_code, void *userdata) {
421 (void)error_code;
422
423 struct chunked_signing_tester *tester = userdata;
424
425 tester->last_signature.len = 0;
426
427 struct aws_string *signature = NULL;
428 aws_signing_result_get_property(result, g_aws_signature_property_name, &signature);
429
430 struct aws_byte_cursor signature_cursor = aws_byte_cursor_from_string(signature);
431 aws_byte_buf_append_dynamic(&tester->last_signature, &signature_cursor);
432 }
433
434 /* There is an error in the s3 docs where they list the authorization header value: it is missing a space between
435 * the ',' and 'SignedHeaders=' as well as a space between the ',' and 'Signature='
436 */
437 AWS_STATIC_STRING_FROM_LITERAL(
438 s_expected_request_authorization_header,
439 "AWS4-HMAC-SHA256 Credential=AKIAIOSFODNN7EXAMPLE/20130524/us-east-1/s3/aws4_request, "
440 "SignedHeaders=content-encoding;content-length;host;x-amz-content-sha256;x-amz-date;x-amz-decoded-content-length;x-"
441 "amz-storage-class, Signature=4f232c4386841ef735655705268965c44a0e4690baa4adea153f7db9fa80a0a9");
442 AWS_STATIC_STRING_FROM_LITERAL(
443 s_expected_request_signature,
444 "4f232c4386841ef735655705268965c44a0e4690baa4adea153f7db9fa80a0a9");
445 AWS_STATIC_STRING_FROM_LITERAL(
446 s_expected_first_chunk_signature,
447 "ad80c730a21e5b8d04586a2213dd63b9a0e99e0e2307b0ade35a65485a288648");
448 AWS_STATIC_STRING_FROM_LITERAL(
449 s_expected_second_chunk_signature,
450 "0055627c9e194cb4542bae2aa5492e3c1575bbb81b612b7d234b86a503ef5497");
451 AWS_STATIC_STRING_FROM_LITERAL(
452 s_expected_final_chunk_signature,
453 "b6c6ea8a5354eaf15b3cb7646744f4275b71ea724fed81ceb9323e279d449df9");
454 AWS_STATIC_STRING_FROM_LITERAL(
455 s_expected_trailing_headers_signature,
456 "df5735bd9f3295cd9386572292562fefc93ba94e80a0a1ddcbd652c4e0a75e6c");
457
458 static int s_sigv4_chunked_signing_test(struct aws_allocator *allocator, void *ctx) {
459 (void)ctx;
460
461 aws_auth_library_init(allocator);
462
463 struct chunked_signing_tester tester;
464 AWS_ZERO_STRUCT(tester);
465 ASSERT_SUCCESS(s_chunked_signing_tester_init(allocator, &tester));
466
467 /* Sign the base request and check the signature and authorization header */
468 ASSERT_SUCCESS(aws_sign_request_aws(
469 allocator,
470 tester.request_signable,
471 (void *)&tester.request_signing_config,
472 s_on_request_signing_complete,
473 &tester));
474
475 ASSERT_BIN_ARRAYS_EQUALS(
476 s_expected_request_authorization_header->bytes,
477 s_expected_request_authorization_header->len,
478 tester.request_authorization_header.buffer,
479 tester.request_authorization_header.len);
480 ASSERT_BIN_ARRAYS_EQUALS(
481 s_expected_request_signature->bytes,
482 s_expected_request_signature->len,
483 tester.last_signature.buffer,
484 tester.last_signature.len);
485
486 /* Make and sign the first chunk */
487 struct aws_signable *first_chunk_signable =
488 aws_signable_new_chunk(allocator, tester.chunk1_stream, aws_byte_cursor_from_buf(&tester.last_signature));
489 ASSERT_SUCCESS(aws_sign_request_aws(
490 allocator, first_chunk_signable, (void *)&tester.chunk_signing_config, s_on_chunk_signing_complete, &tester));
491 ASSERT_BIN_ARRAYS_EQUALS(
492 s_expected_first_chunk_signature->bytes,
493 s_expected_first_chunk_signature->len,
494 tester.last_signature.buffer,
495 tester.last_signature.len);
496 aws_signable_destroy(first_chunk_signable);
497
498 /* Make and sign the second chunk */
499 struct aws_signable *second_chunk_signable =
500 aws_signable_new_chunk(allocator, tester.chunk2_stream, aws_byte_cursor_from_buf(&tester.last_signature));
501 ASSERT_SUCCESS(aws_sign_request_aws(
502 allocator, second_chunk_signable, (void *)&tester.chunk_signing_config, s_on_chunk_signing_complete, &tester));
503 ASSERT_BIN_ARRAYS_EQUALS(
504 s_expected_second_chunk_signature->bytes,
505 s_expected_second_chunk_signature->len,
506 tester.last_signature.buffer,
507 tester.last_signature.len);
508 aws_signable_destroy(second_chunk_signable);
509
510 /* Make and sign the final, empty chunk */
511 struct aws_signable *final_chunk_signable =
512 aws_signable_new_chunk(allocator, NULL, aws_byte_cursor_from_buf(&tester.last_signature));
513 ASSERT_SUCCESS(aws_sign_request_aws(
514 allocator, final_chunk_signable, (void *)&tester.chunk_signing_config, s_on_chunk_signing_complete, &tester));
515 ASSERT_BIN_ARRAYS_EQUALS(
516 s_expected_final_chunk_signature->bytes,
517 s_expected_final_chunk_signature->len,
518 tester.last_signature.buffer,
519 tester.last_signature.len);
520 aws_signable_destroy(final_chunk_signable);
521
522 s_chunked_signing_tester_cleanup(&tester);
523
524 aws_auth_library_clean_up();
525
526 return AWS_OP_SUCCESS;
527 }
528
529 AWS_TEST_CASE(sigv4_chunked_signing_test, s_sigv4_chunked_signing_test);
530
531 static int s_sigv4_trailing_headers_signing_test(struct aws_allocator *allocator, void *ctx) {
532 (void)ctx;
533
534 aws_auth_library_init(allocator);
535
536 struct chunked_signing_tester tester;
537 AWS_ZERO_STRUCT(tester);
538 ASSERT_SUCCESS(s_chunked_signing_tester_init(allocator, &tester));
539
540 /* Sign the base request and check the signature and authorization header */
541 ASSERT_SUCCESS(aws_sign_request_aws(
542 allocator,
543 tester.request_signable,
544 (void *)&tester.request_signing_config,
545 s_on_request_signing_complete,
546 &tester));
547
548 ASSERT_BIN_ARRAYS_EQUALS(
549 s_expected_request_authorization_header->bytes,
550 s_expected_request_authorization_header->len,
551 tester.request_authorization_header.buffer,
552 tester.request_authorization_header.len);
553 ASSERT_BIN_ARRAYS_EQUALS(
554 s_expected_request_signature->bytes,
555 s_expected_request_signature->len,
556 tester.last_signature.buffer,
557 tester.last_signature.len);
558
559 /* Make and sign the first chunk */
560 struct aws_signable *first_chunk_signable =
561 aws_signable_new_chunk(allocator, tester.chunk1_stream, aws_byte_cursor_from_buf(&tester.last_signature));
562 ASSERT_SUCCESS(aws_sign_request_aws(
563 allocator, first_chunk_signable, (void *)&tester.chunk_signing_config, s_on_chunk_signing_complete, &tester));
564 ASSERT_BIN_ARRAYS_EQUALS(
565 s_expected_first_chunk_signature->bytes,
566 s_expected_first_chunk_signature->len,
567 tester.last_signature.buffer,
568 tester.last_signature.len);
569 aws_signable_destroy(first_chunk_signable);
570
571 /* Make and sign the second chunk */
572 struct aws_signable *second_chunk_signable =
573 aws_signable_new_chunk(allocator, tester.chunk2_stream, aws_byte_cursor_from_buf(&tester.last_signature));
574 ASSERT_SUCCESS(aws_sign_request_aws(
575 allocator, second_chunk_signable, (void *)&tester.chunk_signing_config, s_on_chunk_signing_complete, &tester));
576 ASSERT_BIN_ARRAYS_EQUALS(
577 s_expected_second_chunk_signature->bytes,
578 s_expected_second_chunk_signature->len,
579 tester.last_signature.buffer,
580 tester.last_signature.len);
581 aws_signable_destroy(second_chunk_signable);
582
583 /* Make and sign the final, empty chunk */
584 struct aws_signable *final_chunk_signable =
585 aws_signable_new_chunk(allocator, NULL, aws_byte_cursor_from_buf(&tester.last_signature));
586 ASSERT_SUCCESS(aws_sign_request_aws(
587 allocator, final_chunk_signable, (void *)&tester.chunk_signing_config, s_on_chunk_signing_complete, &tester));
588 ASSERT_BIN_ARRAYS_EQUALS(
589 s_expected_final_chunk_signature->bytes,
590 s_expected_final_chunk_signature->len,
591 tester.last_signature.buffer,
592 tester.last_signature.len);
593 aws_signable_destroy(final_chunk_signable);
594
595 /* Make and sign the trailing headers */
596 struct aws_http_headers *trailing_headers = s_trailing_headers_new(allocator);
597 struct aws_signable *trailing_headers_signable = aws_signable_new_trailing_headers(
598 allocator, trailing_headers, aws_byte_cursor_from_buf(&tester.last_signature));
599 /* test aws_signable_new_trailing_headers properly acquires trailing_headers */
600 aws_http_headers_release(trailing_headers);
601 ASSERT_SUCCESS(aws_sign_request_aws(
602 allocator,
603 trailing_headers_signable,
604 (void *)&tester.trailing_headers_signing_config,
605 s_on_chunk_signing_complete,
606 &tester));
607 ASSERT_BIN_ARRAYS_EQUALS(
608 s_expected_trailing_headers_signature->bytes,
609 s_expected_trailing_headers_signature->len,
610 tester.last_signature.buffer,
611 tester.last_signature.len);
612 aws_signable_destroy(trailing_headers_signable);
613
614 s_chunked_signing_tester_cleanup(&tester);
615
616 aws_auth_library_clean_up();
617
618 return AWS_OP_SUCCESS;
619 }
620
621 AWS_TEST_CASE(sigv4_trailing_headers_signing_test, s_sigv4_trailing_headers_signing_test);
622
623 AWS_STATIC_STRING_FROM_LITERAL(
624 s_chunked_expected_canonical_request_cursor,
625 "PUT\n"
626 "/examplebucket/chunkObject.txt\n"
627 "\n"
628 "content-encoding:aws-chunked\n"
629 "content-length:66824\n"
630 "host:s3.amazonaws.com\n"
631 "x-amz-content-sha256:STREAMING-AWS4-ECDSA-P256-SHA256-PAYLOAD\n"
632 "x-amz-date:20130524T000000Z\n"
633 "x-amz-decoded-content-length:66560\n"
634 "x-amz-region-set:us-east-1\n"
635 "x-amz-storage-class:REDUCED_REDUNDANCY\n"
636 "\n"
637 "content-encoding;content-length;host;x-amz-content-sha256;x-amz-date;x-amz-decoded-content-length;x-amz-region-"
638 "set;x-amz-storage-class\n"
639 "STREAMING-AWS4-ECDSA-P256-SHA256-PAYLOAD");
640
641 AWS_STATIC_STRING_FROM_LITERAL(
642 s_chunked_expected_trailing_headers_canonical_request_cursor,
643 "PUT\n"
644 "/examplebucket/chunkObject.txt\n"
645 "\n"
646 "content-encoding:aws-chunked\n"
647 "content-length:66824\n"
648 "host:s3.amazonaws.com\n"
649 "x-amz-content-sha256:STREAMING-AWS4-ECDSA-P256-SHA256-PAYLOAD-TRAILER\n"
650 "x-amz-date:20130524T000000Z\n"
651 "x-amz-decoded-content-length:66560\n"
652 "x-amz-region-set:us-east-1\n"
653 "x-amz-storage-class:REDUCED_REDUNDANCY\n"
654 "x-amz-trailer:first,second,third\n"
655 "\n"
656 "content-encoding;content-length;host;x-amz-content-sha256;x-amz-date;x-amz-decoded-content-length;x-amz-region-"
657 "set;x-amz-storage-class;x-amz-trailer\n"
658 "STREAMING-AWS4-ECDSA-P256-SHA256-PAYLOAD-TRAILER");
659
660 AWS_STATIC_STRING_FROM_LITERAL(
661 s_chunk_sts_pre_signature,
662 "AWS4-ECDSA-P256-SHA256-PAYLOAD\n"
663 "20130524T000000Z\n"
664 "20130524/s3/aws4_request\n");
665
666 AWS_STATIC_STRING_FROM_LITERAL(
667 s_chunk1_sts_post_signature,
668 "\ne3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\n"
669 "bf718b6f653bebc184e1479f1935b8da974d701b893afcf49e701f3e2f9f9c5a");
670
671 AWS_STATIC_STRING_FROM_LITERAL(
672 s_chunk2_sts_post_signature,
673 "\ne3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\n"
674 "2edc986847e209b4016e141a6dc8716d3207350f416969382d431539bf292e4a");
675
676 AWS_STATIC_STRING_FROM_LITERAL(
677 s_chunk3_sts_post_signature,
678 "\ne3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\n"
679 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
680
681 AWS_STATIC_STRING_FROM_LITERAL(
682 s_trailing_headers_expected_sts_pre_signature,
683 "AWS4-ECDSA-P256-SHA256-TRAILER\n"
684 "20130524T000000Z\n"
685 "20130524/s3/aws4_request\n");
686 AWS_STATIC_STRING_FROM_LITERAL(
687 s_trailing_headers_expected_sts_post_signature,
688 "\n83d8f190334fb741bc8daf73c891689d320bd8017756bc730c540021ed48001f");
689
690 static int s_sigv4a_chunked_signing_test(struct aws_allocator *allocator, void *ctx) {
691 (void)ctx;
692
693 aws_auth_library_init(allocator);
694
695 struct chunked_signing_tester tester;
696 AWS_ZERO_STRUCT(tester);
697 ASSERT_SUCCESS(s_chunked_signing_tester_init(allocator, &tester));
698 tester.request_signing_config.algorithm = AWS_SIGNING_ALGORITHM_V4_ASYMMETRIC;
699 tester.request_signing_config.signed_body_value = g_aws_signed_body_value_streaming_aws4_ecdsa_p256_sha256_payload;
700 tester.chunk_signing_config.algorithm = AWS_SIGNING_ALGORITHM_V4_ASYMMETRIC;
701
702 /* Sign the base request */
703 ASSERT_SUCCESS(aws_sign_request_aws(
704 allocator,
705 tester.request_signable,
706 (void *)&tester.request_signing_config,
707 s_on_request_signing_complete,
708 &tester));
709
710 struct aws_byte_cursor signature_cursor =
711 aws_trim_padded_sigv4a_signature(aws_byte_cursor_from_buf(&tester.last_signature));
712
713 /*
714 * Validate the request signature
715 */
716 ASSERT_SUCCESS(aws_verify_sigv4a_signing(
717 allocator,
718 tester.request_signable,
719 (void *)&tester.request_signing_config,
720 aws_byte_cursor_from_string(s_chunked_expected_canonical_request_cursor),
721 signature_cursor,
722 aws_byte_cursor_from_string(s_chunked_test_ecc_pub_x),
723 aws_byte_cursor_from_string(s_chunked_test_ecc_pub_y)));
724
725 /* Manually build the first chunk string-to-sign since it's based on a signature that varies per run */
726 struct aws_byte_buf chunk_string_to_sign;
727 ASSERT_SUCCESS(aws_byte_buf_init(&chunk_string_to_sign, allocator, 512));
728 struct aws_byte_cursor chunk_sts_pre_signature = aws_byte_cursor_from_string(s_chunk_sts_pre_signature);
729 ASSERT_SUCCESS(aws_byte_buf_append(&chunk_string_to_sign, &chunk_sts_pre_signature));
730 ASSERT_SUCCESS(aws_byte_buf_append(&chunk_string_to_sign, &signature_cursor));
731 struct aws_byte_cursor chunk_sts_post_signature = aws_byte_cursor_from_string(s_chunk1_sts_post_signature);
732 ASSERT_SUCCESS(aws_byte_buf_append(&chunk_string_to_sign, &chunk_sts_post_signature));
733
734 /* Make and sign the first chunk */
735 struct aws_signable *first_chunk_signable =
736 aws_signable_new_chunk(allocator, tester.chunk1_stream, aws_byte_cursor_from_buf(&tester.last_signature));
737 ASSERT_SUCCESS(aws_sign_request_aws(
738 allocator, first_chunk_signable, (void *)&tester.chunk_signing_config, s_on_chunk_signing_complete, &tester));
739
740 struct aws_byte_cursor chunk_signature_cursor =
741 aws_trim_padded_sigv4a_signature(aws_byte_cursor_from_buf(&tester.last_signature));
742
743 /* Verify the first chunk's signature */
744 ASSERT_SUCCESS(aws_validate_v4a_authorization_value(
745 allocator, tester.verification_key, aws_byte_cursor_from_buf(&chunk_string_to_sign), chunk_signature_cursor));
746
747 aws_signable_destroy(first_chunk_signable);
748
749 /* Manually build the second chunk string-to-sign since it's based on a signature that varies per run */
750 chunk_string_to_sign.len = 0;
751 chunk_sts_pre_signature = aws_byte_cursor_from_string(s_chunk_sts_pre_signature);
752 ASSERT_SUCCESS(aws_byte_buf_append(&chunk_string_to_sign, &chunk_sts_pre_signature));
753 ASSERT_SUCCESS(aws_byte_buf_append(&chunk_string_to_sign, &chunk_signature_cursor));
754 chunk_sts_post_signature = aws_byte_cursor_from_string(s_chunk2_sts_post_signature);
755 ASSERT_SUCCESS(aws_byte_buf_append(&chunk_string_to_sign, &chunk_sts_post_signature));
756
757 /* Make and sign the second chunk */
758 struct aws_signable *second_chunk_signable =
759 aws_signable_new_chunk(allocator, tester.chunk2_stream, aws_byte_cursor_from_buf(&tester.last_signature));
760 ASSERT_SUCCESS(aws_sign_request_aws(
761 allocator, second_chunk_signable, (void *)&tester.chunk_signing_config, s_on_chunk_signing_complete, &tester));
762
763 chunk_signature_cursor = aws_trim_padded_sigv4a_signature(aws_byte_cursor_from_buf(&tester.last_signature));
764
765 /* Verify the second chunk's signature */
766 ASSERT_SUCCESS(aws_validate_v4a_authorization_value(
767 allocator, tester.verification_key, aws_byte_cursor_from_buf(&chunk_string_to_sign), chunk_signature_cursor));
768
769 aws_signable_destroy(second_chunk_signable);
770
771 /* Manually build the final chunk string-to-sign since it's based on a signature that varies per run */
772 chunk_string_to_sign.len = 0;
773 chunk_sts_pre_signature = aws_byte_cursor_from_string(s_chunk_sts_pre_signature);
774 ASSERT_SUCCESS(aws_byte_buf_append(&chunk_string_to_sign, &chunk_sts_pre_signature));
775 ASSERT_SUCCESS(aws_byte_buf_append(&chunk_string_to_sign, &chunk_signature_cursor));
776 chunk_sts_post_signature = aws_byte_cursor_from_string(s_chunk3_sts_post_signature);
777 ASSERT_SUCCESS(aws_byte_buf_append(&chunk_string_to_sign, &chunk_sts_post_signature));
778
779 /* Make and sign the final, empty chunk */
780 struct aws_signable *final_chunk_signable =
781 aws_signable_new_chunk(allocator, NULL, aws_byte_cursor_from_buf(&tester.last_signature));
782 ASSERT_SUCCESS(aws_sign_request_aws(
783 allocator, final_chunk_signable, (void *)&tester.chunk_signing_config, s_on_chunk_signing_complete, &tester));
784
785 chunk_signature_cursor = aws_trim_padded_sigv4a_signature(aws_byte_cursor_from_buf(&tester.last_signature));
786
787 /* Verify the final chunk's signature */
788 ASSERT_SUCCESS(aws_validate_v4a_authorization_value(
789 allocator, tester.verification_key, aws_byte_cursor_from_buf(&chunk_string_to_sign), chunk_signature_cursor));
790
791 aws_signable_destroy(final_chunk_signable);
792
793 aws_byte_buf_clean_up(&chunk_string_to_sign);
794
795 s_chunked_signing_tester_cleanup(&tester);
796
797 aws_auth_library_clean_up();
798
799 return AWS_OP_SUCCESS;
800 }
801
802 AWS_TEST_CASE(sigv4a_chunked_signing_test, s_sigv4a_chunked_signing_test);
803
804 static int s_sigv4a_trailing_headers_signing_test(struct aws_allocator *allocator, void *ctx) {
805 (void)ctx;
806
807 aws_auth_library_init(allocator);
808
809 struct chunked_signing_tester tester;
810 AWS_ZERO_STRUCT(tester);
811 ASSERT_SUCCESS(s_chunked_signing_tester_init(allocator, &tester));
812 tester.request_signing_config.algorithm = AWS_SIGNING_ALGORITHM_V4_ASYMMETRIC;
813 tester.request_signing_config.signed_body_value =
814 g_aws_signed_body_value_streaming_aws4_ecdsa_p256_sha256_payload_trailer;
815 tester.chunk_signing_config.algorithm = AWS_SIGNING_ALGORITHM_V4_ASYMMETRIC;
816 tester.trailing_headers_signing_config.algorithm = AWS_SIGNING_ALGORITHM_V4_ASYMMETRIC;
817
818 /* Sign the base request */
819 ASSERT_SUCCESS(aws_sign_request_aws(
820 allocator,
821 tester.trailing_request_signable,
822 (void *)&tester.request_signing_config,
823 s_on_request_signing_complete,
824 &tester));
825
826 struct aws_byte_cursor signature_cursor =
827 aws_trim_padded_sigv4a_signature(aws_byte_cursor_from_buf(&tester.last_signature));
828
829 /*
830 * Validate the request signature
831 */
832 ASSERT_SUCCESS(aws_verify_sigv4a_signing(
833 allocator,
834 tester.trailing_request_signable,
835 (void *)&tester.request_signing_config,
836 aws_byte_cursor_from_string(s_chunked_expected_trailing_headers_canonical_request_cursor),
837 signature_cursor,
838 aws_byte_cursor_from_string(s_chunked_test_ecc_pub_x),
839 aws_byte_cursor_from_string(s_chunked_test_ecc_pub_y)));
840
841 /* Manually build the first chunk string-to-sign since it's based on a signature that varies per run */
842 struct aws_byte_buf chunk_string_to_sign;
843 ASSERT_SUCCESS(aws_byte_buf_init(&chunk_string_to_sign, allocator, 512));
844 struct aws_byte_cursor chunk_sts_pre_signature = aws_byte_cursor_from_string(s_chunk_sts_pre_signature);
845 ASSERT_SUCCESS(aws_byte_buf_append(&chunk_string_to_sign, &chunk_sts_pre_signature));
846 ASSERT_SUCCESS(aws_byte_buf_append(&chunk_string_to_sign, &signature_cursor));
847 struct aws_byte_cursor chunk_sts_post_signature = aws_byte_cursor_from_string(s_chunk1_sts_post_signature);
848 ASSERT_SUCCESS(aws_byte_buf_append(&chunk_string_to_sign, &chunk_sts_post_signature));
849
850 /* Make and sign the first chunk */
851 struct aws_signable *first_chunk_signable =
852 aws_signable_new_chunk(allocator, tester.chunk1_stream, aws_byte_cursor_from_buf(&tester.last_signature));
853 ASSERT_SUCCESS(aws_sign_request_aws(
854 allocator, first_chunk_signable, (void *)&tester.chunk_signing_config, s_on_chunk_signing_complete, &tester));
855
856 struct aws_byte_cursor chunk_signature_cursor =
857 aws_trim_padded_sigv4a_signature(aws_byte_cursor_from_buf(&tester.last_signature));
858
859 /* Verify the first chunk's signature */
860 ASSERT_SUCCESS(aws_validate_v4a_authorization_value(
861 allocator, tester.verification_key, aws_byte_cursor_from_buf(&chunk_string_to_sign), chunk_signature_cursor));
862
863 aws_signable_destroy(first_chunk_signable);
864
865 /* Manually build the second chunk string-to-sign since it's based on a signature that varies per run */
866 chunk_string_to_sign.len = 0;
867 chunk_sts_pre_signature = aws_byte_cursor_from_string(s_chunk_sts_pre_signature);
868 ASSERT_SUCCESS(aws_byte_buf_append(&chunk_string_to_sign, &chunk_sts_pre_signature));
869 ASSERT_SUCCESS(aws_byte_buf_append(&chunk_string_to_sign, &chunk_signature_cursor));
870 chunk_sts_post_signature = aws_byte_cursor_from_string(s_chunk2_sts_post_signature);
871 ASSERT_SUCCESS(aws_byte_buf_append(&chunk_string_to_sign, &chunk_sts_post_signature));
872
873 /* Make and sign the second chunk */
874 struct aws_signable *second_chunk_signable =
875 aws_signable_new_chunk(allocator, tester.chunk2_stream, aws_byte_cursor_from_buf(&tester.last_signature));
876 ASSERT_SUCCESS(aws_sign_request_aws(
877 allocator, second_chunk_signable, (void *)&tester.chunk_signing_config, s_on_chunk_signing_complete, &tester));
878
879 chunk_signature_cursor = aws_trim_padded_sigv4a_signature(aws_byte_cursor_from_buf(&tester.last_signature));
880
881 /* Verify the second chunk's signature */
882 ASSERT_SUCCESS(aws_validate_v4a_authorization_value(
883 allocator, tester.verification_key, aws_byte_cursor_from_buf(&chunk_string_to_sign), chunk_signature_cursor));
884
885 aws_signable_destroy(second_chunk_signable);
886
887 /* Manually build the final chunk string-to-sign since it's based on a signature that varies per run */
888 chunk_string_to_sign.len = 0;
889 chunk_sts_pre_signature = aws_byte_cursor_from_string(s_chunk_sts_pre_signature);
890 ASSERT_SUCCESS(aws_byte_buf_append(&chunk_string_to_sign, &chunk_sts_pre_signature));
891 ASSERT_SUCCESS(aws_byte_buf_append(&chunk_string_to_sign, &chunk_signature_cursor));
892 chunk_sts_post_signature = aws_byte_cursor_from_string(s_chunk3_sts_post_signature);
893 ASSERT_SUCCESS(aws_byte_buf_append(&chunk_string_to_sign, &chunk_sts_post_signature));
894
895 /* Make and sign the final, empty chunk */
896 struct aws_signable *final_chunk_signable =
897 aws_signable_new_chunk(allocator, NULL, aws_byte_cursor_from_buf(&tester.last_signature));
898 ASSERT_SUCCESS(aws_sign_request_aws(
899 allocator, final_chunk_signable, (void *)&tester.chunk_signing_config, s_on_chunk_signing_complete, &tester));
900
901 chunk_signature_cursor = aws_trim_padded_sigv4a_signature(aws_byte_cursor_from_buf(&tester.last_signature));
902
903 /* Verify the final chunk's signature */
904 ASSERT_SUCCESS(aws_validate_v4a_authorization_value(
905 allocator, tester.verification_key, aws_byte_cursor_from_buf(&chunk_string_to_sign), chunk_signature_cursor));
906
907 aws_signable_destroy(final_chunk_signable);
908
909 chunk_string_to_sign.len = 0;
910 chunk_sts_pre_signature = aws_byte_cursor_from_string(s_trailing_headers_expected_sts_pre_signature);
911 ASSERT_SUCCESS(aws_byte_buf_append(&chunk_string_to_sign, &chunk_sts_pre_signature));
912 ASSERT_SUCCESS(aws_byte_buf_append(&chunk_string_to_sign, &chunk_signature_cursor));
913 chunk_sts_post_signature = aws_byte_cursor_from_string(s_trailing_headers_expected_sts_post_signature);
914 ASSERT_SUCCESS(aws_byte_buf_append(&chunk_string_to_sign, &chunk_sts_post_signature));
915
916 struct aws_signable *trailing_headers_signable = aws_signable_new_trailing_headers(
917 allocator, tester.trailing_headers, aws_byte_cursor_from_buf(&tester.last_signature));
918
919 ASSERT_SUCCESS(aws_sign_request_aws(
920 allocator,
921 trailing_headers_signable,
922 (void *)&tester.trailing_headers_signing_config,
923 s_on_chunk_signing_complete,
924 &tester));
925
926 struct aws_byte_cursor trailing_headers_signature_cursor =
927 aws_trim_padded_sigv4a_signature(aws_byte_cursor_from_buf(&tester.last_signature));
928
929 ASSERT_SUCCESS(aws_validate_v4a_authorization_value(
930 allocator,
931 tester.verification_key,
932 aws_byte_cursor_from_buf(&chunk_string_to_sign),
933 trailing_headers_signature_cursor));
934
935 aws_signable_destroy(trailing_headers_signable);
936
937 aws_byte_buf_clean_up(&chunk_string_to_sign);
938
939 s_chunked_signing_tester_cleanup(&tester);
940
941 aws_auth_library_clean_up();
942
943 return AWS_OP_SUCCESS;
944 }
945
946 AWS_TEST_CASE(sigv4a_trailing_headers_signing_test, s_sigv4a_trailing_headers_signing_test);
947
948 static int s_on_incoming_headers_fn(
949 struct aws_http_stream *stream,
950 enum aws_http_header_block header_block,
951 const struct aws_http_header *header_array,
952 size_t num_headers,
953 void *user_data) {
954
955 struct chunked_signing_tester *tester = user_data;
956 (void)tester;
957 (void)stream;
958
959 /* Ignore informational headers */
960 if (header_block == AWS_HTTP_HEADER_BLOCK_INFORMATIONAL) {
961 return AWS_OP_SUCCESS;
962 }
963
964 int status = 0;
965 aws_http_stream_get_incoming_response_status(stream, &status);
966 fprintf(stdout, "Response Status: %d\n", status);
967
968 for (size_t i = 0; i < num_headers; ++i) {
969 fwrite(header_array[i].name.ptr, 1, header_array[i].name.len, stdout);
970 fprintf(stdout, ": ");
971 fwrite(header_array[i].value.ptr, 1, header_array[i].value.len, stdout);
972 fprintf(stdout, "\n");
973 }
974
975 return AWS_OP_SUCCESS;
976 }
977
978 static int s_on_incoming_header_block_done_fn(
979 struct aws_http_stream *stream,
980 enum aws_http_header_block header_block,
981 void *user_data) {
982 (void)stream;
983 (void)header_block;
984 (void)user_data;
985
986 return AWS_OP_SUCCESS;
987 }
988
989 static void s_on_stream_complete_fn(struct aws_http_stream *stream, int error_code, void *user_data) {
990 (void)error_code;
991 (void)user_data;
992 aws_http_stream_release(stream);
993 }
994
995 static int s_on_incoming_body_fn(struct aws_http_stream *stream, const struct aws_byte_cursor *data, void *user_data) {
996
997 (void)stream;
998 (void)user_data;
999
1000 fwrite(data->ptr, 1, data->len, stdout);
1001
1002 return AWS_OP_SUCCESS;
1003 }
1004
1005 void s_log_headers(struct aws_http_message *request) {
1006 struct aws_http_header header;
1007 AWS_ZERO_STRUCT(header);
1008 for (size_t i = 0; i < aws_http_message_get_header_count(request); ++i) {
1009 aws_http_message_get_header(request, &header, i);
1010 fwrite(header.name.ptr, 1, header.name.len, stdout);
1011 fprintf(stdout, ": ");
1012 fwrite(header.value.ptr, 1, header.value.len, stdout);
1013 fprintf(stdout, "\n");
1014 }
1015 }
1016
1017 void s_send_request(struct aws_http_connection *connection, int error_code, void *user_data) {
1018 (void)error_code;
1019 struct chunked_signing_tester *tester = user_data;
1020 struct aws_http_make_request_options opt = {
1021 .self_size = sizeof(opt),
1022 .user_data = tester,
1023 .request = tester->integration_request,
1024 .on_response_headers = s_on_incoming_headers_fn,
1025 .on_response_header_block_done = s_on_incoming_header_block_done_fn,
1026 .on_response_body = s_on_incoming_body_fn,
1027 .on_complete = s_on_stream_complete_fn,
1028 };
1029
1030 struct aws_http_stream *stream = aws_http_connection_make_request(connection, &opt);
1031 aws_http_stream_activate(stream);
1032 }
1033
1034 static void s_on_client_connection_shutdown(struct aws_http_connection *connection, int error_code, void *user_data) {
1035 (void)error_code;
1036 (void)connection;
1037 struct chunked_signing_tester *tester = user_data;
1038
1039 aws_mutex_lock(&tester->mutex);
1040 tester->request_completed = true;
1041 aws_mutex_unlock(&tester->mutex);
1042 aws_condition_variable_notify_all(&tester->c_var);
1043 }
1044
1045 static bool s_completion_predicate(void *arg) {
1046 struct chunked_signing_tester *tester = arg;
1047 return tester->request_completed;
1048 }
1049
1050 static int s_sigv4a_trailing_header_integration_test(struct aws_allocator *allocator, void *ctx) {
1051 (void)ctx;
1052 aws_auth_library_init(allocator);
1053
1054 struct chunked_signing_tester tester;
1055 AWS_ZERO_STRUCT(tester);
1056 ASSERT_SUCCESS(s_chunked_signing_tester_init(allocator, &tester));
1057 tester.request_signing_config.algorithm = AWS_SIGNING_ALGORITHM_V4_ASYMMETRIC;
1058 tester.request_signing_config.signed_body_value =
1059 g_aws_signed_body_value_streaming_aws4_ecdsa_p256_sha256_payload_trailer;
1060 tester.chunk_signing_config.algorithm = AWS_SIGNING_ALGORITHM_V4_ASYMMETRIC;
1061 tester.trailing_headers_signing_config.algorithm = AWS_SIGNING_ALGORITHM_V4_ASYMMETRIC;
1062
1063 AWS_ZERO_STRUCT(tester.request_signing_config.date);
1064 AWS_ZERO_STRUCT(tester.chunk_signing_config.date);
1065 AWS_ZERO_STRUCT(tester.trailing_headers_signing_config.date);
1066 aws_date_time_init_now(&tester.request_signing_config.date);
1067 tester.chunk_signing_config.date = tester.request_signing_config.date;
1068 tester.trailing_headers_signing_config.date = tester.request_signing_config.date;
1069 // aws_date_time_init_now(&tester.chunk_signing_config.date);
1070 // aws_date_time_init_now(&tester.trailing_headers_signing_config.date);
1071
1072 struct aws_credentials *credentials = aws_credentials_new_from_string(
1073 allocator, s_integration_chunked_access_key_id, s_integration_chunked_secret_access_key, NULL, UINT64_MAX);
1074 tester.request_signing_config.credentials = credentials;
1075 tester.chunk_signing_config.credentials = credentials;
1076 tester.trailing_headers_signing_config.credentials = credentials;
1077
1078 ASSERT_SUCCESS(aws_sign_request_aws(
1079 allocator,
1080 tester.integration_request_signable,
1081 (void *)&tester.request_signing_config,
1082 s_on_integration_request_signing_complete,
1083 &tester));
1084
1085 struct aws_signable *first_chunk_signable = aws_signable_new_chunk(
1086 allocator, tester.integration_chunk_stream, aws_byte_cursor_from_buf(&tester.last_signature));
1087 ASSERT_SUCCESS(aws_sign_request_aws(
1088 allocator, first_chunk_signable, (void *)&tester.chunk_signing_config, s_on_chunk_signing_complete, &tester));
1089
1090 struct aws_byte_buf first_chunk_signature;
1091 AWS_ZERO_STRUCT(first_chunk_signature);
1092 aws_byte_buf_init_copy(&first_chunk_signature, aws_default_allocator(), &tester.last_signature);
1093
1094 /* Make and sign the final, empty chunk */
1095 struct aws_signable *final_chunk_signable =
1096 aws_signable_new_chunk(allocator, NULL, aws_byte_cursor_from_buf(&tester.last_signature));
1097 ASSERT_SUCCESS(aws_sign_request_aws(
1098 allocator, final_chunk_signable, (void *)&tester.chunk_signing_config, s_on_chunk_signing_complete, &tester));
1099
1100 struct aws_byte_buf final_chunk_signature;
1101 AWS_ZERO_STRUCT(final_chunk_signature);
1102 aws_byte_buf_init_copy(&final_chunk_signature, aws_default_allocator(), &tester.last_signature);
1103
1104 struct aws_signable *trailing_headers_signable = aws_signable_new_trailing_headers(
1105 allocator, tester.integration_trailing_headers, aws_byte_cursor_from_buf(&tester.last_signature));
1106
1107 ASSERT_SUCCESS(aws_sign_request_aws(
1108 allocator,
1109 trailing_headers_signable,
1110 (void *)&tester.trailing_headers_signing_config,
1111 s_on_chunk_signing_complete,
1112 &tester));
1113
1114 struct aws_byte_buf trailing_header_signature;
1115 AWS_ZERO_STRUCT(trailing_header_signature);
1116 aws_byte_buf_init_copy(&trailing_header_signature, aws_default_allocator(), &tester.last_signature);
1117
1118 struct aws_byte_cursor pre_chunk = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("1;chunk-signature=");
1119 struct aws_byte_cursor first_chunk = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\r\na\r\n0;chunk-signature=");
1120 struct aws_byte_cursor trailer_chunk =
1121 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\r\nx-amz-checksum-crc32c:wdBDMA==\r\nx-amz-trailer-signature:");
1122 struct aws_byte_cursor carriage_return = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\r\n\r\n");
1123 struct aws_byte_cursor first_chunk_signature_cursor = aws_byte_cursor_from_buf(&first_chunk_signature);
1124 struct aws_byte_cursor final_chunk_signature_cursor = aws_byte_cursor_from_buf(&final_chunk_signature);
1125 struct aws_byte_cursor trailing_header_signature_cursor = aws_byte_cursor_from_buf(&trailing_header_signature);
1126
1127 AWS_ASSERT(aws_byte_cursor_is_valid(&pre_chunk));
1128 AWS_ASSERT(aws_byte_cursor_is_valid(&first_chunk));
1129 AWS_ASSERT(aws_byte_cursor_is_valid(&trailer_chunk));
1130 AWS_ASSERT(aws_byte_cursor_is_valid(&carriage_return));
1131 AWS_ASSERT(aws_byte_cursor_is_valid(&first_chunk_signature_cursor));
1132 AWS_ASSERT(aws_byte_cursor_is_valid(&final_chunk_signature_cursor));
1133 AWS_ASSERT(aws_byte_cursor_is_valid(&trailing_header_signature_cursor));
1134
1135 size_t body_buf_len = pre_chunk.len + first_chunk.len + trailer_chunk.len + carriage_return.len +
1136 first_chunk_signature_cursor.len + final_chunk_signature_cursor.len +
1137 trailing_header_signature_cursor.len;
1138 struct aws_byte_buf body_buffer;
1139 aws_byte_buf_init(&body_buffer, aws_default_allocator(), body_buf_len);
1140 aws_byte_buf_append(&body_buffer, &pre_chunk);
1141 aws_byte_buf_append(&body_buffer, &first_chunk_signature_cursor);
1142 aws_byte_buf_append(&body_buffer, &first_chunk);
1143 aws_byte_buf_append(&body_buffer, &final_chunk_signature_cursor);
1144 aws_byte_buf_append(&body_buffer, &trailer_chunk);
1145 aws_byte_buf_append(&body_buffer, &trailing_header_signature_cursor);
1146 aws_byte_buf_append(&body_buffer, &carriage_return);
1147 struct aws_byte_cursor body_cursor = aws_byte_cursor_from_buf(&body_buffer);
1148 struct aws_input_stream *body_stream = aws_input_stream_new_from_cursor(aws_default_allocator(), &body_cursor);
1149 aws_http_message_set_body_stream(tester.integration_request, body_stream);
1150
1151 struct aws_http_client_connection_options client_options = AWS_HTTP_CLIENT_CONNECTION_OPTIONS_INIT;
1152 struct aws_byte_cursor host = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("example_bucket");
1153
1154 struct aws_socket_options socket_options = {
1155 .type = AWS_SOCKET_STREAM,
1156 .connect_timeout_ms = 3000,
1157 .keep_alive_timeout_sec = 0,
1158 .keepalive = false,
1159 .keep_alive_interval_sec = 0,
1160 };
1161
1162 struct aws_event_loop_group *el_group = aws_event_loop_group_new_default(allocator, 1, NULL);
1163
1164 struct aws_host_resolver_default_options resolver_options = {
1165 .el_group = el_group,
1166 .max_entries = 8,
1167 };
1168
1169 struct aws_host_resolver *resolver = aws_host_resolver_new_default(allocator, &resolver_options);
1170
1171 struct aws_client_bootstrap_options bootstrap_options = {
1172 .event_loop_group = el_group,
1173 .host_resolver = resolver,
1174 };
1175 struct aws_client_bootstrap *bootstrap = aws_client_bootstrap_new(allocator, &bootstrap_options);
1176
1177 client_options.on_setup = s_send_request;
1178 client_options.on_shutdown = s_on_client_connection_shutdown;
1179 client_options.user_data = &tester;
1180 client_options.allocator = aws_default_allocator();
1181 client_options.host_name = host;
1182 client_options.bootstrap = bootstrap;
1183 client_options.socket_options = &socket_options;
1184 client_options.port = 80;
1185
1186 AWS_LOGF_INFO(AWS_LS_AUTH_SIGNING, "Headers");
1187 s_log_headers(tester.integration_request);
1188 AWS_LOGF_INFO(AWS_LS_AUTH_SIGNING, "Request Body\n" PRInSTR "\n", AWS_BYTE_BUF_PRI(body_buffer));
1189
1190 aws_http_client_connect(&client_options);
1191 aws_mutex_lock(&tester.mutex);
1192 aws_condition_variable_wait_pred(&tester.c_var, &tester.mutex, s_completion_predicate, &tester);
1193 aws_mutex_unlock(&tester.mutex);
1194
1195 return AWS_OP_SUCCESS;
1196 }
1197 AWS_TEST_CASE(sigv4a_trailing_header_integration_test, s_sigv4a_trailing_header_integration_test);