| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include "aws/s3/private/s3_checksum_context.h" |
| 7 |
#include "aws/s3/private/s3_checksums.h" |
| 8 |
#include <aws/common/encoding.h> |
| 9 |
#include <aws/io/stream.h> |
| 10 |
|
| 11 |
struct aws_checksum_stream { |
| 12 |
struct aws_input_stream base; |
| 13 |
struct aws_allocator *allocator; |
| 14 |
|
| 15 |
struct aws_input_stream *old_stream; |
| 16 |
struct aws_s3_checksum *checksum; |
| 17 |
struct aws_s3_upload_request_checksum_context *context; |
| 18 |
struct aws_byte_buf checksum_result; |
| 19 |
}; |
| 20 |
|
| 21 |
static int s_aws_input_checksum_stream_seek( |
| 22 |
struct aws_input_stream *stream, |
| 23 |
int64_t offset, |
| 24 |
enum aws_stream_seek_basis basis) { |
| 25 |
(void)stream; |
| 26 |
(void)offset; |
| 27 |
(void)basis; |
| 28 |
AWS_LOGF_ERROR( |
| 29 |
AWS_LS_S3_CLIENT, |
| 30 |
"Cannot seek on checksum stream, as it will cause the checksum output to mismatch the checksum of the stream " |
| 31 |
"contents"); |
| 32 |
AWS_ASSERT(false); |
| 33 |
return aws_raise_error(AWS_ERROR_UNSUPPORTED_OPERATION); |
| 34 |
} |
| 35 |
|
| 36 |
static int s_aws_input_checksum_stream_read(struct aws_input_stream *stream, struct aws_byte_buf *dest) { |
| 37 |
struct aws_checksum_stream *impl = AWS_CONTAINER_OF(stream, struct aws_checksum_stream, base); |
| 38 |
|
| 39 |
size_t original_len = dest->len; |
| 40 |
if (aws_input_stream_read(impl->old_stream, dest)) { |
| 41 |
return AWS_OP_ERR; |
| 42 |
} |
| 43 |
struct aws_byte_cursor to_sum = aws_byte_cursor_from_buf(dest); |
| 44 |
|
| 45 |
aws_byte_cursor_advance(&to_sum, original_len); |
| 46 |
|
| 47 |
|
| 48 |
if (aws_checksum_update(impl->checksum, &to_sum)) { |
| 49 |
return AWS_OP_ERR; |
| 50 |
} |
| 51 |
if (impl->context) { |
| 52 |
|
| 53 |
struct aws_stream_status status; |
| 54 |
if (aws_input_stream_get_status(impl->old_stream, &status)) { |
| 55 |
return AWS_OP_ERR; |
| 56 |
} |
| 57 |
if (status.is_end_of_stream) { |
| 58 |
if (aws_checksum_finalize(impl->checksum, &impl->checksum_result) != AWS_OP_SUCCESS) { |
| 59 |
AWS_LOGF_ERROR( |
| 60 |
AWS_LS_S3_CLIENT, |
| 61 |
"Failed to calculate checksum with error code %d (%s).", |
| 62 |
aws_last_error(), |
| 63 |
aws_error_str(aws_last_error())); |
| 64 |
aws_byte_buf_reset(&impl->checksum_result, true); |
| 65 |
return aws_raise_error(AWS_ERROR_S3_CHECKSUM_CALCULATION_FAILED); |
| 66 |
} |
| 67 |
struct aws_byte_cursor checksum_result_cursor = aws_byte_cursor_from_buf(&impl->checksum_result); |
| 68 |
if (aws_s3_upload_request_checksum_context_finalize_checksum(impl->context, checksum_result_cursor) != |
| 69 |
AWS_OP_SUCCESS) { |
| 70 |
AWS_LOGF_ERROR( |
| 71 |
AWS_LS_S3_CLIENT, |
| 72 |
"Failed to finalize checksum context with error code %d (%s).", |
| 73 |
aws_last_error(), |
| 74 |
aws_error_str(aws_last_error())); |
| 75 |
aws_byte_buf_reset(&impl->checksum_result, true); |
| 76 |
return aws_raise_error(AWS_ERROR_S3_CHECKSUM_CALCULATION_FAILED); |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
return AWS_OP_SUCCESS; |
| 81 |
} |
| 82 |
|
| 83 |
static int s_aws_input_checksum_stream_get_status(struct aws_input_stream *stream, struct aws_stream_status *status) { |
| 84 |
struct aws_checksum_stream *impl = AWS_CONTAINER_OF(stream, struct aws_checksum_stream, base); |
| 85 |
return aws_input_stream_get_status(impl->old_stream, status); |
| 86 |
} |
| 87 |
|
| 88 |
static int s_aws_input_checksum_stream_get_length(struct aws_input_stream *stream, int64_t *out_length) { |
| 89 |
struct aws_checksum_stream *impl = AWS_CONTAINER_OF(stream, struct aws_checksum_stream, base); |
| 90 |
return aws_input_stream_get_length(impl->old_stream, out_length); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
static void s_aws_input_checksum_stream_destroy(struct aws_checksum_stream *impl) { |
| 98 |
if (!impl) { |
| 99 |
return; |
| 100 |
} |
| 101 |
aws_checksum_destroy(impl->checksum); |
| 102 |
aws_input_stream_release(impl->old_stream); |
| 103 |
aws_byte_buf_clean_up(&impl->checksum_result); |
| 104 |
aws_s3_upload_request_checksum_context_release(impl->context); |
| 105 |
aws_mem_release(impl->allocator, impl); |
| 106 |
} |
| 107 |
|
| 108 |
static struct aws_input_stream_vtable s_aws_input_checksum_stream_vtable = { |
| 109 |
.seek = s_aws_input_checksum_stream_seek, |
| 110 |
.read = s_aws_input_checksum_stream_read, |
| 111 |
.get_status = s_aws_input_checksum_stream_get_status, |
| 112 |
.get_length = s_aws_input_checksum_stream_get_length, |
| 113 |
}; |
| 114 |
|
| 115 |
static struct aws_checksum_stream *s_aws_checksum_input_checksum_stream_new( |
| 116 |
struct aws_allocator *allocator, |
| 117 |
struct aws_input_stream *existing_stream, |
| 118 |
enum aws_s3_checksum_algorithm algorithm) { |
| 119 |
AWS_PRECONDITION(existing_stream); |
| 120 |
|
| 121 |
struct aws_checksum_stream *impl = aws_mem_calloc(allocator, 1, sizeof(struct aws_checksum_stream)); |
| 122 |
impl->allocator = allocator; |
| 123 |
impl->base.vtable = &s_aws_input_checksum_stream_vtable; |
| 124 |
|
| 125 |
impl->checksum = aws_checksum_new(allocator, algorithm); |
| 126 |
if (impl->checksum == NULL) { |
| 127 |
goto on_error; |
| 128 |
} |
| 129 |
aws_byte_buf_init(&impl->checksum_result, allocator, impl->checksum->digest_size); |
| 130 |
impl->old_stream = aws_input_stream_acquire(existing_stream); |
| 131 |
aws_ref_count_init( |
| 132 |
&impl->base.ref_count, impl, (aws_simple_completion_callback *)s_aws_input_checksum_stream_destroy); |
| 133 |
return impl; |
| 134 |
on_error: |
| 135 |
aws_mem_release(impl->allocator, impl); |
| 136 |
return NULL; |
| 137 |
} |
| 138 |
|
| 139 |
struct aws_input_stream *aws_checksum_stream_new( |
| 140 |
struct aws_allocator *allocator, |
| 141 |
struct aws_input_stream *existing_stream, |
| 142 |
enum aws_s3_checksum_algorithm algorithm) { |
| 143 |
AWS_PRECONDITION(existing_stream); |
| 144 |
struct aws_checksum_stream *impl = s_aws_checksum_input_checksum_stream_new(allocator, existing_stream, algorithm); |
| 145 |
if (impl) { |
| 146 |
return &impl->base; |
| 147 |
} |
| 148 |
return NULL; |
| 149 |
} |
| 150 |
|
| 151 |
struct aws_input_stream *aws_checksum_stream_new_with_context( |
| 152 |
struct aws_allocator *allocator, |
| 153 |
struct aws_input_stream *existing_stream, |
| 154 |
struct aws_s3_upload_request_checksum_context *context) { |
| 155 |
AWS_PRECONDITION(existing_stream); |
| 156 |
AWS_PRECONDITION(context); |
| 157 |
struct aws_checksum_stream *impl = |
| 158 |
s_aws_checksum_input_checksum_stream_new(allocator, existing_stream, context->algorithm); |
| 159 |
if (impl) { |
| 160 |
impl->context = aws_s3_upload_request_checksum_context_acquire(context); |
| 161 |
return &impl->base; |
| 162 |
} |
| 163 |
return NULL; |
| 164 |
} |
| 165 |
|
| 166 |
int aws_checksum_stream_finalize_checksum(struct aws_input_stream *checksum_stream, struct aws_byte_buf *checksum_buf) { |
| 167 |
AWS_PRECONDITION(checksum_buf); |
| 168 |
AWS_PRECONDITION(checksum_buf->len == 0 && "Checksum output buffer is not empty"); |
| 169 |
int rt_code = AWS_OP_ERR; |
| 170 |
|
| 171 |
struct aws_checksum_stream *impl = AWS_CONTAINER_OF(checksum_stream, struct aws_checksum_stream, base); |
| 172 |
struct aws_byte_buf checksum_result; |
| 173 |
aws_byte_buf_init(&checksum_result, impl->allocator, impl->checksum->digest_size); |
| 174 |
|
| 175 |
if (aws_checksum_finalize(impl->checksum, &checksum_result) != AWS_OP_SUCCESS) { |
| 176 |
AWS_LOGF_ERROR( |
| 177 |
AWS_LS_S3_CLIENT, |
| 178 |
"Failed to calculate checksum with error code %d (%s).", |
| 179 |
aws_last_error(), |
| 180 |
aws_error_str(aws_last_error())); |
| 181 |
aws_raise_error(AWS_ERROR_S3_CHECKSUM_CALCULATION_FAILED); |
| 182 |
goto done; |
| 183 |
} |
| 184 |
struct aws_byte_cursor checksum_result_cursor = aws_byte_cursor_from_buf(&checksum_result); |
| 185 |
if (aws_base64_encode(&checksum_result_cursor, checksum_buf) != AWS_OP_SUCCESS) { |
| 186 |
AWS_LOGF_ERROR( |
| 187 |
AWS_LS_S3_CLIENT, |
| 188 |
"Failed to base64 encode checksum with error code %d (%s). Output capacity: %zu length %zu", |
| 189 |
aws_last_error(), |
| 190 |
aws_error_str(aws_last_error()), |
| 191 |
checksum_buf->capacity, |
| 192 |
checksum_buf->len); |
| 193 |
aws_raise_error(AWS_ERROR_S3_CHECKSUM_CALCULATION_FAILED); |
| 194 |
goto done; |
| 195 |
} |
| 196 |
rt_code = AWS_OP_SUCCESS; |
| 197 |
done: |
| 198 |
aws_byte_buf_clean_up(&checksum_result); |
| 199 |
return rt_code; |
| 200 |
} |