fixed case 2 extended apdu parsing
git-svn-id: https://vsmartcard.svn.sourceforge.net/svnroot/vsmartcard@269 96b47cad-a561-4643-ad3b-153ac7d7599c
This commit is contained in:
@@ -77,116 +77,119 @@ int initialize(int reader_id, const char *cdriver, int verbose,
|
|||||||
|
|
||||||
int build_apdu(sc_context_t *ctx, const u8 *buf, size_t len, sc_apdu_t *apdu)
|
int build_apdu(sc_context_t *ctx, const u8 *buf, size_t len, sc_apdu_t *apdu)
|
||||||
{
|
{
|
||||||
const u8 *p;
|
const u8 *p;
|
||||||
size_t len0;
|
size_t len0;
|
||||||
|
|
||||||
if (!buf || !apdu)
|
if (!buf || !apdu)
|
||||||
return SC_ERROR_INVALID_ARGUMENTS;
|
return SC_ERROR_INVALID_ARGUMENTS;
|
||||||
|
|
||||||
len0 = len;
|
len0 = len;
|
||||||
if (len < 4) {
|
if (len < 4) {
|
||||||
sc_error(ctx, "APDU too short (must be at least 4 bytes)");
|
sc_error(ctx, "APDU too short (must be at least 4 bytes)");
|
||||||
return SC_ERROR_INVALID_DATA;
|
return SC_ERROR_INVALID_DATA;
|
||||||
}
|
}
|
||||||
|
|
||||||
memset(apdu, 0, sizeof(*apdu));
|
memset(apdu, 0, sizeof(*apdu));
|
||||||
p = buf;
|
p = buf;
|
||||||
apdu->cla = *p++;
|
apdu->cla = *p++;
|
||||||
apdu->ins = *p++;
|
apdu->ins = *p++;
|
||||||
apdu->p1 = *p++;
|
apdu->p1 = *p++;
|
||||||
apdu->p2 = *p++;
|
apdu->p2 = *p++;
|
||||||
len -= 4;
|
len -= 4;
|
||||||
if (len > 1) {
|
if (!len) {
|
||||||
/* case 2 with le=0 and case 4 with lc=0 are the only short APDUs,
|
apdu->cse = SC_APDU_CASE_1;
|
||||||
* where the next byte is 0. Their length (without header) is 1 and
|
} else {
|
||||||
* 2 respectively */
|
if (*p == 0 && len >= 3) {
|
||||||
if (*p == 0 && len > 2) {
|
/* ...must be an extended APDU */
|
||||||
/* ...must be an extended length APDU */
|
p++;
|
||||||
p++;
|
if (len == 3) {
|
||||||
if (len == 3) {
|
apdu->le = (*p++)<<8;
|
||||||
apdu->le = (*p++)<<8;
|
apdu->le += *p++;
|
||||||
apdu->le += *p++;
|
if (apdu->le == 0)
|
||||||
if (apdu->le == 0)
|
apdu->le = 0xffff+1;
|
||||||
apdu->le = 0xffff;
|
len -= 3;
|
||||||
len -= 3;
|
apdu->cse = SC_APDU_CASE_2_EXT;
|
||||||
apdu->cse = SC_APDU_CASE_2_SHORT;
|
} else {
|
||||||
} else {
|
/* len > 3 */
|
||||||
apdu->lc = (*p++)<<8;
|
apdu->lc = (*p++)<<8;
|
||||||
apdu->lc += *p++;
|
apdu->lc += *p++;
|
||||||
len -= 3;
|
len -= 3;
|
||||||
if (len < apdu->lc) {
|
if (len < apdu->lc) {
|
||||||
sc_error(ctx, "APDU too short (need %lu bytes)\n",
|
sc_error(ctx, "APDU too short (need %lu more bytes)\n",
|
||||||
(unsigned long) apdu->lc - len);
|
(unsigned long) apdu->lc - len);
|
||||||
return SC_ERROR_INVALID_DATA;
|
return SC_ERROR_INVALID_DATA;
|
||||||
}
|
}
|
||||||
len -= apdu->lc;
|
apdu->data = p;
|
||||||
p += apdu->lc;
|
apdu->datalen = apdu->lc;
|
||||||
if (len) {
|
len -= apdu->lc;
|
||||||
if (*p++ != 0) {
|
p += apdu->lc;
|
||||||
sc_error(ctx, "Extended APDU needs Lc, that begins with 0x00)\n",
|
if (!len) {
|
||||||
(unsigned long) apdu->lc - len);
|
apdu->cse = SC_APDU_CASE_3_EXT;
|
||||||
return SC_ERROR_INVALID_DATA;
|
} else {
|
||||||
}
|
if (len < 3) {
|
||||||
apdu->le = (*p++)<<8;
|
sc_error(ctx, "APDU too short (need %lu more bytes)\n",
|
||||||
apdu->le += *p++;
|
(unsigned long) apdu->lc - len);
|
||||||
if (apdu->le == 0)
|
return SC_ERROR_INVALID_DATA;
|
||||||
apdu->le = 0xffff;
|
}
|
||||||
len -= 3;
|
if (*p++ != 0) {
|
||||||
apdu->cse = SC_APDU_CASE_4_EXT;
|
sc_error(ctx, "Extended APDU needs Le, that begins with 0x00)\n");
|
||||||
} else {
|
return SC_ERROR_INVALID_DATA;
|
||||||
apdu->cse = SC_APDU_CASE_3_EXT;
|
}
|
||||||
}
|
apdu->le = (*p++)<<8;
|
||||||
if (len) {
|
apdu->le += *p++;
|
||||||
sc_error(ctx, "APDU too long (%lu bytes extra)\n",
|
if (apdu->le == 0)
|
||||||
(unsigned long) len);
|
apdu->le = 0xffff+1;
|
||||||
return SC_ERROR_INVALID_DATA;
|
len -= 3;
|
||||||
}
|
apdu->cse = SC_APDU_CASE_4_EXT;
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
apdu->lc = *p++;
|
} else {
|
||||||
len--;
|
/* ...must be a short APDU */
|
||||||
apdu->data = p;
|
if (len == 1) {
|
||||||
apdu->datalen = apdu->lc;
|
apdu->le = *p++;
|
||||||
if (len < apdu->lc) {
|
if (apdu->le == 0)
|
||||||
sc_error(ctx, "APDU too short (need %lu bytes)\n",
|
apdu->le = 0xff+1;
|
||||||
(unsigned long) apdu->lc - len);
|
len--;
|
||||||
return SC_ERROR_INVALID_DATA;
|
apdu->cse = SC_APDU_CASE_2_SHORT;
|
||||||
}
|
} else {
|
||||||
len -= apdu->lc;
|
apdu->lc = *p++;
|
||||||
p += apdu->lc;
|
len--;
|
||||||
if (len) {
|
if (len < apdu->lc) {
|
||||||
apdu->le = *p++;
|
sc_error(ctx, "APDU too short (need %lu more bytes)\n",
|
||||||
if (apdu->le == 0)
|
(unsigned long) apdu->lc - len);
|
||||||
apdu->le = 0xff;
|
return SC_ERROR_INVALID_DATA;
|
||||||
len--;
|
}
|
||||||
apdu->cse = SC_APDU_CASE_4_SHORT;
|
apdu->data = p;
|
||||||
} else {
|
apdu->datalen = apdu->lc;
|
||||||
apdu->cse = SC_APDU_CASE_3_SHORT;
|
len -= apdu->lc;
|
||||||
}
|
p += apdu->lc;
|
||||||
if (len) {
|
if (!len) {
|
||||||
sc_error(ctx, "APDU too long (%lu bytes extra)\n",
|
apdu->cse = SC_APDU_CASE_3_SHORT;
|
||||||
(unsigned long) len);
|
} else {
|
||||||
return SC_ERROR_INVALID_DATA;
|
apdu->le = *p++;
|
||||||
}
|
if (apdu->le == 0)
|
||||||
}
|
apdu->le = 0xff+1;
|
||||||
} else if (len == 1) {
|
len--;
|
||||||
apdu->le = *p++;
|
apdu->cse = SC_APDU_CASE_4_SHORT;
|
||||||
if (apdu->le == 0)
|
|
||||||
apdu->le = 256;
|
|
||||||
len--;
|
|
||||||
apdu->cse = SC_APDU_CASE_2_SHORT;
|
|
||||||
} else {
|
|
||||||
apdu->cse = SC_APDU_CASE_1;
|
|
||||||
}
|
|
||||||
|
|
||||||
apdu->flags = SC_APDU_FLAGS_NO_GET_RESP|SC_APDU_FLAGS_NO_RETRY_WL;
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (len) {
|
||||||
|
sc_error(ctx, "APDU too long (%lu bytes extra)\n",
|
||||||
|
(unsigned long) len);
|
||||||
|
return SC_ERROR_INVALID_DATA;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
sc_debug(ctx, "Case %d %s APDU, %u bytes:\tins=%02x p1=%02x p2=%02x",
|
apdu->flags = SC_APDU_FLAGS_NO_GET_RESP|SC_APDU_FLAGS_NO_RETRY_WL;
|
||||||
apdu->cse & SC_APDU_SHORT_MASK,
|
|
||||||
(apdu->cse & SC_APDU_EXT) != 0 ? "extended" : "short",
|
|
||||||
(unsigned int) len0, apdu->ins, apdu->p1, apdu->p2);
|
|
||||||
|
|
||||||
return SC_SUCCESS;
|
sc_debug(ctx, "Case %d %s APDU, %lu bytes:\tins=%02x p1=%02x p2=%02x",
|
||||||
|
apdu->cse & SC_APDU_SHORT_MASK,
|
||||||
|
(apdu->cse & SC_APDU_EXT) != 0 ? "extended" : "short",
|
||||||
|
(unsigned long) len0, apdu->ins, apdu->p1, apdu->p2);
|
||||||
|
|
||||||
|
return SC_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
void _bin_log(sc_context_t *ctx, int type, const char *file, int line,
|
void _bin_log(sc_context_t *ctx, int type, const char *file, int line,
|
||||||
|
|||||||
Reference in New Issue
Block a user