diff --git a/pcsc-relay/src/lnfc.c b/pcsc-relay/src/lnfc.c index 14c1c7e9..10cfbfb0 100644 --- a/pcsc-relay/src/lnfc.c +++ b/pcsc-relay/src/lnfc.c @@ -207,13 +207,15 @@ static int lnfc_receive_capdu(driver_data_t *driver_data, } - p = realloc(*capdu, data->iCapduLen); - if (!p) { - RELAY_ERROR("Error allocating memory for C-APDU\n"); - return 0; + if (data->iCapduLen) { + p = realloc(*capdu, data->iCapduLen); + if (!p) { + RELAY_ERROR("Error allocating memory for C-APDU (%d bytes)\n", data->iCapduLen); + return 0; + } + memcpy(p, data->abtCapdu, data->iCapduLen); + *capdu = p; } - memcpy(p, data->abtCapdu, data->iCapduLen); - *capdu = p; *len = data->iCapduLen; @@ -230,13 +232,15 @@ static int lnfc_send_rapdu(driver_data_t *driver_data, return 0; - r = nfc_target_send_bytes(data->pndTarget, rapdu, len, -1); - if (r < 0) { - RELAY_ERROR ("nfc_target_send_bytes: %s\n", nfc_strerror(data->pndTarget)); - return 0; + if (len) { + r = nfc_target_send_bytes(data->pndTarget, rapdu, len, -1); + if (r < 0) { + RELAY_ERROR ("nfc_target_send_bytes: %s\n", nfc_strerror(data->pndTarget)); + return 0; + } + if (r < len) + INFO ("Transmitted %u less bytes than desired: %s\n", (unsigned int) len-r, nfc_strerror(data->pndTarget)); } - if (r < len) - INFO ("Transmitted %u less bytes than desired: %s\n", (unsigned int) len-r, nfc_strerror(data->pndTarget)); return 1; diff --git a/pcsc-relay/src/opicc.c b/pcsc-relay/src/opicc.c index d67f4acf..06a5b8af 100644 --- a/pcsc-relay/src/opicc.c +++ b/pcsc-relay/src/opicc.c @@ -208,15 +208,13 @@ static int picc_receive_capdu(driver_data_t *driver_data, /* read C-APDU */ linelen = getline(&data->line, &data->linemax, data->fd); - if (linelen <= 0) { - if (linelen < 0) { - RELAY_ERROR("Error reading from %s: %s\n", PICCDEV, strerror(errno)); - return 0; - } - if (linelen == 0) { - *len = 0; - return 1; - } + if (linelen < 0) { + RELAY_ERROR("Error reading from %s: %s\n", PICCDEV, strerror(errno)); + return 0; + } + if (linelen == 0) { + *len = 0; + return 1; } if (fflush(data->fd) != 0) RELAY_ERROR("Warning, fflush failed: %s\n", strerror(errno)); @@ -237,6 +235,9 @@ static int picc_send_rapdu(driver_data_t *driver_data, if (!data || !rapdu) return 0; + if (!len) + return 1; + /* encode R-APDU */ if (!picc_encode_rapdu(rapdu, len, &data->e_rapdu, &buflen)) diff --git a/pcsc-relay/src/vicc.c b/pcsc-relay/src/vicc.c index ea934482..a9f6df28 100644 --- a/pcsc-relay/src/vicc.c +++ b/pcsc-relay/src/vicc.c @@ -136,7 +136,7 @@ static int vicc_receive_capdu(driver_data_t *driver_data, goto err; } } else { - // finaly we got the capdu + // finally we got the C-APDU *len = size; r = 1; }