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.
This commit is contained in:
Viktor Tarasov 2013-03-02 19:29:14 +01:00
parent 717a38bcaa
commit 8fc679bf40
8 changed files with 37 additions and 24 deletions

View File

@ -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 {

View File

@ -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);

View File

@ -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

View File

@ -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;

View File

@ -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" },

View File

@ -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);
}

View File

@ -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;

View File

@ -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);