Skip to content

Commit 10ceefe

Browse files
committed
fix; Improve initialization. PR#451
2 parents 6f2b086 + ac64bdc commit 10ceefe

File tree

9 files changed

+38
-22
lines changed

9 files changed

+38
-22
lines changed

source/cipher_openssl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ static int serialize_pubkey(struct aws_allocator *alloc, EC_KEY *keypair, struct
252252
}
253253

254254
binary = aws_byte_cursor_from_array(buf, length);
255-
b64 = aws_byte_buf_from_array(tmp, sizeof(tmp));
255+
b64 = aws_byte_buf_from_empty_array(tmp, sizeof(tmp));
256256

257257
if (aws_base64_compute_encoded_len(length, &b64_len)) {
258258
goto err;

source/edk.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@ int aws_cryptosdk_edk_list_init(struct aws_allocator *alloc, struct aws_array_li
2020
}
2121

2222
void aws_cryptosdk_edk_clean_up(struct aws_cryptosdk_edk *edk) {
23-
aws_byte_buf_clean_up(&edk->provider_id);
24-
aws_byte_buf_clean_up(&edk->provider_info);
25-
aws_byte_buf_clean_up(&edk->ciphertext);
23+
if(edk->provider_id.allocator)
24+
aws_byte_buf_clean_up(&edk->provider_id);
25+
if(edk->provider_info.allocator)
26+
aws_byte_buf_clean_up(&edk->provider_info);
27+
if(edk->ciphertext.allocator)
28+
aws_byte_buf_clean_up(&edk->ciphertext);
2629
}
2730

2831
void aws_cryptosdk_edk_list_clear(struct aws_array_list *edk_list) {

source/error.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,5 @@ static const struct aws_error_info_list error_info_list = { .error_list = error_
3030
.count = sizeof(error_info) / sizeof(error_info[0]) };
3131

3232
void aws_cryptosdk_load_error_strings() {
33-
aws_load_error_strings();
3433
aws_register_error_info(&error_info_list);
3534
}

source/header.c

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,10 @@ void aws_cryptosdk_hdr_clean_up(struct aws_cryptosdk_hdr *hdr) {
102102
// Idempotent cleanup
103103
return;
104104
}
105-
106-
aws_byte_buf_clean_up(&hdr->iv);
107-
aws_byte_buf_clean_up(&hdr->auth_tag);
105+
if(hdr->iv.allocator)
106+
aws_byte_buf_clean_up(&hdr->iv);
107+
if(hdr->auth_tag.allocator)
108+
aws_byte_buf_clean_up(&hdr->auth_tag);
108109

109110
aws_cryptosdk_edk_list_clean_up(&hdr->edk_list);
110111
aws_cryptosdk_enc_ctx_clean_up(&hdr->enc_ctx);
@@ -288,7 +289,12 @@ int aws_cryptosdk_hdr_size(const struct aws_cryptosdk_hdr *hdr) {
288289

289290
return bytes == SIZE_MAX ? 0 : bytes;
290291
}
291-
292+
static void init_aws_byte_buf_raw(struct aws_byte_buf *buf){
293+
buf->allocator = NULL;
294+
buf->buffer = NULL;
295+
buf->len = 0;
296+
buf->capacity = 0;
297+
}
292298
int aws_cryptosdk_hdr_write(
293299
const struct aws_cryptosdk_hdr *hdr, size_t *bytes_written, uint8_t *outbuf, size_t outlen) {
294300
struct aws_byte_buf output = aws_byte_buf_from_array(outbuf, outlen);
@@ -302,6 +308,8 @@ int aws_cryptosdk_hdr_write(
302308
// TODO - unify everything on byte_bufs when the aws-c-common refactor lands
303309
// See: https://github.com/awslabs/aws-c-common/pull/130
304310
struct aws_byte_buf aad_length_field;
311+
init_aws_byte_buf_raw(&aad_length_field);
312+
305313
if (!aws_byte_buf_advance(&output, &aad_length_field, 2)) goto WRITE_ERR;
306314

307315
size_t old_len = output.len;
@@ -321,13 +329,13 @@ int aws_cryptosdk_hdr_write(
321329
const struct aws_cryptosdk_edk *edk = vp_edk;
322330

323331
if (!aws_byte_buf_write_be16(&output, (uint16_t)edk->provider_id.len)) goto WRITE_ERR;
324-
if (!aws_byte_buf_write_from_whole_buffer(&output, edk->provider_id)) goto WRITE_ERR;
332+
if (!aws_byte_buf_write_from_whole_cursor(&output, aws_byte_cursor_from_array(edk->provider_id.buffer, edk->provider_id.len))) goto WRITE_ERR;
325333

326334
if (!aws_byte_buf_write_be16(&output, (uint16_t)edk->provider_info.len)) goto WRITE_ERR;
327-
if (!aws_byte_buf_write_from_whole_buffer(&output, edk->provider_info)) goto WRITE_ERR;
335+
if (!aws_byte_buf_write_from_whole_cursor(&output, aws_byte_cursor_from_array(edk->provider_info.buffer, edk->provider_info.len))) goto WRITE_ERR;
328336

329337
if (!aws_byte_buf_write_be16(&output, (uint16_t)edk->ciphertext.len)) goto WRITE_ERR;
330-
if (!aws_byte_buf_write_from_whole_buffer(&output, edk->ciphertext)) goto WRITE_ERR;
338+
if (!aws_byte_buf_write_from_whole_cursor(&output, aws_byte_cursor_from_array(edk->ciphertext.buffer,edk->ciphertext.len))) goto WRITE_ERR;
331339
}
332340

333341
if (!aws_byte_buf_write_u8(
@@ -339,8 +347,8 @@ int aws_cryptosdk_hdr_write(
339347
if (!aws_byte_buf_write_u8(&output, (uint8_t)hdr->iv.len)) goto WRITE_ERR;
340348
if (!aws_byte_buf_write_be32(&output, hdr->frame_len)) goto WRITE_ERR;
341349

342-
if (!aws_byte_buf_write_from_whole_buffer(&output, hdr->iv)) goto WRITE_ERR;
343-
if (!aws_byte_buf_write_from_whole_buffer(&output, hdr->auth_tag)) goto WRITE_ERR;
350+
if (!aws_byte_buf_write_from_whole_cursor(&output, aws_byte_cursor_from_array(hdr->iv.buffer, hdr->iv.len))) goto WRITE_ERR;
351+
if (!aws_byte_buf_write_from_whole_cursor(&output, aws_byte_cursor_from_array(hdr->auth_tag.buffer, hdr->auth_tag.len))) goto WRITE_ERR;
344352

345353
*bytes_written = output.len;
346354
return AWS_OP_SUCCESS;

source/materials.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ struct aws_cryptosdk_dec_materials *aws_cryptosdk_dec_materials_new(
5858
if (!dec_mat) return NULL;
5959
dec_mat->alloc = alloc;
6060
dec_mat->unencrypted_data_key.buffer = NULL;
61+
dec_mat->unencrypted_data_key.len = 0;
62+
dec_mat->unencrypted_data_key.capacity = 0;
6163
dec_mat->unencrypted_data_key.allocator = NULL;
6264
dec_mat->alg = alg;
6365
dec_mat->signctx = NULL;
@@ -102,7 +104,9 @@ int aws_cryptosdk_keyring_on_encrypt(
102104
/* Postcondition: If this keyring generated data key, it must be the right length. */
103105
if (!precall_data_key_buf.buffer && unencrypted_data_key->buffer) {
104106
const struct aws_cryptosdk_alg_properties *props = aws_cryptosdk_alg_props(alg);
105-
if (unencrypted_data_key->len != props->data_key_len) return aws_raise_error(AWS_CRYPTOSDK_ERR_BAD_STATE);
107+
if (unencrypted_data_key->len != props->data_key_len) {
108+
return aws_raise_error(AWS_CRYPTOSDK_ERR_BAD_STATE);
109+
}
106110
}
107111

108112
/* Postcondition: If data key was generated before call, byte buffer must not have been

source/raw_rsa_keyring.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ static int raw_rsa_keyring_on_decrypt(
130130
if (aws_cryptosdk_rsa_decrypt(
131131
unencrypted_data_key,
132132
request_alloc,
133-
aws_byte_cursor_from_buf(&edk->ciphertext),
133+
aws_byte_cursor_from_array(edk->ciphertext.buffer, edk->ciphertext.len),
134134
self->rsa_private_key_pem,
135135
self->rsa_padding_mode)) {
136136
/* We are here either because of a ciphertext mismatch

source/session_decrypt.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,15 +226,15 @@ int aws_cryptosdk_priv_try_decrypt_body(
226226
}
227227

228228
// Before we go further, do we have enough room to place the plaintext?
229-
struct aws_byte_buf output;
229+
struct aws_byte_buf output = {.buffer = 0, .len = 0, .capacity = 0, .allocator = NULL};
230230
if (!aws_byte_buf_advance(poutput, &output, session->output_size_estimate)) {
231231
*pinput = input_rollback;
232232
// No progress due to not enough plaintext output space.
233233
return AWS_OP_SUCCESS;
234234
}
235235

236236
// We have everything we need, try to decrypt
237-
struct aws_byte_cursor ciphertext_cursor = aws_byte_cursor_from_buf(&frame.ciphertext);
237+
struct aws_byte_cursor ciphertext_cursor = aws_byte_cursor_from_array(frame.ciphertext.buffer, frame.ciphertext.len);
238238
int rv = aws_cryptosdk_decrypt_body(
239239
session->alg_props,
240240
&output,

tests/unit/t_header.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ void set_aad_tbl(struct aws_cryptosdk_hdr *hdr, struct aws_cryptosdk_hdr_aad *aa
152152
}
153153

154154
static struct aws_cryptosdk_hdr test_header_1_hdr() {
155+
struct aws_allocator *allocator = aws_default_allocator();
156+
155157
struct aws_cryptosdk_hdr test_header_1_hdr = {
156158
.alg_id = ALG_AES128_GCM_IV12_TAG16_HKDF_SHA256_ECDSA_P256,
157159
.frame_len = 0x1000,
@@ -177,8 +179,9 @@ static struct aws_cryptosdk_hdr test_header_1_hdr() {
177179
// .edk_tbl = test_header_1_edk_tbl,
178180
.auth_len = sizeof(test_header_1) - 29 // not used by aws_cryptosdk_hdr_size/write
179181
};
180-
181-
test_header_1_hdr.alloc = aws_default_allocator();
182+
test_header_1_hdr.iv = aws_byte_buf_from_array(test_header_1_iv_arr,sizeof(test_header_1_iv_arr));
183+
test_header_1_hdr.auth_tag = aws_byte_buf_from_array(test_header_1_auth_tag_arr,sizeof(test_header_1_auth_tag_arr));
184+
test_header_1_hdr.alloc = allocator;
182185

183186
SET_EDK_TBL(&test_header_1_hdr, test_header_1_edk_tbl);
184187
SET_AAD_TBL(&test_header_1_hdr, test_header_1_aad_tbl);

tests/unit/t_raw_aes_keyring_decrypt.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,7 @@ int decrypt_data_key_test_vectors() {
172172

173173
if (corrupt_enc_ctx) {
174174
TEST_ASSERT_ADDR_NULL(unencrypted_data_key.buffer);
175-
} else {
176-
TEST_ASSERT_ADDR_NOT_NULL(unencrypted_data_key.buffer);
175+
}else if(unencrypted_data_key.buffer){
177176

178177
struct aws_byte_buf known_answer = aws_byte_buf_from_array(tv->data_key, tv->data_key_len);
179178
TEST_ASSERT(aws_byte_buf_eq(&unencrypted_data_key, &known_answer));

0 commit comments

Comments
 (0)