-
Notifications
You must be signed in to change notification settings - Fork 28
/
aws_signing.c
2669 lines (2160 loc) · 91.6 KB
/
aws_signing.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#include <aws/auth/private/aws_signing.h>
#include <aws/auth/credentials.h>
#include <aws/auth/private/key_derivation.h>
#include <aws/auth/signable.h>
#include <aws/auth/signing.h>
#include <aws/cal/ecc.h>
#include <aws/cal/hash.h>
#include <aws/cal/hmac.h>
#include <aws/common/date_time.h>
#include <aws/common/encoding.h>
#include <aws/common/string.h>
#include <aws/io/stream.h>
#include <aws/io/uri.h>
#include <ctype.h>
#include <inttypes.h>
#if defined(_MSC_VER)
# pragma warning(disable : 4204)
#endif /* _MSC_VER */
/*
* A bunch of initial size values for various buffers used throughout the signing process
*
* We want them to be sufficient-but-not-wasting-significant-amounts-of-memory for "most"
* requests. The body read buffer is an exception since it will just be holding windows rather than
* the entire thing.
*/
#define BODY_READ_BUFFER_SIZE 4096
#define CANONICAL_REQUEST_STARTING_SIZE 1024
#define STRING_TO_SIGN_STARTING_SIZE 256
#define SIGNED_HEADERS_STARTING_SIZE 256
#define CANONICAL_HEADER_BLOCK_STARTING_SIZE 1024
#define AUTHORIZATION_VALUE_STARTING_SIZE 512
#define PAYLOAD_HASH_STARTING_SIZE (AWS_SHA256_LEN * 2)
#define CREDENTIAL_SCOPE_STARTING_SIZE 128
#define ACCESS_CREDENTIAL_SCOPE_STARTING_SIZE 149
#define SCRATCH_BUF_STARTING_SIZE 256
#define MAX_AUTHORIZATION_HEADER_COUNT 4
#define MAX_AUTHORIZATION_QUERY_PARAM_COUNT 6
#define DEFAULT_PATH_COMPONENT_COUNT 10
#define CANONICAL_REQUEST_SPLIT_OVER_ESTIMATE 20
#define HEX_ENCODED_SIGNATURE_OVER_ESTIMATE 256
#define MAX_ECDSA_P256_SIGNATURE_AS_BINARY_LENGTH 72
#define MAX_ECDSA_P256_SIGNATURE_AS_HEX_LENGTH (MAX_ECDSA_P256_SIGNATURE_AS_BINARY_LENGTH * 2)
#define AWS_SIGV4A_SIGNATURE_PADDING_BYTE ('*')
AWS_STRING_FROM_LITERAL(g_aws_signing_content_header_name, "x-amz-content-sha256");
AWS_STRING_FROM_LITERAL(g_aws_signing_authorization_header_name, "Authorization");
AWS_STRING_FROM_LITERAL(g_aws_signing_authorization_query_param_name, "X-Amz-Signature");
AWS_STRING_FROM_LITERAL(g_aws_signing_algorithm_query_param_name, "X-Amz-Algorithm");
AWS_STRING_FROM_LITERAL(g_aws_signing_credential_query_param_name, "X-Amz-Credential");
AWS_STRING_FROM_LITERAL(g_aws_signing_date_name, "X-Amz-Date");
AWS_STRING_FROM_LITERAL(g_aws_signing_signed_headers_query_param_name, "X-Amz-SignedHeaders");
AWS_STRING_FROM_LITERAL(g_aws_signing_security_token_name, "X-Amz-Security-Token");
AWS_STRING_FROM_LITERAL(g_aws_signing_expires_query_param_name, "X-Amz-Expires");
AWS_STRING_FROM_LITERAL(g_aws_signing_region_set_name, "X-Amz-Region-Set");
AWS_STATIC_STRING_FROM_LITERAL(s_signature_type_sigv4_http_request, "AWS4-HMAC-SHA256");
AWS_STATIC_STRING_FROM_LITERAL(s_signature_type_sigv4_s3_chunked_payload, "AWS4-HMAC-SHA256-PAYLOAD");
AWS_STATIC_STRING_FROM_LITERAL(s_signature_type_sigv4a_s3_chunked_payload, "AWS4-ECDSA-P256-SHA256-PAYLOAD");
AWS_STATIC_STRING_FROM_LITERAL(s_signature_type_sigv4_s3_chunked_trailer_payload, "AWS4-HMAC-SHA256-TRAILER");
AWS_STATIC_STRING_FROM_LITERAL(s_signature_type_sigv4a_s3_chunked_trailer_payload, "AWS4-ECDSA-P256-SHA256-TRAILER");
/* aws-related query param and header tables */
static struct aws_hash_table s_forbidden_headers;
static struct aws_hash_table s_forbidden_params;
static struct aws_hash_table s_skipped_headers;
static struct aws_byte_cursor s_amzn_trace_id_header_name;
static struct aws_byte_cursor s_user_agent_header_name;
static struct aws_byte_cursor s_connection_header_name;
static struct aws_byte_cursor s_sec_websocket_key_header_name;
static struct aws_byte_cursor s_sec_websocket_protocol_header_name;
static struct aws_byte_cursor s_sec_websocket_version_header_name;
static struct aws_byte_cursor s_upgrade_header_name;
static struct aws_byte_cursor s_amz_content_sha256_header_name;
static struct aws_byte_cursor s_amz_date_header_name;
static struct aws_byte_cursor s_authorization_header_name;
static struct aws_byte_cursor s_region_set_header_name;
static struct aws_byte_cursor s_amz_security_token_header_name;
static struct aws_byte_cursor s_amz_signature_param_name;
static struct aws_byte_cursor s_amz_date_param_name;
static struct aws_byte_cursor s_amz_credential_param_name;
static struct aws_byte_cursor s_amz_algorithm_param_name;
static struct aws_byte_cursor s_amz_signed_headers_param_name;
static struct aws_byte_cursor s_amz_security_token_param_name;
static struct aws_byte_cursor s_amz_expires_param_name;
static struct aws_byte_cursor s_amz_region_set_param_name;
/*
* Build a set of library-static tables for quick lookup.
*
* Construction errors are considered fatal.
*/
int aws_signing_init_signing_tables(struct aws_allocator *allocator) {
if (aws_hash_table_init(
&s_skipped_headers,
allocator,
10,
aws_hash_byte_cursor_ptr_ignore_case,
(aws_hash_callback_eq_fn *)aws_byte_cursor_eq_ignore_case,
NULL,
NULL)) {
return AWS_OP_ERR;
}
s_amzn_trace_id_header_name = aws_byte_cursor_from_c_str("x-amzn-trace-id");
if (aws_hash_table_put(&s_skipped_headers, &s_amzn_trace_id_header_name, NULL, NULL)) {
return AWS_OP_ERR;
}
s_user_agent_header_name = aws_byte_cursor_from_c_str("User-Agent");
if (aws_hash_table_put(&s_skipped_headers, &s_user_agent_header_name, NULL, NULL)) {
return AWS_OP_ERR;
}
s_connection_header_name = aws_byte_cursor_from_c_str("connection");
if (aws_hash_table_put(&s_skipped_headers, &s_connection_header_name, NULL, NULL)) {
return AWS_OP_ERR;
}
s_connection_header_name = aws_byte_cursor_from_c_str("expect");
if (aws_hash_table_put(&s_skipped_headers, &s_connection_header_name, NULL, NULL)) {
return AWS_OP_ERR;
}
s_sec_websocket_key_header_name = aws_byte_cursor_from_c_str("sec-websocket-key");
if (aws_hash_table_put(&s_skipped_headers, &s_sec_websocket_key_header_name, NULL, NULL)) {
return AWS_OP_ERR;
}
s_sec_websocket_protocol_header_name = aws_byte_cursor_from_c_str("sec-websocket-protocol");
if (aws_hash_table_put(&s_skipped_headers, &s_sec_websocket_protocol_header_name, NULL, NULL)) {
return AWS_OP_ERR;
}
s_sec_websocket_version_header_name = aws_byte_cursor_from_c_str("sec-websocket-version");
if (aws_hash_table_put(&s_skipped_headers, &s_sec_websocket_version_header_name, NULL, NULL)) {
return AWS_OP_ERR;
}
s_upgrade_header_name = aws_byte_cursor_from_c_str("upgrade");
if (aws_hash_table_put(&s_skipped_headers, &s_upgrade_header_name, NULL, NULL)) {
return AWS_OP_ERR;
}
if (aws_hash_table_init(
&s_forbidden_headers,
allocator,
10,
aws_hash_byte_cursor_ptr_ignore_case,
(aws_hash_callback_eq_fn *)aws_byte_cursor_eq_ignore_case,
NULL,
NULL)) {
return AWS_OP_ERR;
}
s_amz_content_sha256_header_name = aws_byte_cursor_from_string(g_aws_signing_content_header_name);
if (aws_hash_table_put(&s_forbidden_headers, &s_amz_content_sha256_header_name, NULL, NULL)) {
return AWS_OP_ERR;
}
s_amz_date_header_name = aws_byte_cursor_from_string(g_aws_signing_date_name);
if (aws_hash_table_put(&s_forbidden_headers, &s_amz_date_header_name, NULL, NULL)) {
return AWS_OP_ERR;
}
s_authorization_header_name = aws_byte_cursor_from_string(g_aws_signing_authorization_header_name);
if (aws_hash_table_put(&s_forbidden_headers, &s_authorization_header_name, NULL, NULL)) {
return AWS_OP_ERR;
}
s_region_set_header_name = aws_byte_cursor_from_string(g_aws_signing_region_set_name);
if (aws_hash_table_put(&s_forbidden_headers, &s_region_set_header_name, NULL, NULL)) {
return AWS_OP_ERR;
}
s_amz_security_token_header_name = aws_byte_cursor_from_string(g_aws_signing_security_token_name);
if (aws_hash_table_put(&s_forbidden_headers, &s_amz_security_token_header_name, NULL, NULL)) {
return AWS_OP_ERR;
}
if (aws_hash_table_init(
&s_forbidden_params,
allocator,
10,
aws_hash_byte_cursor_ptr_ignore_case,
(aws_hash_callback_eq_fn *)aws_byte_cursor_eq_ignore_case,
NULL,
NULL)) {
return AWS_OP_ERR;
}
s_amz_signature_param_name = aws_byte_cursor_from_string(g_aws_signing_authorization_query_param_name);
if (aws_hash_table_put(&s_forbidden_params, &s_amz_signature_param_name, NULL, NULL)) {
return AWS_OP_ERR;
}
s_amz_date_param_name = aws_byte_cursor_from_string(g_aws_signing_date_name);
if (aws_hash_table_put(&s_forbidden_params, &s_amz_date_param_name, NULL, NULL)) {
return AWS_OP_ERR;
}
s_amz_credential_param_name = aws_byte_cursor_from_string(g_aws_signing_credential_query_param_name);
if (aws_hash_table_put(&s_forbidden_params, &s_amz_credential_param_name, NULL, NULL)) {
return AWS_OP_ERR;
}
s_amz_algorithm_param_name = aws_byte_cursor_from_string(g_aws_signing_algorithm_query_param_name);
if (aws_hash_table_put(&s_forbidden_params, &s_amz_algorithm_param_name, NULL, NULL)) {
return AWS_OP_ERR;
}
s_amz_signed_headers_param_name = aws_byte_cursor_from_string(g_aws_signing_signed_headers_query_param_name);
if (aws_hash_table_put(&s_forbidden_params, &s_amz_signed_headers_param_name, NULL, NULL)) {
return AWS_OP_ERR;
}
s_amz_security_token_param_name = aws_byte_cursor_from_string(g_aws_signing_security_token_name);
if (aws_hash_table_put(&s_forbidden_params, &s_amz_security_token_param_name, NULL, NULL)) {
return AWS_OP_ERR;
}
s_amz_expires_param_name = aws_byte_cursor_from_string(g_aws_signing_expires_query_param_name);
if (aws_hash_table_put(&s_forbidden_params, &s_amz_expires_param_name, NULL, NULL)) {
return AWS_OP_ERR;
}
s_amz_region_set_param_name = aws_byte_cursor_from_string(g_aws_signing_region_set_name);
if (aws_hash_table_put(&s_forbidden_params, &s_amz_region_set_param_name, NULL, NULL)) {
return AWS_OP_ERR;
}
return AWS_OP_SUCCESS;
}
void aws_signing_clean_up_signing_tables(void) {
aws_hash_table_clean_up(&s_skipped_headers);
aws_hash_table_clean_up(&s_forbidden_headers);
aws_hash_table_clean_up(&s_forbidden_params);
}
static bool s_is_header_based_signature_value(enum aws_signature_type signature_type) {
switch (signature_type) {
case AWS_ST_HTTP_REQUEST_HEADERS:
case AWS_ST_CANONICAL_REQUEST_HEADERS:
return true;
default:
return false;
}
}
static bool s_is_query_param_based_signature_value(enum aws_signature_type signature_type) {
switch (signature_type) {
case AWS_ST_HTTP_REQUEST_QUERY_PARAMS:
case AWS_ST_CANONICAL_REQUEST_QUERY_PARAMS:
return true;
default:
return false;
}
}
static int s_get_signature_type_cursor(struct aws_signing_state_aws *state, struct aws_byte_cursor *cursor) {
switch (state->config.signature_type) {
case AWS_ST_HTTP_REQUEST_HEADERS:
case AWS_ST_HTTP_REQUEST_QUERY_PARAMS:
case AWS_ST_CANONICAL_REQUEST_HEADERS:
case AWS_ST_CANONICAL_REQUEST_QUERY_PARAMS:
if (state->config.algorithm == AWS_SIGNING_ALGORITHM_V4) {
*cursor = aws_byte_cursor_from_string(s_signature_type_sigv4_http_request);
} else {
*cursor = aws_byte_cursor_from_string(g_signature_type_sigv4a_http_request);
}
break;
case AWS_ST_HTTP_REQUEST_CHUNK:
case AWS_ST_HTTP_REQUEST_EVENT:
if (state->config.algorithm == AWS_SIGNING_ALGORITHM_V4) {
*cursor = aws_byte_cursor_from_string(s_signature_type_sigv4_s3_chunked_payload);
} else {
*cursor = aws_byte_cursor_from_string(s_signature_type_sigv4a_s3_chunked_payload);
}
break;
case AWS_ST_HTTP_REQUEST_TRAILING_HEADERS:
if (state->config.algorithm == AWS_SIGNING_ALGORITHM_V4) {
*cursor = aws_byte_cursor_from_string(s_signature_type_sigv4_s3_chunked_trailer_payload);
} else {
*cursor = aws_byte_cursor_from_string(s_signature_type_sigv4a_s3_chunked_trailer_payload);
}
break;
default:
return aws_raise_error(AWS_AUTH_SIGNING_UNSUPPORTED_SIGNATURE_TYPE);
}
return AWS_OP_SUCCESS;
}
static int s_append_sts_signature_type(struct aws_signing_state_aws *state, struct aws_byte_buf *dest) {
struct aws_byte_cursor algorithm_cursor;
if (s_get_signature_type_cursor(state, &algorithm_cursor)) {
return AWS_OP_ERR;
}
return aws_byte_buf_append_dynamic(dest, &algorithm_cursor);
}
/*
* signing state management
*/
struct aws_signing_state_aws *aws_signing_state_new(
struct aws_allocator *allocator,
const struct aws_signing_config_aws *config,
const struct aws_signable *signable,
aws_signing_complete_fn *on_complete,
void *userdata) {
if (aws_validate_aws_signing_config_aws(config)) {
return NULL;
}
struct aws_signing_state_aws *state = aws_mem_calloc(allocator, 1, sizeof(struct aws_signing_state_aws));
if (!state) {
return NULL;
}
state->allocator = allocator;
/* Make our own copy of the signing config */
state->config = *config;
if (state->config.credentials_provider != NULL) {
aws_credentials_provider_acquire(state->config.credentials_provider);
}
if (state->config.credentials != NULL) {
aws_credentials_acquire(state->config.credentials);
}
if (aws_byte_buf_init_cache_and_update_cursors(
&state->config_string_buffer,
allocator,
&state->config.region,
&state->config.service,
&state->config.signed_body_value,
NULL /*end*/)) {
goto on_error;
}
state->signable = signable;
state->on_complete = on_complete;
state->userdata = userdata;
if (aws_signing_result_init(&state->result, allocator)) {
goto on_error;
}
if (aws_byte_buf_init(&state->canonical_request, allocator, CANONICAL_REQUEST_STARTING_SIZE) ||
aws_byte_buf_init(&state->string_to_sign, allocator, STRING_TO_SIGN_STARTING_SIZE) ||
aws_byte_buf_init(&state->signed_headers, allocator, SIGNED_HEADERS_STARTING_SIZE) ||
aws_byte_buf_init(&state->canonical_header_block, allocator, CANONICAL_HEADER_BLOCK_STARTING_SIZE) ||
aws_byte_buf_init(&state->payload_hash, allocator, PAYLOAD_HASH_STARTING_SIZE) ||
aws_byte_buf_init(&state->credential_scope, allocator, CREDENTIAL_SCOPE_STARTING_SIZE) ||
aws_byte_buf_init(&state->access_credential_scope, allocator, ACCESS_CREDENTIAL_SCOPE_STARTING_SIZE) ||
aws_byte_buf_init(&state->date, allocator, AWS_DATE_TIME_STR_MAX_LEN) ||
aws_byte_buf_init(&state->signature, allocator, PAYLOAD_HASH_STARTING_SIZE) ||
aws_byte_buf_init(&state->string_to_sign_payload, allocator, PAYLOAD_HASH_STARTING_SIZE) ||
aws_byte_buf_init(&state->scratch_buf, allocator, SCRATCH_BUF_STARTING_SIZE)) {
goto on_error;
}
snprintf(
state->expiration_array, AWS_ARRAY_SIZE(state->expiration_array), "%" PRIu64 "", config->expiration_in_seconds);
return state;
on_error:
aws_signing_state_destroy(state);
return NULL;
}
void aws_signing_state_destroy(struct aws_signing_state_aws *state) {
aws_signing_result_clean_up(&state->result);
aws_credentials_provider_release(state->config.credentials_provider);
aws_credentials_release(state->config.credentials);
aws_byte_buf_clean_up(&state->config_string_buffer);
aws_byte_buf_clean_up(&state->canonical_request);
aws_byte_buf_clean_up(&state->string_to_sign);
aws_byte_buf_clean_up(&state->signed_headers);
aws_byte_buf_clean_up(&state->canonical_header_block);
aws_byte_buf_clean_up(&state->payload_hash);
aws_byte_buf_clean_up(&state->credential_scope);
aws_byte_buf_clean_up(&state->access_credential_scope);
aws_byte_buf_clean_up(&state->date);
aws_byte_buf_clean_up(&state->signature);
aws_byte_buf_clean_up(&state->string_to_sign_payload);
aws_byte_buf_clean_up(&state->scratch_buf);
aws_mem_release(state->allocator, state);
}
/*
* canonical request utility functions:
*
* various appends, conversion/encoding, etc...
*
*/
static int s_append_canonical_method(struct aws_signing_state_aws *state) {
const struct aws_signable *signable = state->signable;
struct aws_byte_buf *buffer = &state->canonical_request;
struct aws_byte_cursor method_cursor;
aws_signable_get_property(signable, g_aws_http_method_property_name, &method_cursor);
if (aws_byte_buf_append_dynamic(buffer, &method_cursor)) {
return AWS_OP_ERR;
}
if (aws_byte_buf_append_byte_dynamic(buffer, '\n')) {
return AWS_OP_ERR;
}
return AWS_OP_SUCCESS;
}
static int s_append_with_lookup(
struct aws_byte_buf *dst,
const struct aws_byte_cursor *src,
const uint8_t *lookup_table) {
if (aws_byte_buf_reserve_relative(dst, src->len)) {
return AWS_OP_ERR;
}
if (aws_byte_buf_append_with_lookup(dst, src, lookup_table)) {
return AWS_OP_ERR;
}
return AWS_OP_SUCCESS;
}
/*
* A function that builds a normalized path (removes redundant '/' characters, '.' components, and properly pops off
* components in response '..' components)
*
* We use a simple algorithm to do this:
*
* First split the path into components
* Then, using a secondary stack of components, build the final path by pushing and popping (on '..') components
* on the stack. The final path is then the concatenation of the secondary stack.
*/
static int s_append_normalized_path(
const struct aws_byte_cursor *raw_path,
struct aws_allocator *allocator,
struct aws_byte_buf *dest) {
struct aws_array_list raw_split;
AWS_ZERO_STRUCT(raw_split);
struct aws_array_list normalized_split;
AWS_ZERO_STRUCT(normalized_split);
int result = AWS_OP_ERR;
if (aws_array_list_init_dynamic(
&raw_split, allocator, DEFAULT_PATH_COMPONENT_COUNT, sizeof(struct aws_byte_cursor))) {
goto cleanup;
}
if (aws_byte_cursor_split_on_char(raw_path, '/', &raw_split)) {
goto cleanup;
}
const size_t raw_split_count = aws_array_list_length(&raw_split);
if (aws_array_list_init_dynamic(&normalized_split, allocator, raw_split_count, sizeof(struct aws_byte_cursor))) {
goto cleanup;
}
/*
* Iterate the raw split to build a list of path components that make up the
* normalized path
*/
for (size_t i = 0; i < raw_split_count; ++i) {
struct aws_byte_cursor path_component;
AWS_ZERO_STRUCT(path_component);
if (aws_array_list_get_at(&raw_split, &path_component, i)) {
goto cleanup;
}
if (path_component.len == 0 || (path_component.len == 1 && *path_component.ptr == '.')) {
/* '.' and '' contribute nothing to a normalized path */
continue;
}
if (path_component.len == 2 && *path_component.ptr == '.' && *(path_component.ptr + 1) == '.') {
/* '..' causes us to remove the last valid path component */
aws_array_list_pop_back(&normalized_split);
} else {
aws_array_list_push_back(&normalized_split, &path_component);
}
}
/*
* Special case preserve whether or not the path ended with a '/'
*/
bool ends_with_slash = raw_path->len > 0 && raw_path->ptr[raw_path->len - 1] == '/';
/*
* Paths always start with a single '/'
*/
if (aws_byte_buf_append_byte_dynamic(dest, '/')) {
goto cleanup;
}
/*
* build the final normalized path from the normalized split by joining
* the components together with '/'
*/
const size_t normalized_split_count = aws_array_list_length(&normalized_split);
for (size_t i = 0; i < normalized_split_count; ++i) {
struct aws_byte_cursor normalized_path_component;
AWS_ZERO_STRUCT(normalized_path_component);
if (aws_array_list_get_at(&normalized_split, &normalized_path_component, i)) {
goto cleanup;
}
if (aws_byte_buf_append_dynamic(dest, &normalized_path_component)) {
goto cleanup;
}
if (i + 1 < normalized_split_count || ends_with_slash) {
if (aws_byte_buf_append_byte_dynamic(dest, '/')) {
goto cleanup;
}
}
}
result = AWS_OP_SUCCESS;
cleanup:
aws_array_list_clean_up(&raw_split);
aws_array_list_clean_up(&normalized_split);
return result;
}
static int s_append_canonical_path(const struct aws_uri *uri, struct aws_signing_state_aws *state) {
const struct aws_signing_config_aws *config = &state->config;
struct aws_byte_buf *canonical_request_buffer = &state->canonical_request;
struct aws_allocator *allocator = state->allocator;
int result = AWS_OP_ERR;
/*
* Put this at function global scope so that it gets cleaned up even though it's only used inside
* a single branch. Allows error handling and cleanup to follow the pattern established
* throughout this file.
*/
struct aws_byte_buf normalized_path;
AWS_ZERO_STRUCT(normalized_path);
/*
* We assume the request's uri path has already been encoded once (in order to go out on the wire).
* Some services do not decode the path before performing the sig v4 calculation, resulting in the
* service actually performing sigv4 on a double-encoding of the path. In order to match those
* services, we must double encode in our calculation as well.
*/
if (config->flags.use_double_uri_encode) {
struct aws_byte_cursor path_cursor;
/*
* We need to transform the the normalized path, so we can't just append it into the canonical
* request. Instead we append it into a temporary buffer and perform the transformation from
* it.
*
* All this does is skip the temporary normalized path in the case where we don't need to
* double encode.
*/
if (config->flags.should_normalize_uri_path) {
if (aws_byte_buf_init(&normalized_path, state->allocator, uri->path.len)) {
goto cleanup;
}
if (s_append_normalized_path(&uri->path, allocator, &normalized_path)) {
goto cleanup;
}
path_cursor = aws_byte_cursor_from_buf(&normalized_path);
} else {
path_cursor = uri->path;
}
if (aws_byte_buf_append_encoding_uri_path(canonical_request_buffer, &path_cursor)) {
goto cleanup;
}
} else {
/*
* If we don't need to perform any kind of transformation on the normalized path, just append it directly
* into the canonical request buffer
*/
if (config->flags.should_normalize_uri_path) {
if (s_append_normalized_path(&uri->path, allocator, canonical_request_buffer)) {
goto cleanup;
}
} else {
if (aws_byte_buf_append_dynamic(canonical_request_buffer, &uri->path)) {
goto cleanup;
}
}
}
if (aws_byte_buf_append_byte_dynamic(canonical_request_buffer, '\n')) {
goto cleanup;
}
result = AWS_OP_SUCCESS;
cleanup:
aws_byte_buf_clean_up(&normalized_path);
return result;
}
/*
* URI-encoded query params are compared first by key, then by value
*/
int s_canonical_query_param_comparator(const void *lhs, const void *rhs) {
const struct aws_uri_param *left_param = lhs;
const struct aws_uri_param *right_param = rhs;
int key_compare = aws_byte_cursor_compare_lexical(&left_param->key, &right_param->key);
if (key_compare != 0) {
return key_compare;
}
return aws_byte_cursor_compare_lexical(&left_param->value, &right_param->value);
}
/*
* We need to sort the headers in a stable fashion, but the default sorting methods available in the c library are not
* guaranteed to be stable. We can make the sort stable by instead sorting a wrapper object that includes the original
* index of the wrapped object and using that index to break lexical ties.
*
* We sort a copy of the header (rather than pointers) so that we can easily inject secondary headers into
* the canonical request.
*/
struct stable_header {
struct aws_signable_property_list_pair header;
size_t original_index;
};
int s_canonical_header_comparator(const void *lhs, const void *rhs) {
const struct stable_header *left_header = lhs;
const struct stable_header *right_header = rhs;
int result = aws_byte_cursor_compare_lookup(
&left_header->header.name, &right_header->header.name, aws_lookup_table_to_lower_get());
if (result != 0) {
return result;
}
/* they're the same header, use the original index to keep the sort stable */
if (left_header->original_index < right_header->original_index) {
return -1;
}
/* equality should never happen */
AWS_ASSERT(left_header->original_index > right_header->original_index);
return 1;
}
/**
* Given URI-encoded query param, write it to canonical buffer.
*/
static int s_append_canonical_query_param(struct aws_uri_param *encoded_param, struct aws_byte_buf *buffer) {
if (aws_byte_buf_append_dynamic(buffer, &encoded_param->key)) {
return AWS_OP_ERR;
}
if (aws_byte_buf_append_byte_dynamic(buffer, '=')) {
return AWS_OP_ERR;
}
if (aws_byte_buf_append_dynamic(buffer, &encoded_param->value)) {
return AWS_OP_ERR;
}
return AWS_OP_SUCCESS;
}
/**
* Given unencoded authorization query param:
* Add it, URI-encoded to final signing result (to be added to signable later).
*/
static int s_add_query_param_to_signing_result(
struct aws_signing_state_aws *state,
const struct aws_uri_param *unencoded_param) {
/* URI-Encode, and add to final signing result */
state->scratch_buf.len = 0;
if (aws_byte_buf_append_encoding_uri_param(&state->scratch_buf, &unencoded_param->key)) {
return AWS_OP_ERR;
}
size_t key_len = state->scratch_buf.len;
if (aws_byte_buf_append_encoding_uri_param(&state->scratch_buf, &unencoded_param->value)) {
return AWS_OP_ERR;
}
struct aws_byte_cursor encoded_val = aws_byte_cursor_from_buf(&state->scratch_buf);
struct aws_byte_cursor encoded_key = aws_byte_cursor_advance(&encoded_val, key_len);
if (aws_signing_result_append_property_list(
&state->result, g_aws_http_query_params_property_list_name, &encoded_key, &encoded_val)) {
return AWS_OP_ERR;
}
return AWS_OP_SUCCESS;
}
/**
* Given unencoded authorization query param:
* 1) Add it to list of all unencoded query params (to be canonicalized later).
* 2) Add it, URI-encoded to final signing result (to be added to signable later).
*/
static int s_add_authorization_query_param(
struct aws_signing_state_aws *state,
struct aws_array_list *unencoded_auth_params,
const struct aws_uri_param *unencoded_auth_param) {
/* Add to unencoded list */
if (aws_array_list_push_back(unencoded_auth_params, unencoded_auth_param)) {
return AWS_OP_ERR;
}
return s_add_query_param_to_signing_result(state, unencoded_auth_param);
}
/*
* Checks the header against both an internal skip list as well as an optional user-supplied filter
* function. Only sign the header if both functions allow it.
*/
static bool s_should_sign_header(struct aws_signing_state_aws *state, struct aws_byte_cursor *name) {
if (state->config.should_sign_header) {
if (!state->config.should_sign_header(name, state->config.should_sign_header_ud)) {
return false;
}
}
struct aws_hash_element *element = NULL;
if (aws_hash_table_find(&s_skipped_headers, name, &element) == AWS_OP_ERR || element != NULL) {
return false;
}
return true;
}
/*
* If the auth type was query param then this function adds all the required query params and values with the
* exception of X-Amz-Signature (because we're still computing its value) Parameters are added to both the
* canonical request and the final signing result.
*/
static int s_add_authorization_query_params(
struct aws_signing_state_aws *state,
struct aws_array_list *unencoded_query_params) {
if (state->config.signature_type != AWS_ST_HTTP_REQUEST_QUERY_PARAMS) {
return AWS_OP_SUCCESS;
}
int result = AWS_OP_ERR;
/* X-Amz-Algorithm */
struct aws_uri_param algorithm_param = {
.key = aws_byte_cursor_from_string(g_aws_signing_algorithm_query_param_name),
};
if (s_get_signature_type_cursor(state, &algorithm_param.value)) {
goto done;
}
if (s_add_authorization_query_param(state, unencoded_query_params, &algorithm_param)) {
goto done;
}
/* X-Amz-Credential */
struct aws_uri_param credential_param = {
.key = aws_byte_cursor_from_string(g_aws_signing_credential_query_param_name),
.value = aws_byte_cursor_from_buf(&state->access_credential_scope),
};
if (s_add_authorization_query_param(state, unencoded_query_params, &credential_param)) {
goto done;
}
/* X-Amz-Date */
struct aws_uri_param date_param = {
.key = aws_byte_cursor_from_string(g_aws_signing_date_name),
.value = aws_byte_cursor_from_buf(&state->date),
};
if (s_add_authorization_query_param(state, unencoded_query_params, &date_param)) {
goto done;
}
/* X-Amz-SignedHeaders */
struct aws_uri_param signed_headers_param = {
.key = aws_byte_cursor_from_string(g_aws_signing_signed_headers_query_param_name),
.value = aws_byte_cursor_from_buf(&state->signed_headers),
};
if (s_add_authorization_query_param(state, unencoded_query_params, &signed_headers_param)) {
goto done;
}
/* X-Amz-Expires */
uint64_t expiration_in_seconds = state->config.expiration_in_seconds;
if (expiration_in_seconds > 0) {
struct aws_uri_param expires_param = {
.key = aws_byte_cursor_from_string(g_aws_signing_expires_query_param_name),
.value = aws_byte_cursor_from_c_str(state->expiration_array),
};
if (s_add_authorization_query_param(state, unencoded_query_params, &expires_param)) {
goto done;
}
}
/* X-Amz-Security-token */
struct aws_byte_cursor security_token_name_cur = aws_byte_cursor_from_string(g_aws_signing_security_token_name);
struct aws_byte_cursor session_token_cursor = aws_credentials_get_session_token(state->config.credentials);
if (session_token_cursor.len > 0) {
struct aws_uri_param security_token_param = {
.key = security_token_name_cur,
.value = session_token_cursor,
};
/* If omit_session_token is true, then security token is added to the
* final signing result, but is treated as "unsigned" and does not
* contribute to the authorization signature */
if (state->config.flags.omit_session_token) {
if (s_add_query_param_to_signing_result(state, &security_token_param)) {
goto done;
}
} else {
if (s_add_authorization_query_param(state, unencoded_query_params, &security_token_param)) {
goto done;
}
}
}
/* X-Amz-Region-Set */
if (state->config.algorithm == AWS_SIGNING_ALGORITHM_V4_ASYMMETRIC) {
struct aws_uri_param region_set_param = {
.key = aws_byte_cursor_from_string(g_aws_signing_region_set_name),
.value = state->config.region,
};
if (s_add_authorization_query_param(state, unencoded_query_params, ®ion_set_param)) {
goto done;
}
}
/* NOTE: Update MAX_AUTHORIZATION_QUERY_PARAM_COUNT if more params added */
result = AWS_OP_SUCCESS;
done:
return result;
}
static int s_validate_query_params(struct aws_array_list *unencoded_query_params) {
const size_t param_count = aws_array_list_length(unencoded_query_params);
for (size_t i = 0; i < param_count; ++i) {
struct aws_uri_param param;
AWS_ZERO_STRUCT(param);
aws_array_list_get_at(unencoded_query_params, ¶m, i);
struct aws_hash_element *forbidden_element = NULL;
aws_hash_table_find(&s_forbidden_params, ¶m.key, &forbidden_element);
if (forbidden_element != NULL) {
AWS_LOGF_ERROR(
AWS_LS_AUTH_SIGNING,
"AWS authorization query param \"" PRInSTR "\" found in request while signing",
AWS_BYTE_CURSOR_PRI(param.key));
return aws_raise_error(AWS_AUTH_SIGNING_ILLEGAL_REQUEST_QUERY_PARAM);
}
}
return AWS_OP_SUCCESS;
}
/**
* Apply or remove URI-encoding to each aws_uri_param in a list.
* (new strings are added to temp_strings)
* Append function must grow buffer if necessary.
*/
static int s_transform_query_params(
struct aws_signing_state_aws *state,
struct aws_array_list *param_list,
struct aws_array_list *temp_strings,
int (*byte_buf_append_dynamic_param_fn)(struct aws_byte_buf *, const struct aws_byte_cursor *)) {
const size_t param_count = aws_array_list_length(param_list);
struct aws_uri_param *param = NULL;
for (size_t i = 0; i < param_count; ++i) {
aws_array_list_get_at_ptr(param_list, (void **)¶m, i);
/* encode/decode key and save string */
state->scratch_buf.len = 0;
if (byte_buf_append_dynamic_param_fn(&state->scratch_buf, ¶m->key)) {
return AWS_OP_ERR;
}
struct aws_string *key_str = aws_string_new_from_buf(state->allocator, &state->scratch_buf);
if (!key_str) {
return AWS_OP_ERR;
}
if (aws_array_list_push_back(temp_strings, &key_str)) {
aws_string_destroy(key_str);
return AWS_OP_ERR;
}
/* encode/decode value and save string */
state->scratch_buf.len = 0;
if (byte_buf_append_dynamic_param_fn(&state->scratch_buf, ¶m->value)) {
return AWS_OP_ERR;
}
struct aws_string *value_str = aws_string_new_from_buf(state->allocator, &state->scratch_buf);
if (!value_str) {
return AWS_OP_ERR;
}
if (aws_array_list_push_back(temp_strings, &value_str)) {
aws_string_destroy(value_str);
return AWS_OP_ERR;
}
/* save encoded/decoded param */
param->key = aws_byte_cursor_from_string(key_str);
param->value = aws_byte_cursor_from_string(value_str);
}
return AWS_OP_SUCCESS;
}
/*
* Adds the full canonical query string to the canonical request.
* Note that aws-c-auth takes query params from the URI, so they should already be URI-encoded.
* To ensure that the signature uses "canonical" URI-encoding, we decode and then re-encode the params.
*/
static int s_append_canonical_query_string(struct aws_uri *uri, struct aws_signing_state_aws *state) {
struct aws_allocator *allocator = state->allocator;
struct aws_byte_buf *canonical_request_buffer = &state->canonical_request;
int result = AWS_OP_ERR;
struct aws_array_list query_params;
AWS_ZERO_STRUCT(query_params);
struct aws_array_list temp_strings;
AWS_ZERO_STRUCT(temp_strings);
/* Determine max number of query parameters.
* If none, skip to end of function */
size_t max_param_count = 0;
struct aws_uri_param param_i;
AWS_ZERO_STRUCT(param_i);
while (aws_uri_query_string_next_param(uri, ¶m_i)) {
++max_param_count;
}
if (state->config.signature_type == AWS_ST_HTTP_REQUEST_QUERY_PARAMS) {
max_param_count += MAX_AUTHORIZATION_QUERY_PARAM_COUNT;
}
if (max_param_count == 0) {
goto finish;
}
/* Allocate storage for mutable list of query params */
if (aws_array_list_init_dynamic(&query_params, allocator, max_param_count, sizeof(struct aws_uri_param))) {
goto cleanup;
}
/* Allocate storage for both the decoded, and re-encoded, key and value strings */
if (aws_array_list_init_dynamic(
&temp_strings, state->allocator, max_param_count * 4, sizeof(struct aws_string *))) {
goto cleanup;
}