@@ -352,8 +352,10 @@ struct aws_http_message *aws_s3_create_multipart_upload_message_new(
352352 return NULL ;
353353}
354354
355- /* Create a new put object request from an existing put object request. Currently just optionally adds part information
356- * for a multipart upload. */
355+ /**
356+ * Create a new put object request from an existing put object request. Currently just optionally adds part information
357+ * for a multipart upload.
358+ **/
357359struct aws_http_message * aws_s3_upload_part_message_new (
358360 struct aws_allocator * allocator ,
359361 struct aws_http_message * base_message ,
@@ -362,7 +364,7 @@ struct aws_http_message *aws_s3_upload_part_message_new(
362364 const struct aws_string * upload_id ,
363365 bool should_compute_content_md5 ,
364366 const struct checksum_config_storage * checksum_config ,
365- struct aws_byte_buf * encoded_checksum_output ) {
367+ struct aws_byte_buf * encoded_checksum ) {
366368 AWS_PRECONDITION (allocator );
367369 AWS_PRECONDITION (base_message );
368370 AWS_PRECONDITION (part_number > 0 );
@@ -383,7 +385,7 @@ struct aws_http_message *aws_s3_upload_part_message_new(
383385 goto error_clean_up ;
384386 }
385387
386- if (aws_s3_message_util_assign_body (allocator , buffer , message , checksum_config , encoded_checksum_output ) == NULL ) {
388+ if (aws_s3_message_util_assign_body (allocator , buffer , message , checksum_config , encoded_checksum ) == NULL ) {
387389 goto error_clean_up ;
388390 }
389391
@@ -836,19 +838,30 @@ static int s_calculate_in_memory_checksum_helper(
836838 struct aws_allocator * allocator ,
837839 struct aws_byte_cursor data ,
838840 const struct checksum_config_storage * checksum_config ,
839- struct aws_byte_buf * out_checksum ) {
841+ struct aws_byte_buf * checksum_buf ) {
840842 AWS_ASSERT (checksum_config -> checksum_algorithm != AWS_SCA_NONE );
841- AWS_ASSERT (out_checksum != NULL );
842- AWS_ZERO_STRUCT (* out_checksum );
843+ AWS_ASSERT (checksum_buf != NULL );
843844
844845 int ret_code = AWS_OP_ERR ;
845846 size_t digest_size = aws_get_digest_size_from_checksum_algorithm (checksum_config -> checksum_algorithm );
846847 size_t encoded_checksum_len = 0 ;
847848 if (aws_base64_compute_encoded_len (digest_size , & encoded_checksum_len )) {
848849 return AWS_OP_ERR ;
849850 }
850-
851- aws_byte_buf_init (out_checksum , allocator , encoded_checksum_len );
851+ if (checksum_buf -> len > 0 ) {
852+ if (checksum_buf -> len == encoded_checksum_len ) {
853+ return AWS_OP_SUCCESS ;
854+ } else {
855+ AWS_LOGF_ERROR (
856+ AWS_LS_S3_GENERAL ,
857+ "Mismatched checksum buffer and algorithm. checksum_buf->len is %zu, but encoded_checksum_len is %zu" ,
858+ checksum_buf -> len ,
859+ encoded_checksum_len );
860+ aws_raise_error (AWS_ERROR_INVALID_ARGUMENT );
861+ return AWS_OP_ERR ;
862+ }
863+ }
864+ aws_byte_buf_init (checksum_buf , allocator , encoded_checksum_len );
852865
853866 struct aws_byte_buf raw_checksum ;
854867 aws_byte_buf_init (& raw_checksum , allocator , digest_size );
@@ -857,14 +870,14 @@ static int s_calculate_in_memory_checksum_helper(
857870 goto done ;
858871 }
859872 struct aws_byte_cursor raw_checksum_cursor = aws_byte_cursor_from_buf (& raw_checksum );
860- if (aws_base64_encode (& raw_checksum_cursor , out_checksum )) {
873+ if (aws_base64_encode (& raw_checksum_cursor , checksum_buf )) {
861874 goto done ;
862875 }
863876
864877 ret_code = AWS_OP_SUCCESS ;
865878done :
866879 if (ret_code ) {
867- aws_byte_buf_clean_up (out_checksum );
880+ aws_byte_buf_clean_up (checksum_buf );
868881 }
869882 aws_byte_buf_clean_up (& raw_checksum );
870883 return ret_code ;
@@ -880,19 +893,19 @@ static int s_calculate_and_add_checksum_to_header_helper(
880893 struct aws_byte_cursor data ,
881894 const struct checksum_config_storage * checksum_config ,
882895 struct aws_http_message * out_message ,
883- struct aws_byte_buf * out_checksum ) {
896+ struct aws_byte_buf * checksum_buf ) {
884897 AWS_ASSERT (checksum_config -> checksum_algorithm != AWS_SCA_NONE );
885898 AWS_ASSERT (out_message != NULL );
886899 int ret_code = AWS_OP_ERR ;
887900
888901 struct aws_byte_buf local_encoded_checksum_buf ;
902+ AWS_ZERO_STRUCT (local_encoded_checksum_buf );
889903 struct aws_byte_buf * local_encoded_checksum ;
890- if (out_checksum == NULL ) {
904+ if (checksum_buf == NULL ) {
891905 local_encoded_checksum = & local_encoded_checksum_buf ;
892906 } else {
893- local_encoded_checksum = out_checksum ;
907+ local_encoded_checksum = checksum_buf ;
894908 }
895- AWS_ZERO_STRUCT (* local_encoded_checksum );
896909 if (s_calculate_in_memory_checksum_helper (allocator , data , checksum_config , local_encoded_checksum )) {
897910 goto done ;
898911 }
@@ -908,8 +921,8 @@ static int s_calculate_and_add_checksum_to_header_helper(
908921
909922 ret_code = AWS_OP_SUCCESS ;
910923done :
911- if (ret_code || out_checksum == NULL ) {
912- /* In case of error happen or out_checksum is not set, clean up the encoded checksum. Otherwise, the caller will
924+ if (ret_code || checksum_buf == NULL ) {
925+ /* In case of error happen or checksum_buf is not set, clean up the encoded checksum. Otherwise, the caller will
913926 * own the encoded checksum. */
914927 aws_byte_buf_clean_up (local_encoded_checksum );
915928 }
@@ -922,7 +935,7 @@ struct aws_input_stream *aws_s3_message_util_assign_body(
922935 struct aws_byte_buf * byte_buf ,
923936 struct aws_http_message * out_message ,
924937 const struct checksum_config_storage * checksum_config ,
925- struct aws_byte_buf * out_checksum ) {
938+ struct aws_byte_buf * checksum_buf ) {
926939 AWS_PRECONDITION (allocator );
927940 AWS_PRECONDITION (out_message );
928941 AWS_PRECONDITION (byte_buf );
@@ -992,7 +1005,7 @@ struct aws_input_stream *aws_s3_message_util_assign_body(
9921005 }
9931006 /* set input stream to chunk stream */
9941007 struct aws_input_stream * chunk_stream =
995- aws_chunk_stream_new (allocator , input_stream , checksum_config -> checksum_algorithm , out_checksum );
1008+ aws_chunk_stream_new (allocator , input_stream , checksum_config -> checksum_algorithm , checksum_buf );
9961009 if (!chunk_stream ) {
9971010 goto error_clean_up ;
9981011 }
@@ -1001,14 +1014,14 @@ struct aws_input_stream *aws_s3_message_util_assign_body(
10011014 } else if (checksum_config -> location == AWS_SCL_HEADER ) {
10021015 /* Calculate the checksum directly from memory and add it to the header. */
10031016 if (s_calculate_and_add_checksum_to_header_helper (
1004- allocator , buffer_byte_cursor , checksum_config , out_message , out_checksum )) {
1017+ allocator , buffer_byte_cursor , checksum_config , out_message , checksum_buf )) {
10051018 goto error_clean_up ;
10061019 }
10071020
1008- } else if (checksum_config -> checksum_algorithm != AWS_SCA_NONE && out_checksum != NULL ) {
1009- /* In case checksums still wanted, and we can calculate it directly from the buffer in memory to
1010- * out_checksum */
1011- if (s_calculate_in_memory_checksum_helper (allocator , buffer_byte_cursor , checksum_config , out_checksum )) {
1021+ } else if (checksum_config -> checksum_algorithm != AWS_SCA_NONE && checksum_buf != NULL ) {
1022+ /* In case checksums still wanted, but not sent to the server, and we can calculate it directly from the
1023+ * buffer in memory to out_checksum */
1024+ if (s_calculate_in_memory_checksum_helper (allocator , buffer_byte_cursor , checksum_config , checksum_buf )) {
10121025 goto error_clean_up ;
10131026 }
10141027 }
0 commit comments