From 8623c7fe6e24dbac595668cb3e5af1e0e548309c Mon Sep 17 00:00:00 2001 From: frankmorgner Date: Tue, 14 Sep 2010 20:37:43 +0000 Subject: [PATCH] - some unneded functions are no longer available in header files - cleaned memory leaks in pace-tool and ccid-emulator git-svn-id: https://vsmartcard.svn.sourceforge.net/svnroot/vsmartcard@264 96b47cad-a561-4643-ad3b-153ac7d7599c --- ccid/src/ccid.c | 61 ++++++++++++++++++++++++++++++++------------ ccid/src/pace-tool.c | 13 ++++++++++ ccid/src/pace.c | 17 +++++++----- ccid/src/pace/sm.h | 4 --- ccid/src/sm.c | 7 ++--- 5 files changed, 72 insertions(+), 30 deletions(-) diff --git a/ccid/src/ccid.c b/ccid/src/ccid.c index 0836bfc..3960300 100644 --- a/ccid/src/ccid.c +++ b/ccid/src/ccid.c @@ -776,8 +776,10 @@ perform_PC_to_RDR_Secure_EstablishPACEChannel(sc_card_t *card, __le16 word; __u8 *p; - if (!abDataOut || !abDataOutLen) - return SC_ERROR_INVALID_ARGUMENTS; + if (!abDataOut || !abDataOutLen) { + sc_result = SC_ERROR_INVALID_ARGUMENTS; + goto err; + } memset(&pace_input, 0, sizeof(pace_input)); memset(&pace_output, 0, sizeof(pace_output)); @@ -785,7 +787,8 @@ perform_PC_to_RDR_Secure_EstablishPACEChannel(sc_card_t *card, if (abDatalen < parsed+1) { sc_error(ctx, "Buffer too small, could not get PinID"); - return SC_ERROR_INVALID_ARGUMENTS; + sc_result = SC_ERROR_INVALID_ARGUMENTS; + goto err; } pace_input.pin_id = abData[parsed]; parsed++; @@ -793,14 +796,16 @@ perform_PC_to_RDR_Secure_EstablishPACEChannel(sc_card_t *card, if (abDatalen < parsed+1) { sc_error(ctx, "Buffer too small, could not get lengthCHAT"); - return SC_ERROR_INVALID_ARGUMENTS; + sc_result = SC_ERROR_INVALID_ARGUMENTS; + goto err; } pace_input.chat_length = abData[parsed]; parsed++; if (abDatalen < parsed+pace_input.chat_length) { sc_error(ctx, "Buffer too small, could not get CHAT"); - return SC_ERROR_INVALID_ARGUMENTS; + sc_result = SC_ERROR_INVALID_ARGUMENTS; + goto err; } pace_input.chat = &abData[parsed]; parsed += pace_input.chat_length; @@ -812,14 +817,16 @@ perform_PC_to_RDR_Secure_EstablishPACEChannel(sc_card_t *card, if (abDatalen < parsed+1) { sc_error(ctx, "Buffer too small, could not get lengthPIN"); - return SC_ERROR_INVALID_ARGUMENTS; + sc_result = SC_ERROR_INVALID_ARGUMENTS; + goto err; } pace_input.pin_length = abData[parsed]; parsed++; if (abDatalen < parsed+pace_input.pin_length) { sc_error(ctx, "Buffer too small, could not get PIN"); - return SC_ERROR_INVALID_ARGUMENTS; + sc_result = SC_ERROR_INVALID_ARGUMENTS; + goto err; } pace_input.pin = &abData[parsed]; parsed += pace_input.pin_length; @@ -827,7 +834,8 @@ perform_PC_to_RDR_Secure_EstablishPACEChannel(sc_card_t *card, if (abDatalen < parsed+sizeof(word)) { sc_error(ctx, "Buffer too small, could not get lengthCertificateDescription"); - return SC_ERROR_INVALID_ARGUMENTS; + sc_result = SC_ERROR_INVALID_ARGUMENTS; + goto err; } memcpy(&word, &abData[parsed], sizeof word); pace_input.certificate_description_length = __le16_to_cpu(word); @@ -835,7 +843,8 @@ perform_PC_to_RDR_Secure_EstablishPACEChannel(sc_card_t *card, if (abDatalen < parsed+pace_input.certificate_description_length) { sc_error(ctx, "Buffer too small, could not get CertificateDescription"); - return SC_ERROR_INVALID_ARGUMENTS; + sc_result = SC_ERROR_INVALID_ARGUMENTS; + goto err; } pace_input.certificate_description = &abData[parsed]; parsed += pace_input.certificate_description_length; @@ -856,7 +865,7 @@ perform_PC_to_RDR_Secure_EstablishPACEChannel(sc_card_t *card, sc_result = EstablishPACEChannel(NULL, card, pace_input, &pace_output, &sctx); if (sc_result < 0) - return sc_result; + goto err; p = realloc(*abDataOut, 2 + /* Statusbytes */ @@ -864,8 +873,10 @@ perform_PC_to_RDR_Secure_EstablishPACEChannel(sc_card_t *card, 1+pace_output.recent_car_length + /* Most recent CAR */ 1+pace_output.previous_car_length + /* Previous CAR */ 2+pace_output.id_icc_length); /* identifier of the MRTD chip */ - if (!p) - return SC_ERROR_OUT_OF_MEMORY; + if (!p) { + sc_result = SC_ERROR_OUT_OF_MEMORY; + goto err; + } *abDataOut = p; @@ -879,7 +890,8 @@ perform_PC_to_RDR_Secure_EstablishPACEChannel(sc_card_t *card, if (pace_output.ef_cardaccess_length > 0xffff) { sc_error(ctx, "EF.CardAcces %u bytes too long", pace_output.ef_cardaccess_length-0xffff); - return SC_ERROR_INVALID_DATA; + sc_result = SC_ERROR_INVALID_DATA; + goto err; } word = __cpu_to_le16(pace_output.ef_cardaccess_length); memcpy(p, &word, sizeof word); @@ -893,7 +905,8 @@ perform_PC_to_RDR_Secure_EstablishPACEChannel(sc_card_t *card, if (pace_output.recent_car_length > 0xff) { sc_error(ctx, "Most recent CAR %u bytes too long", pace_output.recent_car_length-0xff); - return SC_ERROR_INVALID_DATA; + sc_result = SC_ERROR_INVALID_DATA; + goto err; } *p = pace_output.recent_car_length; p += 1; @@ -906,7 +919,8 @@ perform_PC_to_RDR_Secure_EstablishPACEChannel(sc_card_t *card, if (pace_output.previous_car_length > 0xff) { sc_error(ctx, "Most previous CAR %u bytes too long", pace_output.previous_car_length-0xff); - return SC_ERROR_INVALID_DATA; + sc_result = SC_ERROR_INVALID_DATA; + goto err; } *p = pace_output.previous_car_length; p += 1; @@ -919,7 +933,8 @@ perform_PC_to_RDR_Secure_EstablishPACEChannel(sc_card_t *card, if (pace_output.id_icc_length > 0xffff) { sc_error(ctx, "ID ICC %u bytes too long", pace_output.id_icc_length-0xffff); - return SC_ERROR_INVALID_DATA; + sc_result = SC_ERROR_INVALID_DATA; + goto err; } word = __cpu_to_le16(pace_output.id_icc_length); memcpy(p, &word, sizeof word); @@ -936,7 +951,19 @@ perform_PC_to_RDR_Secure_EstablishPACEChannel(sc_card_t *card, 1+pace_output.previous_car_length + 2+pace_output.id_icc_length; - return SC_SUCCESS; + sc_result = SC_SUCCESS; + +err: + if (pace_output.recent_car) + free(pace_output.recent_car); + if (pace_output.previous_car) + free(pace_output.previous_car); + if (pace_output.id_icc) + free(pace_output.id_icc); + if (pace_output.id_pcd) + free(pace_output.id_pcd); + + return sc_result; } static int diff --git a/ccid/src/pace-tool.c b/ccid/src/pace-tool.c index d6c70e6..4296d53 100644 --- a/ccid/src/pace-tool.c +++ b/ccid/src/pace-tool.c @@ -502,6 +502,19 @@ main (int argc, char **argv) } err: + pace_sm_ctx_clear_free(sctx.cipher_ctx); + pace_sm_ctx_clear_free(tmpctx.cipher_ctx); + if (pace_output.ef_cardaccess) + free(pace_output.ef_cardaccess); + if (pace_output.recent_car) + free(pace_output.recent_car); + if (pace_output.previous_car) + free(pace_output.previous_car); + if (pace_output.id_icc) + free(pace_output.id_icc); + if (pace_output.id_pcd) + free(pace_output.id_pcd); + sc_disconnect_card(card, 0); sc_release_context(ctx); diff --git a/ccid/src/pace.c b/ccid/src/pace.c index 3e4b18a..8bb2bbd 100644 --- a/ccid/src/pace.c +++ b/ccid/src/pace.c @@ -1280,7 +1280,8 @@ reset_ssc(struct pace_sm_ctx *psmctx) return SC_SUCCESS; } -int pace_sm_encrypt(sc_card_t *card, const struct sm_ctx *ctx, +static int +pace_sm_encrypt(sc_card_t *card, const struct sm_ctx *ctx, const u8 *data, size_t datalen, u8 **enc) { BUF_MEM *encbuf = NULL, *databuf = NULL; @@ -1322,7 +1323,8 @@ err: return r; } -int pace_sm_decrypt(sc_card_t *card, const struct sm_ctx *ctx, +static int +pace_sm_decrypt(sc_card_t *card, const struct sm_ctx *ctx, const u8 *enc, size_t enclen, u8 **data) { BUF_MEM *encbuf = NULL, *databuf = NULL; @@ -1364,7 +1366,8 @@ err: return r; } -int pace_sm_authenticate(sc_card_t *card, const struct sm_ctx *ctx, +static int +pace_sm_authenticate(sc_card_t *card, const struct sm_ctx *ctx, const u8 *data, size_t datalen, u8 **macdata) { BUF_MEM *macbuf = NULL; @@ -1405,7 +1408,8 @@ err: return r; } -int pace_sm_verify_authentication(sc_card_t *card, const struct sm_ctx *ctx, +static int +pace_sm_verify_authentication(sc_card_t *card, const struct sm_ctx *ctx, const u8 *mac, size_t maclen, const u8 *macdata, size_t macdatalen) { @@ -1448,14 +1452,15 @@ err: return r; } -int pace_sm_pre_transmit(sc_card_t *card, const struct sm_ctx *ctx, +static int +pace_sm_pre_transmit(sc_card_t *card, const struct sm_ctx *ctx, sc_apdu_t *apdu) { SC_FUNC_RETURN(card->ctx, SC_LOG_TYPE_DEBUG, increment_ssc(ctx->cipher_ctx)); } -int pace_sm_post_transmit(sc_card_t *card, const struct sm_ctx *ctx, +static int pace_sm_post_transmit(sc_card_t *card, const struct sm_ctx *ctx, sc_apdu_t *sm_apdu) { SC_FUNC_RETURN(card->ctx, SC_LOG_TYPE_DEBUG, diff --git a/ccid/src/pace/sm.h b/ccid/src/pace/sm.h index 537f86d..80dc6a9 100644 --- a/ccid/src/pace/sm.h +++ b/ccid/src/pace/sm.h @@ -56,10 +56,6 @@ struct sm_ctx { int sm_transmit_apdu(const struct sm_ctx *sctx, sc_card_t *card, sc_apdu_t *apdu); -int add_iso_pad(const u8 *data, size_t datalen, int block_size, u8 **padded); -int add_padding(const struct sm_ctx *ctx, const u8 *data, size_t datalen, - u8 **padded); - #ifdef __cplusplus } #endif diff --git a/ccid/src/sm.c b/ccid/src/sm.c index ca63079..2ed57f0 100644 --- a/ccid/src/sm.c +++ b/ccid/src/sm.c @@ -44,7 +44,7 @@ static const struct sc_asn1_entry c_sm_rapdu[] = { { NULL, 0, 0, 0, NULL, NULL } }; -int +static int add_iso_pad(const u8 *data, size_t datalen, int block_size, u8 **padded) { u8 *p; @@ -72,7 +72,7 @@ add_iso_pad(const u8 *data, size_t datalen, int block_size, u8 **padded) return p_len; } -int +static int add_padding(const struct sm_ctx *ctx, const u8 *data, size_t datalen, u8 **padded) { @@ -95,7 +95,8 @@ add_padding(const struct sm_ctx *ctx, const u8 *data, size_t datalen, } } -int no_padding(u8 padding_indicator, const u8 *data, size_t datalen) +static int +no_padding(u8 padding_indicator, const u8 *data, size_t datalen) { if (!datalen || !data) return SC_ERROR_INVALID_ARGUMENTS;