| 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/common/logging.h> |
| 10 |
|
| 11 |
static void s_lock_synced_data(struct aws_s3_upload_request_checksum_context *context) { |
| 12 |
aws_mutex_lock(&context->synced_data.lock); |
| 13 |
} |
| 14 |
|
| 15 |
static void s_unlock_synced_data(struct aws_s3_upload_request_checksum_context *context) { |
| 16 |
aws_mutex_unlock(&context->synced_data.lock); |
| 17 |
} |
| 18 |
|
| 19 |
static void s_aws_s3_upload_request_checksum_context_destroy(void *context) { |
| 20 |
struct aws_s3_upload_request_checksum_context *checksum_context = context; |
| 21 |
aws_byte_buf_clean_up(&checksum_context->synced_data.base64_checksum); |
| 22 |
aws_mutex_clean_up(&checksum_context->synced_data.lock); |
| 23 |
aws_mem_release(checksum_context->allocator, checksum_context); |
| 24 |
} |
| 25 |
|
| 26 |
static struct aws_s3_upload_request_checksum_context *s_s3_upload_request_checksum_context_new_base( |
| 27 |
struct aws_allocator *allocator, |
| 28 |
const struct aws_s3_meta_request_checksum_config_storage *checksum_config, |
| 29 |
bool has_review_callback) { |
| 30 |
AWS_PRECONDITION(allocator); |
| 31 |
|
| 32 |
struct aws_s3_upload_request_checksum_context *context = |
| 33 |
aws_mem_calloc(allocator, 1, sizeof(struct aws_s3_upload_request_checksum_context)); |
| 34 |
|
| 35 |
aws_ref_count_init(&context->ref_count, context, s_aws_s3_upload_request_checksum_context_destroy); |
| 36 |
context->allocator = allocator; |
| 37 |
if (aws_mutex_init(&context->synced_data.lock)) { |
| 38 |
aws_s3_upload_request_checksum_context_release(context); |
| 39 |
return NULL; |
| 40 |
} |
| 41 |
|
| 42 |
if (!checksum_config || checksum_config->checksum_algorithm == AWS_SCA_NONE) { |
| 43 |
context->algorithm = AWS_SCA_NONE; |
| 44 |
context->encoded_checksum_size = 0; |
| 45 |
return context; |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
context->algorithm = checksum_config->checksum_algorithm; |
| 50 |
context->location = checksum_config->location; |
| 51 |
context->encoded_checksum_size = aws_get_digest_size_from_checksum_algorithm(context->algorithm); |
| 52 |
context->has_review_callback = has_review_callback; |
| 53 |
|
| 54 |
|
| 55 |
size_t encoded_size = 0; |
| 56 |
if (aws_base64_compute_encoded_len(context->encoded_checksum_size, &encoded_size)) { |
| 57 |
AWS_LOGF_ERROR(AWS_LS_S3_GENERAL, "Failed to compute base64 encoded length for checksum"); |
| 58 |
aws_s3_upload_request_checksum_context_release(context); |
| 59 |
return NULL; |
| 60 |
} |
| 61 |
context->encoded_checksum_size = encoded_size; |
| 62 |
return context; |
| 63 |
} |
| 64 |
|
| 65 |
struct aws_s3_upload_request_checksum_context *aws_s3_upload_request_checksum_context_new( |
| 66 |
struct aws_allocator *allocator, |
| 67 |
const struct aws_s3_meta_request_checksum_config_storage *checksum_config, |
| 68 |
bool has_review_callback) { |
| 69 |
struct aws_s3_upload_request_checksum_context *context = |
| 70 |
s_s3_upload_request_checksum_context_new_base(allocator, checksum_config, has_review_callback); |
| 71 |
if (context && context->encoded_checksum_size > 0) { |
| 72 |
|
| 73 |
aws_byte_buf_init(&context->synced_data.base64_checksum, allocator, context->encoded_checksum_size); |
| 74 |
} |
| 75 |
return context; |
| 76 |
} |
| 77 |
|
| 78 |
struct aws_s3_upload_request_checksum_context *aws_s3_upload_request_checksum_context_new_with_existing_base64_checksum( |
| 79 |
struct aws_allocator *allocator, |
| 80 |
const struct aws_s3_meta_request_checksum_config_storage *checksum_config, |
| 81 |
struct aws_byte_cursor existing_base64_checksum, |
| 82 |
bool has_review_callback) { |
| 83 |
struct aws_s3_upload_request_checksum_context *context = |
| 84 |
s_s3_upload_request_checksum_context_new_base(allocator, checksum_config, has_review_callback); |
| 85 |
if (context) { |
| 86 |
|
| 87 |
if (context->encoded_checksum_size != existing_base64_checksum.len) { |
| 88 |
struct aws_byte_cursor algo_name = aws_get_checksum_algorithm_name(context->algorithm); |
| 89 |
AWS_LOGF_ERROR( |
| 90 |
AWS_LS_S3_GENERAL, |
| 91 |
"Encoded checksum size mismatch during creating the context for algorithm " PRInSTR |
| 92 |
": expected %zu bytes, got %zu bytes", |
| 93 |
AWS_BYTE_CURSOR_PRI(algo_name), |
| 94 |
context->encoded_checksum_size, |
| 95 |
existing_base64_checksum.len); |
| 96 |
aws_s3_upload_request_checksum_context_release(context); |
| 97 |
return NULL; |
| 98 |
} |
| 99 |
aws_byte_buf_init_copy_from_cursor(&context->synced_data.base64_checksum, allocator, existing_base64_checksum); |
| 100 |
context->synced_data.checksum_calculated = true; |
| 101 |
} |
| 102 |
return context; |
| 103 |
} |
| 104 |
|
| 105 |
struct aws_s3_upload_request_checksum_context *aws_s3_upload_request_checksum_context_acquire( |
| 106 |
struct aws_s3_upload_request_checksum_context *context) { |
| 107 |
if (context) { |
| 108 |
aws_ref_count_acquire(&context->ref_count); |
| 109 |
} |
| 110 |
return context; |
| 111 |
} |
| 112 |
|
| 113 |
struct aws_s3_upload_request_checksum_context *aws_s3_upload_request_checksum_context_release( |
| 114 |
struct aws_s3_upload_request_checksum_context *context) { |
| 115 |
if (context) { |
| 116 |
aws_ref_count_release(&context->ref_count); |
| 117 |
} |
| 118 |
return NULL; |
| 119 |
} |
| 120 |
|
| 121 |
bool aws_s3_upload_request_checksum_context_should_calculate(struct aws_s3_upload_request_checksum_context *context) { |
| 122 |
if (!context || context->algorithm == AWS_SCA_NONE || context->algorithm == AWS_SCA_UNKNOWN || |
| 123 |
(context->location == AWS_SCL_NONE && !context->has_review_callback)) { |
| 124 |
return false; |
| 125 |
} |
| 126 |
|
| 127 |
bool should_calculate = false; |
| 128 |
s_lock_synced_data(context); |
| 129 |
|
| 130 |
should_calculate = !context->synced_data.checksum_calculated; |
| 131 |
s_unlock_synced_data(context); |
| 132 |
|
| 133 |
return should_calculate; |
| 134 |
} |
| 135 |
|
| 136 |
bool aws_s3_upload_request_checksum_context_should_add_header( |
| 137 |
const struct aws_s3_upload_request_checksum_context *context) { |
| 138 |
if (!context) { |
| 139 |
return false; |
| 140 |
} |
| 141 |
|
| 142 |
return context->location == AWS_SCL_HEADER && context->algorithm != AWS_SCA_NONE; |
| 143 |
} |
| 144 |
|
| 145 |
bool aws_s3_upload_request_checksum_context_should_add_trailer( |
| 146 |
const struct aws_s3_upload_request_checksum_context *context) { |
| 147 |
if (!context) { |
| 148 |
return false; |
| 149 |
} |
| 150 |
|
| 151 |
return context->location == AWS_SCL_TRAILER && context->algorithm != AWS_SCA_NONE; |
| 152 |
} |
| 153 |
|
| 154 |
int aws_s3_upload_request_checksum_context_finalize_checksum( |
| 155 |
struct aws_s3_upload_request_checksum_context *context, |
| 156 |
struct aws_byte_cursor raw_checksum_cursor) { |
| 157 |
if (!context) { |
| 158 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 159 |
} |
| 160 |
s_lock_synced_data(context); |
| 161 |
|
| 162 |
if (!context->synced_data.checksum_calculated) { |
| 163 |
AWS_ASSERT(context->synced_data.base64_checksum.len == 0); |
| 164 |
|
| 165 |
if (aws_base64_encode(&raw_checksum_cursor, &context->synced_data.base64_checksum)) { |
| 166 |
aws_byte_buf_reset(&context->synced_data.base64_checksum, false); |
| 167 |
AWS_LOGF_ERROR( |
| 168 |
AWS_LS_S3_GENERAL, |
| 169 |
"Failed to base64 encode for the checksum. Raw checksum length: %zu. Output buffer capacity: %zu " |
| 170 |
"length %zu", |
| 171 |
raw_checksum_cursor.len, |
| 172 |
context->synced_data.base64_checksum.capacity, |
| 173 |
context->synced_data.base64_checksum.len); |
| 174 |
s_unlock_synced_data(context); |
| 175 |
return AWS_OP_ERR; |
| 176 |
} |
| 177 |
context->synced_data.checksum_calculated = true; |
| 178 |
} |
| 179 |
s_unlock_synced_data(context); |
| 180 |
return AWS_OP_SUCCESS; |
| 181 |
} |
| 182 |
|
| 183 |
struct aws_byte_cursor aws_s3_upload_request_checksum_context_get_checksum_cursor( |
| 184 |
struct aws_s3_upload_request_checksum_context *context) { |
| 185 |
struct aws_byte_cursor checksum_cursor = {0}; |
| 186 |
if (!context) { |
| 187 |
return checksum_cursor; |
| 188 |
} |
| 189 |
s_lock_synced_data(context); |
| 190 |
if (context->synced_data.checksum_calculated) { |
| 191 |
checksum_cursor = aws_byte_cursor_from_buf(&context->synced_data.base64_checksum); |
| 192 |
} |
| 193 |
s_unlock_synced_data(context); |
| 194 |
return checksum_cursor; |
| 195 |
} |