version 1.10, 2001/07/19 21:24:27 |
version 1.11, 2001/07/20 15:52:54 |
|
|
#include <signal.h> |
#include <signal.h> |
#include <string.h> |
#include <string.h> |
#include <sectok.h> |
#include <sectok.h> |
#include <sc7816.h> |
|
|
|
#include "sc.h" |
#include "sc.h" |
|
|
|
|
|
|
/* 7816-4 commands */ |
/* 7816-4 commands */ |
{ "apdu", "[ -c class ] ins p1 p2 p3 data ...", apdu }, |
{ "apdu", "[ -c class ] ins p1 p2 p3 data ...", apdu }, |
{ "fid", "fid", selfid }, |
{ "fid", "[ -v ] fid", selfid }, |
{ "isearch", "", isearch }, |
{ "isearch", "", isearch }, |
{ "class", "[ class ]", class }, |
{ "class", "[ class ]", class }, |
{ "read", "[ -x ] filesize", dread }, |
{ "read", "[ -x ] filesize", dread }, |
|
|
|
|
n = sectok_reset(fd, rflags, atr, &sw); |
n = sectok_reset(fd, rflags, atr, &sw); |
if (vflag) |
if (vflag) |
parse_atr(fd, SCRV, atr, n, ¶m); |
sectok_parse_atr(fd, STRV, atr, n, ¶m); |
if (!sectok_swOK(sw)) { |
if (!sectok_swOK(sw)) { |
printf("sectok_reset: %s\n", sectok_get_sw(sw)); |
printf("sectok_reset: %s\n", sectok_get_sw(sw)); |
dclose(0, NULL); |
dclose(0, NULL); |
|
|
int dclose(int ac, char *av[]) |
int dclose(int ac, char *av[]) |
{ |
{ |
if (fd >= 0) { |
if (fd >= 0) { |
scclose(fd); |
sectok_close(fd); |
fd = -1; |
fd = -1; |
} |
} |
return 0; |
return 0; |
|
|
|
|
int selfid(int ac, char *av[]) |
int selfid(int ac, char *av[]) |
{ |
{ |
unsigned char fid[2]; |
unsigned char fid[2], obuf[256]; |
int sw; |
int i, n, olen = 0, sw; |
|
|
if (ac != 2) { |
optind = optreset = 1; |
printf("usage: f fid\n"); |
|
|
while ((i = getopt(ac, av, "v")) != -1) { |
|
switch (i) { |
|
case 'v': |
|
olen = sizeof obuf; |
|
break; |
|
} |
|
} |
|
|
|
if (ac - optind != 1) { |
|
printf("usage: f [ -v ] fid\n"); |
return -1; |
return -1; |
} |
} |
|
sectok_parse_fname(av[optind++], fid); |
|
|
if (fd < 0 && reset(0, NULL) < 0) |
if (fd < 0 && reset(0, NULL) < 0) |
return -1; |
return -1; |
|
|
sectok_parse_fname(av[1], fid); |
n = sectok_apdu(fd, cla, 0xa4, 0, 0, 2, fid, olen, obuf, &sw); |
if (sectok_selectfile(fd, cla, fid, &sw) < 0) { |
if (!sectok_swOK(sw)) { |
printf("selectfile: %s\n", sectok_get_sw(sw)); |
printf("Select %02x%02x: %s\n", fid[0], fid[1], sectok_get_sw(sw)); |
return -1; |
return -1; |
} |
} |
|
|
|
if (olen) |
|
sectok_dump_reply(obuf, n, sw); |
|
|
return 0; |
return 0; |
} |
} |
|
|
int isearch(int ac, char *av[]) |
int isearch(int ac, char *av[]) |
{ |
{ |
int i, r1, r2; |
int i, r1, sw; |
unsigned char buf[256]; |
|
|
|
if (fd < 0 && reset(0, NULL) < 0) |
if (fd < 0 && reset(0, NULL) < 0) |
return -1; |
return -1; |
|
|
/* find instructions */ |
/* find instructions */ |
for (i = 0; i < 0xff; i += 2) |
for (i = 0; i < 0xff; i += 2) { |
if (scread(fd, cla, i, 0, 0, 0, buf, &r1, &r2) == 0 |
sectok_apdu(fd, cla, i, 0, 0, 0, NULL, 0, NULL, &sw); |
&& r1 != 0x6d && r1 != 0x6e) |
r1 = sectok_r1(sw); |
printf("%02x %s %s\n", i, lookup_cmdname(i), get_r1r2s(r1, r2)); |
if (r1 != 0x06 && r1 != 0x6d && r1 != 0x6e) |
|
printf("%02x %s %s\n", i, sectok_get_ins(i), sectok_get_sw(sw)); |
|
} |
return 0; |
return 0; |
} |
} |
|
|
|
|
} |
} |
} |
} |
|
|
if (ac - optind < 1) { |
if (ac - optind != 1) { |
printf("usage: read [ -x ] filesize\n"); |
printf("usage: read [ -x ] filesize\n"); |
return -1; |
return -1; |
} |
} |
|
|
n = (fsize < CARDIOSIZE) ? fsize : CARDIOSIZE; |
n = (fsize < CARDIOSIZE) ? fsize : CARDIOSIZE; |
n = sectok_apdu(fd, cla, 0xb0, p3 >> 8, p3 & 0xff, 0, NULL, n, buf, &sw); |
n = sectok_apdu(fd, cla, 0xb0, p3 >> 8, p3 & 0xff, 0, NULL, n, buf, &sw); |
if (!sectok_swOK(sw)) { |
if (!sectok_swOK(sw)) { |
printf("read binary: %s\n", sectok_get_sw(sw)); |
printf("ReadBinary: %s\n", sectok_get_sw(sw)); |
break; |
break; |
} |
} |
if (xflag) { |
if (xflag) { |
|
|
|
|
int dwrite(int ac, char *av[]) |
int dwrite(int ac, char *av[]) |
{ |
{ |
int n, p3, r1, r2; |
int n, p3, sw; |
FILE *f; |
FILE *f; |
unsigned char buf[CARDIOSIZE]; |
unsigned char buf[CARDIOSIZE]; |
|
|
|
|
|
|
n = 0; |
n = 0; |
while ((p3 = fread(buf, 1, CARDIOSIZE, f)) > 0) { |
while ((p3 = fread(buf, 1, CARDIOSIZE, f)) > 0) { |
if (scwrite(fd, cla, 0xd6, n >> 8, n & 0xff, p3, buf, &r1, &r2) < 0) { |
sectok_apdu(fd, cla, 0xd6, n >> 8, n & 0xff, p3, buf, 0, NULL, &sw); |
printf("scwrite failed\n"); |
if (!sectok_swOK(sw)) { |
break; |
printf("UpdateBinary: %s\n", sectok_get_sw(sw)); |
} |
|
if (r1 != 0x90 && r1 != 0x61) { |
|
print_r1r2(r1, r2); |
|
break; |
break; |
} |
} |
n += p3; |
n += p3; |