opensc-{explorer,tool}: allow sending extended APDUs
In do_apdu() resp send_apdu/(, flexibilize parsing the APDU string passed so that extended APDUs are accepted a valid APDUs too. While at it, fix a bug where more data than available would have been copied, potentially leading to a SIGSEGV. Signed-off-by: Peter Marschall <peter@adpm.de> git-svn-id: https://www.opensc-project.org/svnp/opensc/trunk@5369 c6295689-39f2-0310-b995-f0e70906c6a9
This commit is contained in:
parent
9dc233163a
commit
1932ce861f
|
@ -1302,6 +1302,7 @@ static int do_apdu(int argc, char **argv)
|
||||||
u8 rbuf[SC_MAX_APDU_BUFFER_SIZE];
|
u8 rbuf[SC_MAX_APDU_BUFFER_SIZE];
|
||||||
u8 *p;
|
u8 *p;
|
||||||
size_t len, len0, r, ii;
|
size_t len, len0, r, ii;
|
||||||
|
int cse = 0;
|
||||||
|
|
||||||
if (argc < 1) {
|
if (argc < 1) {
|
||||||
puts("Usage: apdu [apdu:hex:codes:...]");
|
puts("Usage: apdu [apdu:hex:codes:...]");
|
||||||
|
@ -1327,42 +1328,72 @@ static int do_apdu(int argc, char **argv)
|
||||||
apdu.p1 = *p++;
|
apdu.p1 = *p++;
|
||||||
apdu.p2 = *p++;
|
apdu.p2 = *p++;
|
||||||
len -= 4;
|
len -= 4;
|
||||||
if (len > 1) {
|
|
||||||
apdu.lc = *p++;
|
if (len == 0) {
|
||||||
len--;
|
|
||||||
memcpy(sbuf, p, apdu.lc);
|
|
||||||
apdu.data = sbuf;
|
|
||||||
apdu.datalen = apdu.lc;
|
|
||||||
if (len < apdu.lc) {
|
|
||||||
printf("APDU too short (need %lu bytes)\n",
|
|
||||||
(unsigned long) apdu.lc - len);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
len -= apdu.lc;
|
|
||||||
p += apdu.lc;
|
|
||||||
if (len) {
|
|
||||||
apdu.le = *p++;
|
|
||||||
if (apdu.le == 0)
|
|
||||||
apdu.le = 256;
|
|
||||||
len--;
|
|
||||||
apdu.cse = SC_APDU_CASE_4_SHORT;
|
|
||||||
} else {
|
|
||||||
apdu.cse = SC_APDU_CASE_3_SHORT;
|
|
||||||
}
|
|
||||||
if (len) {
|
|
||||||
printf("APDU too long (%lu bytes extra)\n",
|
|
||||||
(unsigned long) len);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
} else if (len == 1) {
|
|
||||||
apdu.le = *p++;
|
|
||||||
if (apdu.le == 0)
|
|
||||||
apdu.le = 256;
|
|
||||||
len--;
|
|
||||||
apdu.cse = SC_APDU_CASE_2_SHORT;
|
|
||||||
} else {
|
|
||||||
apdu.cse = SC_APDU_CASE_1;
|
apdu.cse = SC_APDU_CASE_1;
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
size_t size = 0;
|
||||||
|
|
||||||
|
if ((*p == 0) && (len >= 3)) {
|
||||||
|
cse |= SC_APDU_EXT;
|
||||||
|
p++;
|
||||||
|
size = (*p++) << 8;
|
||||||
|
size += *p++;
|
||||||
|
len -= 3;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
size = *p++;
|
||||||
|
len--;
|
||||||
|
}
|
||||||
|
if (len == 0) {
|
||||||
|
apdu.le = (size == 0) ? 256 : size;
|
||||||
|
if ((apdu.le == 0) && (cse & SC_APDU_EXT))
|
||||||
|
apdu.le <<= 8;
|
||||||
|
apdu.cse = SC_APDU_CASE_2_SHORT | cse;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
apdu.lc = size;
|
||||||
|
if (len < apdu.lc) {
|
||||||
|
printf("APDU too short (need %lu bytes)\n",
|
||||||
|
(unsigned long) apdu.lc - len);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
memcpy(sbuf, p, apdu.lc);
|
||||||
|
apdu.data = sbuf;
|
||||||
|
apdu.datalen = apdu.lc;
|
||||||
|
len -= apdu.lc;
|
||||||
|
p += apdu.lc;
|
||||||
|
if (len == 0) {
|
||||||
|
apdu.cse = SC_APDU_CASE_3_SHORT | cse;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
apdu.le = 0;
|
||||||
|
if (cse & SC_APDU_EXT) {
|
||||||
|
if (len < 2) {
|
||||||
|
printf("APDU too short (need %lu bytes)\n",
|
||||||
|
(unsigned long) apdu.lc - len);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
size = (*p++) << 8;
|
||||||
|
size += *p++;
|
||||||
|
len -= 2;
|
||||||
|
apdu.le = (size == 0) ? 65536 : size;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
size = *p++;
|
||||||
|
len--;
|
||||||
|
apdu.le = (size == 0) ? 256 : size;
|
||||||
|
}
|
||||||
|
apdu.cse = SC_APDU_CASE_4_SHORT | cse;
|
||||||
|
if (len) {
|
||||||
|
printf("APDU too long (%lu bytes extra)\n",
|
||||||
|
(unsigned long) len);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
apdu.resp = rbuf;
|
apdu.resp = rbuf;
|
||||||
apdu.resplen = sizeof(rbuf);
|
apdu.resplen = sizeof(rbuf);
|
||||||
|
|
||||||
|
|
|
@ -496,6 +496,7 @@ static int send_apdu(void)
|
||||||
rbuf[SC_MAX_APDU_BUFFER_SIZE], *p;
|
rbuf[SC_MAX_APDU_BUFFER_SIZE], *p;
|
||||||
size_t len, len0, r;
|
size_t len, len0, r;
|
||||||
int c;
|
int c;
|
||||||
|
int cse = 0;
|
||||||
|
|
||||||
for (c = 0; c < opt_apdu_count; c++) {
|
for (c = 0; c < opt_apdu_count; c++) {
|
||||||
len0 = sizeof(buf);
|
len0 = sizeof(buf);
|
||||||
|
@ -514,40 +515,73 @@ static int send_apdu(void)
|
||||||
apdu.resp = rbuf;
|
apdu.resp = rbuf;
|
||||||
apdu.resplen = sizeof(rbuf);
|
apdu.resplen = sizeof(rbuf);
|
||||||
len -= 4;
|
len -= 4;
|
||||||
if (len > 1) {
|
|
||||||
apdu.lc = *p++;
|
if (len == 0) {
|
||||||
len--;
|
|
||||||
memcpy(sbuf, p, apdu.lc);
|
|
||||||
apdu.data = sbuf;
|
|
||||||
apdu.datalen = apdu.lc;
|
|
||||||
if (len < apdu.lc) {
|
|
||||||
fprintf(stderr, "APDU too short (need %lu bytes).\n",
|
|
||||||
(unsigned long) apdu.lc-len);
|
|
||||||
return 2;
|
|
||||||
}
|
|
||||||
len -= apdu.lc;
|
|
||||||
p += apdu.lc;
|
|
||||||
if (len) {
|
|
||||||
apdu.le = *p++;
|
|
||||||
if (apdu.le == 0)
|
|
||||||
apdu.le = 256;
|
|
||||||
len--;
|
|
||||||
apdu.cse = SC_APDU_CASE_4_SHORT;
|
|
||||||
} else
|
|
||||||
apdu.cse = SC_APDU_CASE_3_SHORT;
|
|
||||||
if (len) {
|
|
||||||
fprintf(stderr, "APDU too long (%lu bytes extra).\n",
|
|
||||||
(unsigned long) len);
|
|
||||||
return 2;
|
|
||||||
}
|
|
||||||
} else if (len == 1) {
|
|
||||||
apdu.le = *p++;
|
|
||||||
if (apdu.le == 0)
|
|
||||||
apdu.le = 256;
|
|
||||||
len--;
|
|
||||||
apdu.cse = SC_APDU_CASE_2_SHORT;
|
|
||||||
} else
|
|
||||||
apdu.cse = SC_APDU_CASE_1;
|
apdu.cse = SC_APDU_CASE_1;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
size_t size = 0;
|
||||||
|
|
||||||
|
if ((*p == 0) && (len >= 3)) {
|
||||||
|
cse |= SC_APDU_EXT;
|
||||||
|
p++;
|
||||||
|
size = (*p++) << 8;
|
||||||
|
size += *p++;
|
||||||
|
len -= 3;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
size = *p++;
|
||||||
|
len--;
|
||||||
|
}
|
||||||
|
if (len == 0) {
|
||||||
|
apdu.le = (size == 0) ? 256 : size;
|
||||||
|
if ((apdu.le == 0) && (cse & SC_APDU_EXT))
|
||||||
|
apdu.le <<= 8;
|
||||||
|
apdu.cse = SC_APDU_CASE_2_SHORT | cse;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
apdu.lc = size;
|
||||||
|
if (len < apdu.lc) {
|
||||||
|
printf("APDU too short (need %lu bytes)\n",
|
||||||
|
(unsigned long) apdu.lc - len);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
memcpy(sbuf, p, apdu.lc);
|
||||||
|
apdu.data = sbuf;
|
||||||
|
apdu.datalen = apdu.lc;
|
||||||
|
len -= apdu.lc;
|
||||||
|
p += apdu.lc;
|
||||||
|
if (len == 0) {
|
||||||
|
apdu.cse = SC_APDU_CASE_3_SHORT | cse;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
apdu.le = 0;
|
||||||
|
if (cse & SC_APDU_EXT) {
|
||||||
|
if (len < 2) {
|
||||||
|
printf("APDU too short (need %lu bytes)\n",
|
||||||
|
(unsigned long) apdu.lc - len);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
size = (*p++) << 8;
|
||||||
|
size += *p++;
|
||||||
|
len -= 2;
|
||||||
|
apdu.le = (size == 0) ? 65536 : size;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
size = *p++;
|
||||||
|
len--;
|
||||||
|
apdu.le = (size == 0) ? 256 : size;
|
||||||
|
}
|
||||||
|
apdu.cse = SC_APDU_CASE_4_SHORT | cse;
|
||||||
|
if (len) {
|
||||||
|
printf("APDU too long (%lu bytes extra)\n",
|
||||||
|
(unsigned long) len);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
printf("Sending: ");
|
printf("Sending: ");
|
||||||
for (r = 0; r < len0; r++)
|
for (r = 0; r < len0; r++)
|
||||||
printf("%02X ", buf[r]);
|
printf("%02X ", buf[r]);
|
||||||
|
|
Loading…
Reference in New Issue