From 6c2b657bfe6991224c665bc4c8e93fdcad8262b7 Mon Sep 17 00:00:00 2001 From: Michael Rash Date: Thu, 21 Feb 2013 22:44:33 -0500 Subject: [PATCH] [libfko] free dynamically allocated resources for multiple libfko fcn calls --- lib/fko_decode.c | 25 ++++++++++++++++++++++++- lib/fko_encryption.c | 3 +++ lib/fko_funcs.c | 9 ++++++--- lib/fko_hmac.c | 3 +++ 4 files changed, 36 insertions(+), 4 deletions(-) diff --git a/lib/fko_decode.c b/lib/fko_decode.c index fc5a5ecf..5b1da332 100644 --- a/lib/fko_decode.c +++ b/lib/fko_decode.c @@ -98,6 +98,9 @@ fko_decode_spa_data(fko_ctx_t ctx) if (ctx->encoded_msg_len - t_size < 0) return(FKO_ERROR_INVALID_DATA); + if(ctx->digest != NULL) + free(ctx->digest); + /* Copy the digest into the context and terminate the encoded data * at that point so the original digest is not part of the * encoded string. @@ -142,7 +145,6 @@ fko_decode_spa_data(fko_ctx_t ctx) case FKO_DIGEST_SHA512: sha512_base64(tbuf, (unsigned char*)ctx->encoded_msg, ctx->encoded_msg_len); break; - } /* We give up here if the computed digest does not match the @@ -166,6 +168,9 @@ fko_decode_spa_data(fko_ctx_t ctx) return(FKO_ERROR_INVALID_DATA); } + if(ctx->rand_val != NULL) + free(ctx->rand_val); + ctx->rand_val = calloc(1, FKO_RAND_VAL_SIZE+1); if(ctx->rand_val == NULL) { @@ -192,6 +197,9 @@ fko_decode_spa_data(fko_ctx_t ctx) strlcpy(tbuf, ndx, t_size+1); + if(ctx->username != NULL) + free(ctx->username); + ctx->username = malloc(t_size+1); /* Yes, more than we need */ if(ctx->username == NULL) { @@ -250,6 +258,9 @@ fko_decode_spa_data(fko_ctx_t ctx) return(FKO_ERROR_INVALID_DATA); } + if(ctx->version != NULL) + free(ctx->version); + ctx->version = malloc(t_size+1); if(ctx->version == NULL) { @@ -301,6 +312,9 @@ fko_decode_spa_data(fko_ctx_t ctx) strlcpy(tbuf, ndx, t_size+1); + if(ctx->message != NULL) + free(ctx->message); + ctx->message = malloc(t_size+1); /* Yes, more than we need */ if(ctx->message == NULL) { @@ -357,6 +371,9 @@ fko_decode_spa_data(fko_ctx_t ctx) strlcpy(tbuf, ndx, t_size+1); + if(ctx->nat_access != NULL) + free(ctx->nat_access); + ctx->nat_access = malloc(t_size+1); /* Yes, more than we need */ if(ctx->nat_access == NULL) { @@ -398,6 +415,9 @@ fko_decode_spa_data(fko_ctx_t ctx) { strlcpy(tbuf, ndx, t_size+1); + if(ctx->server_auth != NULL) + free(ctx->server_auth); + ctx->server_auth = malloc(t_size+1); /* Yes, more than we need */ if(ctx->server_auth == NULL) { @@ -442,6 +462,9 @@ fko_decode_spa_data(fko_ctx_t ctx) */ strlcpy(tbuf, ndx, t_size+1); + if(ctx->server_auth != NULL) + free(ctx->server_auth); + ctx->server_auth = malloc(t_size+1); /* Yes, more than we need */ if(ctx->server_auth == NULL) { diff --git a/lib/fko_encryption.c b/lib/fko_encryption.c index 19ef1d3c..0a7d1e6b 100644 --- a/lib/fko_encryption.c +++ b/lib/fko_encryption.c @@ -149,6 +149,9 @@ _rijndael_decrypt(fko_ctx_t ctx, return(FKO_ERROR_INVALID_DATA); } + if(ctx->encoded_msg != NULL) + free(ctx->encoded_msg); + /* Create a bucket for the plaintext data and decrypt the message * data into it. */ diff --git a/lib/fko_funcs.c b/lib/fko_funcs.c index 15df6cdb..d7237005 100644 --- a/lib/fko_funcs.c +++ b/lib/fko_funcs.c @@ -160,9 +160,6 @@ fko_new(fko_ctx_t *r_ctx) FKO_SET_CTX_INITIALIZED(ctx); - if(r_ctx != NULL) - fko_destroy(*r_ctx); - *r_ctx = ctx; return(FKO_SUCCESS); @@ -194,6 +191,9 @@ fko_new_with_data(fko_ctx_t *r_ctx, const char * const enc_msg, return(FKO_ERROR_INVALID_DATA); } + if(ctx->encrypted_msg != NULL) + free(ctx->encrypted_msg); + /* First, add the data to the context. */ ctx->encrypted_msg = strdup(enc_msg); @@ -492,6 +492,9 @@ fko_set_spa_data(fko_ctx_t ctx, const char * const enc_msg) if(! is_valid_encoded_msg_len(enc_msg_len)) return(FKO_ERROR_INVALID_DATA); + if(ctx->encrypted_msg != NULL) + free(ctx->encrypted_msg); + /* First, add the data to the context. */ ctx->encrypted_msg = strdup(enc_msg); diff --git a/lib/fko_hmac.c b/lib/fko_hmac.c index 9be7f470..32809385 100644 --- a/lib/fko_hmac.c +++ b/lib/fko_hmac.c @@ -166,6 +166,9 @@ int fko_calculate_hmac(fko_ctx_t ctx, b64_encode(hmac, hmac_base64, SHA256_DIGEST_LEN); strip_b64_eq(hmac_base64); + if(ctx->msg_hmac != NULL) + free(ctx->msg_hmac); + ctx->msg_hmac = strdup(hmac_base64); ctx->msg_hmac_len = strnlen(ctx->msg_hmac, SHA512_DIGEST_STR_LEN);