Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

calculate sha256 #704

Closed
zhzfight opened this issue Jan 17, 2024 · 3 comments
Closed

calculate sha256 #704

zhzfight opened this issue Jan 17, 2024 · 3 comments

Comments

@zhzfight
Copy link

zhzfight commented Jan 17, 2024

it seems like a bug?

in mountpoint-s3-crt-sys/crt/aws-c-s3/source/s3_meta_request.c

/* Handles signing a message for the caller. */
void aws_s3_meta_request_sign_request_default(
    struct aws_s3_meta_request *meta_request,
    struct aws_s3_request *request,
    aws_signing_complete_fn *on_signing_complete,
    void *user_data) {
    AWS_PRECONDITION(meta_request);
    AWS_PRECONDITION(request);
    AWS_PRECONDITION(on_signing_complete);

    struct aws_s3_client *client = meta_request->client;
    AWS_ASSERT(client);

    struct aws_signing_config_aws signing_config;

in this func, signing_config has not been initialized, so when i check

if (signing_config.signed_body_value.len == 0) {

i can find it not equal to 0.

This indirectly caused the following code to not work when calculating sha256.

static int s_build_canonical_payload(struct aws_signing_state_aws *state) {
    const struct aws_signable *signable = state->signable;
    struct aws_allocator *allocator = state->allocator;
    struct aws_byte_buf *payload_hash_buffer = &state->payload_hash;

    AWS_ASSERT(payload_hash_buffer->len == 0);

    struct aws_byte_buf body_buffer;
    AWS_ZERO_STRUCT(body_buffer);
    struct aws_byte_buf digest_buffer;
    AWS_ZERO_STRUCT(digest_buffer);

    struct aws_hash *hash = NULL;

    int result = AWS_OP_ERR;
    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;
        }

        if (payload_stream != NULL) {
            if (aws_input_stream_seek(payload_stream, 0, AWS_SSB_BEGIN)) {
                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;
                }
            }

            /* reset the input stream for sending */
            if (aws_input_stream_seek(payload_stream, 0, AWS_SSB_BEGIN)) {
                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;
        }
    }

    result = AWS_OP_SUCCESS;

on_cleanup:

    aws_byte_buf_clean_up(&digest_buffer);
    aws_byte_buf_clean_up(&body_buffer);

    if (hash) {
        aws_hash_destroy(hash);
    }

    return result;
}

it will not caculate sha256 forever, even though i set the trailer checksum to false.

@zhzfight
Copy link
Author

in some old s3 service, they will check the sha256, I encountered a potential error while trying to adapt mounts3 to this type of s3 service.

@passaro
Copy link
Contributor

passaro commented Jan 17, 2024

Hi @zhzfight, if I understand correctly, you are seeing this issue when trying to change the configuration of aws_s3_client from what we use in Mountpoint. In that case, I would suggest you report it on https://github.com/awslabs/aws-c-s3.

@passaro
Copy link
Contributor

passaro commented Jan 19, 2024

I'm closing this issue as it does not seem relevant for Mountpoint.

@passaro passaro closed this as not planned Won't fix, can't repro, duplicate, stale Jan 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants