Skip to content

Commit

Permalink
this is better
Browse files Browse the repository at this point in the history
  • Loading branch information
TingDaoK committed Sep 4, 2024
1 parent c626563 commit 75a2b99
Showing 1 changed file with 48 additions and 46 deletions.
94 changes: 48 additions & 46 deletions source/aws_signing.c
Original file line number Diff line number Diff line change
Expand Up @@ -1531,71 +1531,73 @@ static int s_build_canonical_payload(struct aws_signing_state_aws *state) {
aws_byte_cursor_eq_c_str(&state->config.service, "vpc-lattice-svcs")) {
/* NOTES: TEMPORAY WORKAROUND FOR VPC Lattice. SHALL BE REMOVED IN NEAR FUTURE */
/* ALWAYS USE UNSIGNED-PAYLOAD FOR VPC Lattice. */
if (aws_byte_buf_append_dynamic(payload_hash_buffer, &g_aws_signed_body_value_unsigned_payload)) {
if (aws_byte_buf_append_dynamic(payload_hash_buffer, &g_aws_signed_body_value_unsigned_payload) ==
AWS_OP_SUCCESS) {
result = AWS_OP_SUCCESS;
}
goto on_cleanup;
}

if (state->config.signed_body_value.len == 0) {
/* No value provided by user, so we must calculate it */
hash = aws_sha256_new(allocator);
if (hash == NULL) {
return AWS_OP_ERR;
}

if (aws_byte_buf_init(&body_buffer, allocator, BODY_READ_BUFFER_SIZE) ||
aws_byte_buf_init(&digest_buffer, allocator, AWS_SHA256_LEN)) {
goto on_cleanup;
}
} else {
if (state->config.signed_body_value.len == 0) {
/* No value provided by user, so we must calculate it */
hash = aws_sha256_new(allocator);
if (hash == NULL) {
return AWS_OP_ERR;
}

if (aws_byte_buf_init(&body_buffer, allocator, BODY_READ_BUFFER_SIZE) ||
aws_byte_buf_init(&digest_buffer, allocator, AWS_SHA256_LEN)) {
goto on_cleanup;
}
struct aws_input_stream *payload_stream = NULL;
if (aws_signable_get_payload_stream(signable, &payload_stream)) {
goto on_cleanup;
}

struct aws_input_stream *payload_stream = NULL;
if (aws_signable_get_payload_stream(signable, &payload_stream)) {
if (payload_stream != NULL) {
if (aws_input_stream_seek(payload_stream, 0, AWS_SSB_BEGIN)) {
goto on_cleanup;
}

if (payload_stream != NULL) {
if (aws_input_stream_seek(payload_stream, 0, AWS_SSB_BEGIN)) {
struct aws_stream_status payload_status;
AWS_ZERO_STRUCT(payload_status);

while (!payload_status.is_end_of_stream) {
/* reset the temporary body buffer; we can calculate the hash in window chunks */
body_buffer.len = 0;
if (aws_input_stream_read(payload_stream, &body_buffer)) {
goto on_cleanup;
}

struct aws_stream_status payload_status;
AWS_ZERO_STRUCT(payload_status);

while (!payload_status.is_end_of_stream) {
/* reset the temporary body buffer; we can calculate the hash in window chunks */
body_buffer.len = 0;
if (aws_input_stream_read(payload_stream, &body_buffer)) {
goto on_cleanup;
}

if (body_buffer.len > 0) {
struct aws_byte_cursor body_cursor = aws_byte_cursor_from_buf(&body_buffer);
aws_hash_update(hash, &body_cursor);
}

if (aws_input_stream_get_status(payload_stream, &payload_status)) {
goto on_cleanup;
}
if (body_buffer.len > 0) {
struct aws_byte_cursor body_cursor = aws_byte_cursor_from_buf(&body_buffer);
aws_hash_update(hash, &body_cursor);
}

/* reset the input stream for sending */
if (aws_input_stream_seek(payload_stream, 0, AWS_SSB_BEGIN)) {
if (aws_input_stream_get_status(payload_stream, &payload_status)) {
goto on_cleanup;
}
}

if (aws_hash_finalize(hash, &digest_buffer, 0)) {
/* reset the input stream for sending */
if (aws_input_stream_seek(payload_stream, 0, AWS_SSB_BEGIN)) {
goto on_cleanup;
}
}

struct aws_byte_cursor digest_cursor = aws_byte_cursor_from_buf(&digest_buffer);
if (aws_hex_encode_append_dynamic(&digest_cursor, payload_hash_buffer)) {
goto on_cleanup;
}
} else {
/* Use value provided in config */
if (aws_byte_buf_append_dynamic(payload_hash_buffer, &state->config.signed_body_value)) {
goto on_cleanup;
}
if (aws_hash_finalize(hash, &digest_buffer, 0)) {
goto on_cleanup;
}

struct aws_byte_cursor digest_cursor = aws_byte_cursor_from_buf(&digest_buffer);
if (aws_hex_encode_append_dynamic(&digest_cursor, payload_hash_buffer)) {
goto on_cleanup;
}
} else {
/* Use value provided in config */
if (aws_byte_buf_append_dynamic(payload_hash_buffer, &state->config.signed_body_value)) {
goto on_cleanup;
}
}

Expand Down

0 comments on commit 75a2b99

Please sign in to comment.