From 8fc679bf4062bc1d74092ed51eab663620dabaef Mon Sep 17 00:00:00 2001 From: Viktor Tarasov Date: Sat, 2 Mar 2013 19:29:14 +0100 Subject: [PATCH] libopensc: invalid usage of SC_ERROR_MEMORY_FAILURE Initiated by discussion in https://github.com/OpenSC/OpenSC/pull/134 . SC_ERROR_MEMORY_FAILURE has to be used as a resulting code of the card related operations, and not as result of the memory allocation problems. New 'warning' category of SC_ERRORs introduced -- SC_WARNING_xx . Error text and SC_ERROR associated with return codes 6300 and 6200 has been changed. --- src/libopensc/card.c | 2 +- src/libopensc/errors.c | 10 ++++++++++ src/libopensc/errors.h | 3 +++ src/libopensc/iasecc-sdo.c | 4 ++-- src/libopensc/iso7816.c | 6 +++--- src/libopensc/pkcs15-oberthur.c | 10 +++++----- src/pkcs15init/pkcs15-iasecc.c | 24 ++++++++++++------------ src/smm/sm-global-platform.c | 2 +- 8 files changed, 37 insertions(+), 24 deletions(-) diff --git a/src/libopensc/card.c b/src/libopensc/card.c index 53cb1f8a..7d348d76 100644 --- a/src/libopensc/card.c +++ b/src/libopensc/card.c @@ -1187,7 +1187,7 @@ sc_card_sm_load(struct sc_card *card, const char *module_path, const char *in_mo } if (!module) - return SC_ERROR_MEMORY_FAILURE; + return SC_ERROR_OUT_OF_MEMORY; sc_log(ctx, "try to load SM module '%s'", module); do { diff --git a/src/libopensc/errors.c b/src/libopensc/errors.c index adc55b87..f89eb227 100644 --- a/src/libopensc/errors.c +++ b/src/libopensc/errors.c @@ -137,6 +137,12 @@ const char *sc_strerror(int error) }; const int sm_base = -SC_ERROR_SM; + const char *warnings[] = { + "Warning: no information given", + "Warning: file filled up by last write", + }; + const int warn_base = -SC_WARNING; + const char *misc_errors[] = { "Unknown error", "PKCS#15 compatible smart card not found", @@ -156,6 +162,10 @@ const char *sc_strerror(int error) errors = misc_errors; count = DIM(misc_errors); err_base = misc_base; + } else if (error >= warn_base) { + errors = warnings; + count = DIM(warnings); + err_base = warn_base; } else if (error >= sm_base) { errors = sm_errors; count = DIM(sm_errors); diff --git a/src/libopensc/errors.h b/src/libopensc/errors.h index 92403144..8bab5d70 100644 --- a/src/libopensc/errors.h +++ b/src/libopensc/errors.h @@ -124,6 +124,9 @@ extern "C" { #define SC_ERROR_SM_SESSION_ALREADY_ACTIVE -1611 #define SC_ERROR_SM_INVALID_CHECKSUM -1612 +/* Warnings */ +#define SC_WARNING -1700 +#define SC_WARNING_FILE_FILLED -1701 /* Errors that do not fit the categories above */ #define SC_ERROR_UNKNOWN -1900 diff --git a/src/libopensc/iasecc-sdo.c b/src/libopensc/iasecc-sdo.c index 8a3e39bd..ea5aa971 100644 --- a/src/libopensc/iasecc-sdo.c +++ b/src/libopensc/iasecc-sdo.c @@ -763,7 +763,7 @@ iasecc_sdo_allocate_and_parse(struct sc_card *card, unsigned char *data, size_t sdo = calloc(1, sizeof(struct iasecc_sdo)); if (!sdo) - return SC_ERROR_MEMORY_FAILURE; + return SC_ERROR_OUT_OF_MEMORY; sdo->sdo_class = *(data + 1) & 0x7F; sdo->sdo_ref = *(data + 2) & 0x3F; @@ -823,7 +823,7 @@ iasecc_update_blob(struct sc_context *ctx, struct iasecc_extended_tlv *tlv, pp = realloc(*blob, *blob_size + sz); if (!pp) - LOG_FUNC_RETURN(ctx, SC_ERROR_MEMORY_FAILURE); + LOG_FUNC_RETURN(ctx, SC_ERROR_OUT_OF_MEMORY); if (tlv->tag > 0xFF) *(pp + *blob_size + offs++) = (tlv->tag >> 8) & 0xFF; diff --git a/src/libopensc/iso7816.c b/src/libopensc/iso7816.c index 9d0d1c6b..a7c672dc 100644 --- a/src/libopensc/iso7816.c +++ b/src/libopensc/iso7816.c @@ -30,14 +30,14 @@ #include "iso7816.h" static const struct sc_card_error iso7816_errors[] = { - { 0x6200, SC_ERROR_MEMORY_FAILURE, "State of non-volatile memory unchanged" }, + { 0x6200, SC_WARNING, "warning: no information given, non-volatile memory is unchanged" }, { 0x6281, SC_ERROR_CORRUPTED_DATA, "Part of returned data may be corrupted" }, { 0x6282, SC_ERROR_FILE_END_REACHED, "End of file/record reached before reading Le bytes" }, { 0x6283, SC_ERROR_CARD_CMD_FAILED, "Selected file invalidated" }, { 0x6284, SC_ERROR_CARD_CMD_FAILED, "FCI not formatted according to ISO 7816-4" }, - { 0x6300, SC_ERROR_MEMORY_FAILURE, "State of non-volatile memory changed" }, - { 0x6381, SC_ERROR_CARD_CMD_FAILED, "File filled up by last write" }, + { 0x6300, SC_WARNING, "warning: no information given, non-volatile memory has changed" }, + { 0x6381, SC_WARNING_FILE_FILLED, "warning: file filled up by last write" }, { 0x6581, SC_ERROR_MEMORY_FAILURE, "Memory failure" }, diff --git a/src/libopensc/pkcs15-oberthur.c b/src/libopensc/pkcs15-oberthur.c index b723111b..bbdd14fd 100644 --- a/src/libopensc/pkcs15-oberthur.c +++ b/src/libopensc/pkcs15-oberthur.c @@ -199,14 +199,14 @@ sc_oberthur_get_certificate_authority(struct sc_pkcs15_der *der, int *out_author buf_mem.data = malloc(der->len); if (!buf_mem.data) - return SC_ERROR_MEMORY_FAILURE; + return SC_ERROR_OUT_OF_MEMORY; memcpy(buf_mem.data, der->value, der->len); buf_mem.max = buf_mem.length = der->len; bio = BIO_new(BIO_s_mem()); if(!bio) - return SC_ERROR_MEMORY_FAILURE; + return SC_ERROR_OUT_OF_MEMORY; BIO_set_mem_buf(bio, &buf_mem, BIO_NOCLOSE); x = d2i_X509_bio(bio, 0); @@ -259,7 +259,7 @@ sc_oberthur_read_file(struct sc_pkcs15_card *p15card, const char *in_path, *out = calloc(sz, 1); if (*out == NULL) - SC_TEST_RET(card->ctx, SC_LOG_DEBUG_NORMAL, SC_ERROR_MEMORY_FAILURE, "Cannot read oberthur file"); + SC_TEST_RET(card->ctx, SC_LOG_DEBUG_NORMAL, SC_ERROR_OUT_OF_MEMORY, "Cannot read oberthur file"); if (file->ef_structure == SC_FILE_EF_TRANSPARENT) { rv = sc_read_binary(card, 0, *out, sz, 0); @@ -395,7 +395,7 @@ sc_oberthur_parse_containers (struct sc_pkcs15_card *p15card, cont = (struct container *)calloc(sizeof(struct container), 1); if (!cont) - return SC_ERROR_MEMORY_FAILURE; + return SC_ERROR_OUT_OF_MEMORY; cont->exchange.id_pub = *ptr * 0x100 + *(ptr + 1); ptr += 2; cont->exchange.id_prv = *ptr * 0x100 + *(ptr + 1); ptr += 2; @@ -787,7 +787,7 @@ sc_pkcs15emu_oberthur_add_prvkey(struct sc_pkcs15_card *p15card, if (len) { kinfo.subject.value = malloc(len); if (!kinfo.subject.value) - SC_TEST_RET(ctx, SC_LOG_DEBUG_NORMAL, SC_ERROR_MEMORY_FAILURE, "Failed to add private key: memory allocation error"); + SC_TEST_RET(ctx, SC_LOG_DEBUG_NORMAL, SC_ERROR_OUT_OF_MEMORY, "Failed to add private key: memory allocation error"); kinfo.subject.len = len; memcpy(kinfo.subject.value, info_blob + offs + 2, len); } diff --git a/src/pkcs15init/pkcs15-iasecc.c b/src/pkcs15init/pkcs15-iasecc.c index d14bc219..3b6970ff 100644 --- a/src/pkcs15init/pkcs15-iasecc.c +++ b/src/pkcs15init/pkcs15-iasecc.c @@ -415,7 +415,7 @@ iasecc_sdo_set_key_acls_from_profile(struct sc_profile *profile, struct sc_card sdo->docp.acls_contact.size = cntr + 1; sdo->docp.acls_contact.value = calloc(1, sdo->docp.acls_contact.size); if (!sdo->docp.acls_contact.value) - return SC_ERROR_MEMORY_FAILURE; + return SC_ERROR_OUT_OF_MEMORY; *(sdo->docp.acls_contact.value + 0) = amb; memcpy(sdo->docp.acls_contact.value + 1, scb, cntr); @@ -438,7 +438,7 @@ iasecc_sdo_allocate_prvkey(struct sc_profile *profile, struct sc_card *card, sdo = calloc(1, sizeof(struct iasecc_sdo)); if (!sdo) - LOG_TEST_RET(ctx, SC_ERROR_MEMORY_FAILURE, "Cannot allocate 'iasecc_sdo'"); + LOG_TEST_RET(ctx, SC_ERROR_OUT_OF_MEMORY, "Cannot allocate 'iasecc_sdo'"); sdo->magic = SC_CARDCTL_IASECC_SDO_MAGIC; sdo->sdo_ref = key_info->key_reference & 0x3F; @@ -458,19 +458,19 @@ iasecc_sdo_allocate_prvkey(struct sc_profile *profile, struct sc_card *card, sdo->docp.non_repudiation.value = calloc(1, 1); if (!sdo->docp.non_repudiation.value) - LOG_FUNC_RETURN(ctx, SC_ERROR_MEMORY_FAILURE); + LOG_FUNC_RETURN(ctx, SC_ERROR_OUT_OF_MEMORY); sdo->docp.non_repudiation.tag = IASECC_DOCP_TAG_NON_REPUDATION; sdo->docp.non_repudiation.size = 1; sdo->data.prv_key.compulsory.value = calloc(1, 1); if (!sdo->data.prv_key.compulsory.value) - LOG_FUNC_RETURN(ctx, SC_ERROR_MEMORY_FAILURE); + LOG_FUNC_RETURN(ctx, SC_ERROR_OUT_OF_MEMORY); sdo->data.prv_key.compulsory.tag = IASECC_SDO_PRVKEY_TAG_COMPULSORY; sdo->data.prv_key.compulsory.size = 1; sdo->docp.size.value = calloc(1, 2); if (!sdo->docp.size.value) - LOG_FUNC_RETURN(ctx, SC_ERROR_MEMORY_FAILURE); + LOG_FUNC_RETURN(ctx, SC_ERROR_OUT_OF_MEMORY); sdo->docp.size.tag = IASECC_DOCP_TAG_SIZE; sdo->docp.size.size = 2; *(sdo->docp.size.value + 0) = (sz >> 8) & 0xFF; @@ -517,7 +517,7 @@ iasecc_sdo_allocate_pubkey(struct sc_profile *profile, struct sc_card *card, str LOG_FUNC_CALLED(ctx); sdo = calloc(1, sizeof(struct iasecc_sdo)); if (!sdo) - return SC_ERROR_MEMORY_FAILURE; + return SC_ERROR_OUT_OF_MEMORY; sdo->magic = SC_CARDCTL_IASECC_SDO_MAGIC; sdo->sdo_ref = key_info->key_reference & 0x3F; @@ -533,7 +533,7 @@ iasecc_sdo_allocate_pubkey(struct sc_profile *profile, struct sc_card *card, str sdo->docp.size.value = calloc(1, 2); if (!sdo->docp.size.value) - LOG_FUNC_RETURN(ctx, SC_ERROR_MEMORY_FAILURE); + LOG_FUNC_RETURN(ctx, SC_ERROR_OUT_OF_MEMORY); sdo->docp.size.size = 2; sdo->docp.size.tag = IASECC_DOCP_TAG_SIZE; *(sdo->docp.size.value + 0) = (sz >> 8) & 0xFF; @@ -545,14 +545,14 @@ iasecc_sdo_allocate_pubkey(struct sc_profile *profile, struct sc_card *card, str else { sdo->data.pub_key.cha.value = calloc(1, 2); if (!sdo->data.pub_key.cha.value) - LOG_FUNC_RETURN(ctx, SC_ERROR_MEMORY_FAILURE); + LOG_FUNC_RETURN(ctx, SC_ERROR_OUT_OF_MEMORY); sdo->data.pub_key.cha.size = 2; sdo->data.pub_key.cha.tag = IASECC_SDO_PUBKEY_TAG_CHA; } sdo->data.pub_key.compulsory.value = calloc(1, 1); if (!sdo->data.pub_key.compulsory.value) - LOG_FUNC_RETURN(ctx, SC_ERROR_MEMORY_FAILURE); + LOG_FUNC_RETURN(ctx, SC_ERROR_OUT_OF_MEMORY); sdo->data.pub_key.compulsory.tag = IASECC_SDO_PUBKEY_TAG_COMPULSORY; sdo->data.pub_key.compulsory.size = 1; } @@ -1137,13 +1137,13 @@ iasecc_pkcs15_generate_key(struct sc_profile *profile, sc_pkcs15_card_t *p15card pubkey->u.rsa.modulus.len = sdo_pubkey->data.pub_key.n.size; pubkey->u.rsa.modulus.data = (unsigned char *) malloc(pubkey->u.rsa.modulus.len); if (!pubkey->u.rsa.modulus.data) - LOG_FUNC_RETURN(ctx, SC_ERROR_MEMORY_FAILURE); + LOG_FUNC_RETURN(ctx, SC_ERROR_OUT_OF_MEMORY); memcpy(pubkey->u.rsa.modulus.data, sdo_pubkey->data.pub_key.n.value, pubkey->u.rsa.modulus.len); pubkey->u.rsa.exponent.len = sdo_pubkey->data.pub_key.e.size; pubkey->u.rsa.exponent.data = (unsigned char *) malloc(pubkey->u.rsa.exponent.len); if (!pubkey->u.rsa.exponent.data) - LOG_FUNC_RETURN(ctx, SC_ERROR_MEMORY_FAILURE); + LOG_FUNC_RETURN(ctx, SC_ERROR_OUT_OF_MEMORY); memcpy(pubkey->u.rsa.exponent.data, sdo_pubkey->data.pub_key.e.value, pubkey->u.rsa.exponent.len); rv = sc_pkcs15_encode_pubkey(ctx, pubkey, &pubkey->data.value, &pubkey->data.len); @@ -1236,7 +1236,7 @@ iasecc_pkcs15_delete_sdo (struct sc_profile *profile, struct sc_pkcs15_card *p15 sdo = calloc(1, sizeof(struct iasecc_sdo)); if (!sdo) - return SC_ERROR_MEMORY_FAILURE; + return SC_ERROR_OUT_OF_MEMORY; sdo->magic = SC_CARDCTL_IASECC_SDO_MAGIC; sdo->sdo_class = sdo_class; diff --git a/src/smm/sm-global-platform.c b/src/smm/sm-global-platform.c index 0b0efe92..1c450be9 100644 --- a/src/smm/sm-global-platform.c +++ b/src/smm/sm-global-platform.c @@ -248,7 +248,7 @@ sm_gp_get_mac(unsigned char *key, DES_cblock *icv, block = malloc(in_len + 8); if (!block) - return SC_ERROR_MEMORY_FAILURE; + return SC_ERROR_OUT_OF_MEMORY; memcpy(block, in, in_len); memcpy(block + in_len, "\x80\0\0\0\0\0\0\0", 8);