Fixed memory leaks

This commit is contained in:
Sansar Choinyambuu
2011-11-28 14:34:20 +01:00
committed by Andreas Steffen
parent 2f229f10b1
commit 97ebeaa45c
4 changed files with 112 additions and 107 deletions
@@ -72,7 +72,7 @@ static pts_dh_group_t supported_dh_groups = 0;
* High Entropy Random Data * High Entropy Random Data
* used in calculation of shared secret for the assessment session * used in calculation of shared secret for the assessment session
*/ */
static chunk_t responder_nonce; static char *responder_nonce = NULL;
/** /**
* see section 3.7.1 of TCG TNC IF-IMC Specification 1.2 * see section 3.7.1 of TCG TNC IF-IMC Specification 1.2
@@ -82,6 +82,8 @@ TNC_Result TNC_IMC_Initialize(TNC_IMCID imc_id,
TNC_Version max_version, TNC_Version max_version,
TNC_Version *actual_version) TNC_Version *actual_version)
{ {
rng_t *rng;
if (imc_attestation) if (imc_attestation)
{ {
DBG1(DBG_IMC, "IMC \"%s\" has already been initialized", imc_name); DBG1(DBG_IMC, "IMC \"%s\" has already been initialized", imc_name);
@@ -104,6 +106,15 @@ TNC_Result TNC_IMC_Initialize(TNC_IMCID imc_id,
libpts_init(); libpts_init();
/* create a responder nonce */
responder_nonce = (char*)malloc(NONCE_LEN);
rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK);
if (rng)
{
rng->get_bytes(rng, NONCE_LEN, responder_nonce);
rng->destroy(rng);
}
if (min_version > TNC_IFIMC_VERSION_1 || max_version < TNC_IFIMC_VERSION_1) if (min_version > TNC_IFIMC_VERSION_1 || max_version < TNC_IFIMC_VERSION_1)
{ {
DBG1(DBG_IMC, "no common IF-IMC version"); DBG1(DBG_IMC, "no common IF-IMC version");
@@ -296,9 +307,7 @@ TNC_Result TNC_IMC_ReceiveMessage(TNC_IMCID imc_id,
tcg_pts_attr_dh_nonce_params_req_t *attr_cast; tcg_pts_attr_dh_nonce_params_req_t *attr_cast;
u_int8_t min_nonce_len; u_int8_t min_nonce_len;
pts_dh_group_t offered_dh_groups, selected_dh_group; pts_dh_group_t offered_dh_groups, selected_dh_group;
rng_t *rng;
chunk_t responder_pub_val; chunk_t responder_pub_val;
char buf[NONCE_LEN];
attr_cast = (tcg_pts_attr_dh_nonce_params_req_t*)attr; attr_cast = (tcg_pts_attr_dh_nonce_params_req_t*)attr;
min_nonce_len = attr_cast->get_min_nonce_len(attr_cast); min_nonce_len = attr_cast->get_min_nonce_len(attr_cast);
@@ -355,18 +364,10 @@ TNC_Result TNC_IMC_ReceiveMessage(TNC_IMCID imc_id,
} }
responder_pub_val = pts->get_my_pub_val(pts); responder_pub_val = pts->get_my_pub_val(pts);
/* create a responder nonce */
rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK);
if (rng)
{
rng->get_bytes(rng, sizeof(buf), buf);
rng->destroy(rng);
}
responder_nonce = chunk_create(buf, sizeof(buf));
attr = tcg_pts_attr_dh_nonce_params_resp_create(NONCE_LEN, attr = tcg_pts_attr_dh_nonce_params_resp_create(NONCE_LEN,
selected_dh_group, supported_algorithms, selected_dh_group, supported_algorithms,
responder_nonce, responder_pub_val); chunk_create(responder_nonce, NONCE_LEN),
responder_pub_val);
attr_list->insert_last(attr_list, attr); attr_list->insert_last(attr_list, attr);
break; break;
} }
@@ -375,7 +376,7 @@ TNC_Result TNC_IMC_ReceiveMessage(TNC_IMCID imc_id,
tcg_pts_attr_dh_nonce_finish_t *attr_cast; tcg_pts_attr_dh_nonce_finish_t *attr_cast;
u_int8_t nonce_len; u_int8_t nonce_len;
pts_meas_algorithms_t selected_algorithm; pts_meas_algorithms_t selected_algorithm;
chunk_t initiator_nonce, initiator_pub_val; chunk_t initiator_nonce, initiator_pub_val, responder_non;
attr_cast = (tcg_pts_attr_dh_nonce_finish_t*)attr; attr_cast = (tcg_pts_attr_dh_nonce_finish_t*)attr;
nonce_len = attr_cast->get_nonce_len(attr_cast); nonce_len = attr_cast->get_nonce_len(attr_cast);
@@ -391,12 +392,14 @@ TNC_Result TNC_IMC_ReceiveMessage(TNC_IMCID imc_id,
selected_algorithm = attr_cast->get_hash_algo(attr_cast); selected_algorithm = attr_cast->get_hash_algo(attr_cast);
initiator_pub_val = attr_cast->get_initiator_pub_val(attr_cast); initiator_pub_val = attr_cast->get_initiator_pub_val(attr_cast);
initiator_nonce = attr_cast->get_initiator_nonce(attr_cast); initiator_nonce = attr_cast->get_initiator_nonce(attr_cast);
responder_non = chunk_create(responder_nonce, NONCE_LEN);
DBG3(DBG_IMC, "Initiator nonce: %B", &initiator_nonce); DBG3(DBG_IMC, "Initiator nonce: %B", &initiator_nonce);
DBG3(DBG_IMC, "Responder nonce: %B", &responder_nonce); DBG3(DBG_IMC, "Responder nonce: %B", &responder_non);
pts->set_other_pub_val(pts, initiator_pub_val); pts->set_other_pub_val(pts, initiator_pub_val);
if (!pts->calculate_secret(pts, initiator_nonce, if (!pts->calculate_secret(pts, initiator_nonce,
responder_nonce, selected_algorithm)) responder_non, selected_algorithm))
{ {
return TNC_RESULT_FATAL; return TNC_RESULT_FATAL;
} }
@@ -792,7 +795,7 @@ TNC_Result TNC_IMC_Terminate(TNC_IMCID imc_id)
return TNC_RESULT_NOT_INITIALIZED; return TNC_RESULT_NOT_INITIALIZED;
} }
free(responder_nonce.ptr); free(responder_nonce);
libpts_deinit(); libpts_deinit();
imc_attestation->destroy(imc_attestation); imc_attestation->destroy(imc_attestation);
@@ -76,7 +76,7 @@ static pts_dh_group_t supported_dh_groups = 0;
* High Entropy Random Data * High Entropy Random Data
* used in calculation of shared secret for the assessment session * used in calculation of shared secret for the assessment session
*/ */
static chunk_t initiator_nonce; static char *initiator_nonce = NULL;
/** /**
* PTS file measurement database * PTS file measurement database
@@ -107,6 +107,7 @@ TNC_Result TNC_IMV_Initialize(TNC_IMVID imv_id,
TNC_Version *actual_version) TNC_Version *actual_version)
{ {
char *hash_alg, *dh_group, *uri, *cadir; char *hash_alg, *dh_group, *uri, *cadir;
rng_t *rng;
if (imv_attestation) if (imv_attestation)
{ {
@@ -130,6 +131,15 @@ TNC_Result TNC_IMV_Initialize(TNC_IMVID imv_id,
libpts_init(); libpts_init();
/* Create a initiator nonce */
initiator_nonce = (char*)malloc(NONCE_LEN);
rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK);
if (rng)
{
rng->get_bytes(rng, NONCE_LEN, initiator_nonce);
rng->destroy(rng);
}
if (min_version > TNC_IFIMV_VERSION_1 || max_version < TNC_IFIMV_VERSION_1) if (min_version > TNC_IFIMV_VERSION_1 || max_version < TNC_IFIMV_VERSION_1)
{ {
DBG1(DBG_IMV, "no common IF-IMV version"); DBG1(DBG_IMV, "no common IF-IMV version");
@@ -257,7 +267,15 @@ static TNC_Result send_message(TNC_ConnectionID connection_id)
msg = pa_tnc_msg_create(); msg = pa_tnc_msg_create();
switch_state: /* Jump to Measurement state if IMC has no TPM */
if (handshake_state == IMV_ATTESTATION_STATE_TPM_INIT &&
!(pts->get_proto_caps(pts) & PTS_PROTO_CAPS_T))
{
handshake_state = IMV_ATTESTATION_STATE_MEAS;
DBG3(DBG_IMV, "TPM is not available on IMC side, ",
"jumping to measurement phase");
}
/* Switch on the attribute type IMV has received */ /* Switch on the attribute type IMV has received */
switch (handshake_state) switch (handshake_state)
{ {
@@ -282,15 +300,6 @@ static TNC_Result send_message(TNC_ConnectionID connection_id)
} }
case IMV_ATTESTATION_STATE_TPM_INIT: case IMV_ATTESTATION_STATE_TPM_INIT:
{ {
/* Jump to Measurement state if IMC has no TPM */
if(!(pts->get_proto_caps(pts) & PTS_PROTO_CAPS_T))
{
handshake_state = IMV_ATTESTATION_STATE_MEAS;
DBG3(DBG_IMV, "TPM is not available on IMC side, ",
"jumping to measurement phase");
goto switch_state;
}
if (!dh_nonce_req_sent) if (!dh_nonce_req_sent)
{ {
/* Send DH nonce parameters request attribute */ /* Send DH nonce parameters request attribute */
@@ -308,7 +317,8 @@ static TNC_Result send_message(TNC_ConnectionID connection_id)
selected_algorithm = pts->get_meas_algorithm(pts); selected_algorithm = pts->get_meas_algorithm(pts);
initiator_pub_val = pts->get_my_pub_val(pts); initiator_pub_val = pts->get_my_pub_val(pts);
attr = tcg_pts_attr_dh_nonce_finish_create(NONCE_LEN, attr = tcg_pts_attr_dh_nonce_finish_create(NONCE_LEN,
selected_algorithm, initiator_nonce, selected_algorithm,
chunk_create(initiator_nonce, NONCE_LEN),
initiator_pub_val); initiator_pub_val);
attr->set_noskip_flag(attr, TRUE); attr->set_noskip_flag(attr, TRUE);
msg->add_attribute(msg, attr); msg->add_attribute(msg, attr);
@@ -560,10 +570,7 @@ TNC_Result TNC_IMV_ReceiveMessage(TNC_IMVID imv_id,
u_int8_t nonce_len; u_int8_t nonce_len;
pts_dh_group_t dh_group; pts_dh_group_t dh_group;
pts_meas_algorithms_t offered_algorithms, selected_algorithm; pts_meas_algorithms_t offered_algorithms, selected_algorithm;
chunk_t responder_nonce; chunk_t responder_nonce, initiator_non, responder_pub_val;
chunk_t responder_pub_val;
rng_t *rng;
char buf[NONCE_LEN];
attr_cast = (tcg_pts_attr_dh_nonce_params_resp_t*)attr; attr_cast = (tcg_pts_attr_dh_nonce_params_resp_t*)attr;
@@ -606,6 +613,7 @@ TNC_Result TNC_IMV_ReceiveMessage(TNC_IMVID imv_id,
selected_algorithm = pts->get_meas_algorithm(pts); selected_algorithm = pts->get_meas_algorithm(pts);
responder_nonce = attr_cast->get_responder_nonce(attr_cast); responder_nonce = attr_cast->get_responder_nonce(attr_cast);
responder_pub_val = attr_cast->get_responder_pub_val(attr_cast); responder_pub_val = attr_cast->get_responder_pub_val(attr_cast);
initiator_non = chunk_create(initiator_nonce, NONCE_LEN);
/* Calculate secret assessment value */ /* Calculate secret assessment value */
if (!pts->create_dh(pts, dh_group)) if (!pts->create_dh(pts, dh_group))
@@ -614,19 +622,10 @@ TNC_Result TNC_IMV_ReceiveMessage(TNC_IMVID imv_id,
} }
pts->set_other_pub_val(pts, responder_pub_val); pts->set_other_pub_val(pts, responder_pub_val);
/* Create a initiator nonce */ DBG3(DBG_IMV, "Initiator nonce: %B", &initiator_non);
rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK);
if (rng)
{
rng->get_bytes(rng, sizeof(buf), buf);
rng->destroy(rng);
}
initiator_nonce = chunk_create(buf, sizeof(buf));
DBG3(DBG_IMV, "Initiator nonce: %B", &initiator_nonce);
DBG3(DBG_IMV, "Responder nonce: %B", &responder_nonce); DBG3(DBG_IMV, "Responder nonce: %B", &responder_nonce);
if (!pts->calculate_secret(pts, initiator_nonce, if (!pts->calculate_secret(pts, initiator_non,
responder_nonce, selected_algorithm)) responder_nonce, selected_algorithm))
{ {
return TNC_RESULT_FATAL; return TNC_RESULT_FATAL;
} }
@@ -827,10 +826,13 @@ TNC_Result TNC_IMV_ReceiveMessage(TNC_IMVID imv_id,
pa_tnc_msg->build(pa_tnc_msg); pa_tnc_msg->build(pa_tnc_msg);
result = imv_attestation->send_message(imv_attestation, connection_id, result = imv_attestation->send_message(imv_attestation, connection_id,
pa_tnc_msg->get_encoding(pa_tnc_msg)); pa_tnc_msg->get_encoding(pa_tnc_msg));
pa_tnc_msg->destroy(pa_tnc_msg); pa_tnc_msg->destroy(pa_tnc_msg);
attr_list->destroy(attr_list); attr_list->destroy(attr_list);
return result; return result;
} }
DESTROY_IF(attr_list);
if (attestation_state->get_handshake_state(attestation_state) & if (attestation_state->get_handshake_state(attestation_state) &
IMV_ATTESTATION_STATE_END) IMV_ATTESTATION_STATE_END)
@@ -921,7 +923,7 @@ TNC_Result TNC_IMV_Terminate(TNC_IMVID imv_id)
} }
DESTROY_IF(pts_db); DESTROY_IF(pts_db);
DESTROY_IF(pts_credmgr); DESTROY_IF(pts_credmgr);
free(initiator_nonce.ptr); free(initiator_nonce);
libpts_deinit(); libpts_deinit();