myeid: fixed a bug in setting card->name

Fixed a bug in setting card->name in myeid_init and myeid_get_info:
The buffer containing the card name fell out of scope.
This commit is contained in:
Hannu Honkanen 2016-03-30 10:53:14 +03:00 committed by Viktor Tarasov
parent bd84e18f45
commit fe2312dd19
1 changed files with 98 additions and 93 deletions

View File

@ -47,6 +47,10 @@
#define MYEID_STATE_ACTIVATED 0x07 #define MYEID_STATE_ACTIVATED 0x07
#define MYEID_INFINEON_CHIP_ATR 0x04 #define MYEID_INFINEON_CHIP_ATR 0x04
#define MYEID_CARD_NAME_MAX_LEN 100
static const char *myeid_card_name = "MyEID";
static char card_name_buf[MYEID_CARD_NAME_MAX_LEN];
static struct sc_card_operations myeid_ops; static struct sc_card_operations myeid_ops;
static struct sc_card_driver myeid_drv = { static struct sc_card_driver myeid_drv = {
@ -121,8 +125,7 @@ static int myeid_match_card(struct sc_card *card)
static int myeid_init(struct sc_card *card) static int myeid_init(struct sc_card *card)
{ {
unsigned long flags = 0, unsigned long flags = 0, ext_flags = 0;
ext_flags = 0;
myeid_private_data_t *priv; myeid_private_data_t *priv;
u8 appletInfo[20]; u8 appletInfo[20];
size_t appletInfoLen; size_t appletInfoLen;
@ -133,7 +136,11 @@ static int myeid_init(struct sc_card *card)
const char *atrp = myeid_atrs[MYEID_INFINEON_CHIP_ATR]; const char *atrp = myeid_atrs[MYEID_INFINEON_CHIP_ATR];
LOG_FUNC_CALLED(card->ctx); LOG_FUNC_CALLED(card->ctx);
card->name = myeid_card_name;
priv = calloc(1, sizeof(myeid_private_data_t)); priv = calloc(1, sizeof(myeid_private_data_t));
if (!priv) if (!priv)
LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY); LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY);
@ -651,12 +658,12 @@ static int myeid_set_security_env_ec(sc_card_t *card, const sc_security_env_t *e
if (env->flags & SC_SEC_ENV_KEY_REF_ASYMMETRIC) if (env->flags & SC_SEC_ENV_KEY_REF_ASYMMETRIC)
{ {
sc_log(card->ctx, "asymmetric keyref not supported.\n"); sc_log(card->ctx, "asymmetric keyref not supported.");
return SC_ERROR_NOT_SUPPORTED; return SC_ERROR_NOT_SUPPORTED;
} }
if (se_num > 0) if (se_num > 0)
{ {
sc_log(card->ctx, "restore security environment not supported.\n"); sc_log(card->ctx, "restore security environment not supported.");
return SC_ERROR_NOT_SUPPORTED; return SC_ERROR_NOT_SUPPORTED;
} }
@ -664,7 +671,7 @@ static int myeid_set_security_env_ec(sc_card_t *card, const sc_security_env_t *e
switch (env->operation) switch (env->operation)
{ {
case SC_SEC_OPERATION_DECIPHER: case SC_SEC_OPERATION_DECIPHER:
sc_debug(card->ctx, SC_LOG_DEBUG_NORMAL, "Decipher operation is not supported with EC keys.\n"); sc_log(card->ctx, "Decipher operation is not supported with EC keys.");
return SC_ERROR_NOT_SUPPORTED; return SC_ERROR_NOT_SUPPORTED;
break; break;
case SC_SEC_OPERATION_SIGN: case SC_SEC_OPERATION_SIGN:
@ -914,9 +921,7 @@ int myeid_ecdh_derive(struct sc_card *card, const u8* pubkey, size_t pubkey_len,
int r; int r;
sc_format_apdu(card, &apdu, sc_format_apdu(card, &apdu, SC_APDU_CASE_4_SHORT, 0x86, 0x00, 0x00);
SC_APDU_CASE_4_SHORT,
0x86, 0x00, 0x00);
apdu.resp = rbuf; apdu.resp = rbuf;
apdu.resplen = sizeof(rbuf); apdu.resplen = sizeof(rbuf);
@ -1297,7 +1302,6 @@ static int myeid_get_info(struct sc_card *card, u8 *rbuf, size_t buflen)
{ {
sc_apdu_t apdu; sc_apdu_t apdu;
int r; int r;
char nameBuf[100];
LOG_FUNC_CALLED(card->ctx); LOG_FUNC_CALLED(card->ctx);
@ -1314,7 +1318,7 @@ static int myeid_get_info(struct sc_card *card, u8 *rbuf, size_t buflen)
if (apdu.resplen != 20) if (apdu.resplen != 20)
{ {
sc_log(card->ctx, "Unexpected response to GET DATA (applet info)\n"); sc_log(card->ctx, "Unexpected response to GET DATA (applet info)");
return SC_ERROR_INTERNAL; return SC_ERROR_INTERNAL;
} }
@ -1322,9 +1326,10 @@ static int myeid_get_info(struct sc_card *card, u8 *rbuf, size_t buflen)
card->version.fw_major = rbuf[5] * 10 + rbuf[6]; card->version.fw_major = rbuf[5] * 10 + rbuf[6];
card->version.fw_minor = rbuf[7]; card->version.fw_minor = rbuf[7];
/* add version to name */ /* add version to name */
sprintf((char *) nameBuf, "%s %d.%d.%d", card->name, rbuf[5], rbuf[6], rbuf[7]); snprintf(card_name_buf, sizeof(card_name_buf),
card->name = nameBuf; "%s %d.%d.%d", card->name, rbuf[5], rbuf[6], rbuf[7]);
//card->driver->name card->name = card_name_buf;
LOG_FUNC_RETURN(card->ctx, r); LOG_FUNC_RETURN(card->ctx, r);
} }