summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJim Rees <rees@cvs.openbsd.org>2001-07-26 16:10:02 +0000
committerJim Rees <rees@cvs.openbsd.org>2001-07-26 16:10:02 +0000
commit5f69a456aa76400ba9df10b80426c7ccd5627b89 (patch)
treeddfad7d6c5fd7a33ec705754ab99a6151a000994
parente99e9155e1633be5c2e6ef445ff5b2f20d0e07c5 (diff)
fid command now selects aids too
remove jselect port numbers are now 0-based print friendly aid names
-rw-r--r--usr.bin/sectok/cmds.c49
-rw-r--r--usr.bin/sectok/cyberflex.c120
-rw-r--r--usr.bin/sectok/main.c8
-rw-r--r--usr.bin/sectok/sc.h4
4 files changed, 83 insertions, 98 deletions
diff --git a/usr.bin/sectok/cmds.c b/usr.bin/sectok/cmds.c
index fb04bf4f27f..5928a3f9755 100644
--- a/usr.bin/sectok/cmds.c
+++ b/usr.bin/sectok/cmds.c
@@ -1,4 +1,4 @@
-/* $Id: cmds.c,v 1.11 2001/07/20 15:52:54 rees Exp $ */
+/* $Id: cmds.c,v 1.12 2001/07/26 16:10:01 rees Exp $ */
/*
* Smartcard commander.
@@ -60,7 +60,7 @@ struct {
/* 7816-4 commands */
{ "apdu", "[ -c class ] ins p1 p2 p3 data ...", apdu },
- { "fid", "[ -v ] fid", selfid },
+ { "fid", "[ -v ] fid/aid", selfid },
{ "isearch", "", isearch },
{ "class", "[ class ]", class },
{ "read", "[ -x ] filesize", dread },
@@ -78,7 +78,6 @@ struct {
{ "jaut", "", jaut },
{ "jload", "[ -p progID ] [ -c contID ] [ -s cont_size ] [ -i inst_size ] [ -a aid ] [ -v ] filename", jload },
{ "junload", "[ -p progID ] [ -c contID ]", junload },
- { "jselect", "[ -a aid ] [ -d ]", jselect },
{ "setpass", "[ -d ] [ -x hex-aut0 ]", jsetpass },
{ NULL, NULL, NULL }
};
@@ -133,13 +132,13 @@ int reset(int ac, char *av[])
optind = optreset = 1;
- while ((i = getopt(ac, av, "1234ivf")) != -1) {
+ while ((i = getopt(ac, av, "0123ivf")) != -1) {
switch (i) {
+ case '0':
case '1':
case '2':
case '3':
- case '4':
- port = i - '1';
+ port = i - '0';
break;
case 'i':
oflags |= STONOWAIT;
@@ -234,34 +233,56 @@ int apdu(int ac, char *av[])
int selfid(int ac, char *av[])
{
- unsigned char fid[2], obuf[256];
- int i, n, olen = 0, sw;
+ unsigned char fid[16], obuf[256];
+ char *fname;
+ int i, n, sel, fidlen, olen = 0, sw;
optind = optreset = 1;
while ((i = getopt(ac, av, "v")) != -1) {
switch (i) {
case 'v':
- olen = sizeof obuf;
+ olen = 256;
break;
}
}
- if (ac - optind != 1) {
- printf("usage: f [ -v ] fid\n");
- return -1;
+ if (ac - optind == 0) {
+ /* No fid/aid given; select null aid (default loader for Cyberflex) */
+ sel = 4;
+ fidlen = 0;
+ } else {
+ fname = av[optind++];
+ if (!strcmp(fname, "..")) {
+ /* Special case ".." means parent */
+ sel = 3;
+ fidlen = 0;
+ } else if (strlen(fname) < 5) {
+ /* fid */
+ sel = 0;
+ fidlen = 2;
+ sectok_parse_fname(fname, fid);
+ } else {
+ /* aid */
+ sel = 4;
+ fidlen = sectok_parse_input(fname, fid, sizeof fid);
+ }
}
- sectok_parse_fname(av[optind++], fid);
if (fd < 0 && reset(0, NULL) < 0)
return -1;
- n = sectok_apdu(fd, cla, 0xa4, 0, 0, 2, fid, olen, obuf, &sw);
+ n = sectok_apdu(fd, cla, 0xa4, sel, 0, fidlen, fid, olen, obuf, &sw);
if (!sectok_swOK(sw)) {
printf("Select %02x%02x: %s\n", fid[0], fid[1], sectok_get_sw(sw));
return -1;
}
+ if (olen && !n && sectok_r1(sw) == 0x61 && sectok_r2(sw)) {
+ /* The card has out data but we must explicitly ask for it */
+ n = sectok_apdu(fd, cla, 0xc0, 0, 0, 0, NULL, sectok_r2(sw), obuf, &sw);
+ }
+
if (olen)
sectok_dump_reply(obuf, n, sw);
diff --git a/usr.bin/sectok/cyberflex.c b/usr.bin/sectok/cyberflex.c
index d12a25f3f49..40fcb0a1955 100644
--- a/usr.bin/sectok/cyberflex.c
+++ b/usr.bin/sectok/cyberflex.c
@@ -1,4 +1,4 @@
-/* $Id: cyberflex.c,v 1.11 2001/07/20 15:52:54 rees Exp $ */
+/* $Id: cyberflex.c,v 1.12 2001/07/26 16:10:01 rees Exp $ */
/*
copyright 1999, 2000
@@ -56,6 +56,8 @@ such damages.
#define RSA_BIT_LEN 1024
#define KEY_FILE_HEADER_SIZE 8
+#define myisprint(x) ((x) >= '!' && (x) <= 'z')
+
static unsigned char key_fid[] = {0x00, 0x11};
static unsigned char DFLTATR[] = {0x81, 0x10, 0x06, 0x01};
static unsigned char DFLTAUT0[] = {0xad, 0x9f, 0x61, 0xfe, 0xfa, 0x20, 0xce, 0x63};
@@ -197,7 +199,7 @@ int jatr(int ac, char *av[])
buf[n++] = 0x94; /* TA1 */
buf[n++] = 0x40; /* TD1 */
buf[n++] = 0x28; /* TC2 (WWT=4sec) */
- if (ac > optind) {
+ if (ac > 1) {
/* set historical bytes from command line */
n += sectok_parse_input(av[1], &buf[n], 15);
} else {
@@ -227,6 +229,12 @@ int jdata(int ac, char *av[])
if (fd < 0 && reset(0, NULL) < 0)
return -1;
+ cla = cyberflex_inq_class(fd);
+ if (cla < 0) {
+ printf("can't determine Cyberflex application class\n");
+ return -1;
+ }
+
sectok_apdu(fd, cla, 0xca, 0, 1, 0, NULL, 0x16, buf, &sw);
if (sectok_swOK(sw)) {
printf("serno ");
@@ -315,11 +323,30 @@ print_acl(int isdir, unsigned char *acl)
}
}
+void
+sectok_fmt_aidname(char *aidname, int aidlen, unsigned char *aid)
+{
+ int i, istext = 1;
+
+ for (i = 0; i < aidlen; i++)
+ if (!myisprint(aid[i])) {
+ istext = 0;
+ break;
+ }
+ if (istext) {
+ memmove(aidname, aid, aidlen);
+ aidname[aidlen] = '\0';
+ } else {
+ for (i = 0; i < aidlen; i++)
+ sprintf(&aidname[i * 2], "%02x", aid[i]);
+ }
+}
+
int ls(int ac, char *av[])
{
- int i, p2, f0, f1, lflag = 0, sw;
+ int i, p2, f0, f1, lflag = 0, buflen, sw;
int isdir, fsize;
- char ftype[32], fname[6];
+ char ftype[32], fname[6], aidname[34];
unsigned char buf[JDIRSIZE];
optind = optreset = 1;
@@ -336,7 +363,7 @@ int ls(int ac, char *av[])
return -1;
for (p2 = 0; ; p2++) {
- sectok_apdu(fd, cla, 0xa8, 0, p2, 0, NULL, JDIRSIZE, buf, &sw);
+ buflen = sectok_apdu(fd, cla, 0xa8, 0, p2, 0, NULL, JDIRSIZE, buf, &sw);
if (!sectok_swOK(sw))
break;
f0 = buf[4];
@@ -352,15 +379,21 @@ int ls(int ac, char *av[])
/* Format file type */
isdir = 0;
+ aidname[0] = '\0';
if (buf[6] == 1) {
/* root */
sprintf(ftype, "root");
isdir = 1;
} else if (buf[6] == 2) {
/* DF */
- if (buf[12] == 27)
+ if (buf[12] == 27) {
+ /* application */
sprintf(ftype, "%s %s", appstat[buf[10]], apptype[buf[9]]);
- else
+ if (buflen > 23 && buf[23]) {
+ aidname[0] = ' ';
+ sectok_fmt_aidname(&aidname[1], buf[23], &buf[24]);
+ }
+ } else
sprintf(ftype, "directory");
isdir = 1;
} else if (buf[6] == 4) {
@@ -371,7 +404,7 @@ int ls(int ac, char *av[])
if (!lflag)
printf("%-4s\n", fname);
else
- printf("%-4s %5d %s\n", fname, fsize, ftype);
+ printf("%-4s %5d %s%s\n", fname, fsize, ftype, aidname);
}
return 0;
}
@@ -509,8 +542,6 @@ void load_default_options()
{
memmove(progID, "ww", 2);
memmove(contID, "wx", 2);
- cont_size = 1152;
- inst_size = 1024;
memset(aid, 'w', sizeof aid);
aid_len = 5;
}
@@ -525,6 +556,8 @@ int jload(int ac, char *av[])
des_key_schedule schedule;
load_default_options();
+ cont_size = 1152;
+ inst_size = 1024;
optind = optreset = 1;
@@ -782,73 +815,6 @@ int junload(int ac, char *av[])
return 0;
}
-int jselect(int ac, char *av[])
-{
- int i, vflag = 0, sw;
-
- load_default_options();
-
- optind = optreset = 1;
-
- while ((i = getopt(ac, av, "dp:c:s:i:a:v")) != -1) {
- switch (i) {
- case 'd':
- aid_len = 0;
- break;
- case 'p':
- sectok_parse_input(optarg, progID, 2);
- break;
- case 'c':
- sectok_parse_input(optarg, contID, 2);
- break;
- case 's':
- sscanf(optarg, "%d", &cont_size);
- break;
- case 'i':
- sscanf(optarg, "%d", &inst_size);
- break;
- case 'a':
- aid_len = sectok_parse_input(optarg, aid, sizeof aid);
- break;
- case 'v':
- vflag = 1;
- break;
- default:
- printf ("unknown option. command aborted.\n");
- return -1;
- }
- }
-
- if (fd < 0 && reset(0, NULL) < 0)
- return -1;
-
- if (vflag && aid_len) {
- printf ("select applet\n");
- printf ("AID ");
- for (i = 0 ; i < aid_len ; i ++ )
- printf ("%02x", (int) aid[i]);
- printf ("\n");
- }
-
- sectok_apdu(fd, cla, 0xa4, 0x04, 0, aid_len, aid, 0, NULL, &sw);
- if (!sectok_swOK(sw)) {
- /* even with F0 card, select applet APDU (00 a4 04)
- only accepts class byte 00 (not f0) */
- sectok_apdu(fd, 0, 0xa4, 0x04, 0, aid_len, aid, 0, NULL, &sw);
- }
- if (!sectok_swOK(sw)) {
- /* error */
- printf ("selecting the cardlet: ");
- for (i = 0 ; i < aid_len ; i ++ )
- printf ("%02x", aid[i]);
- printf("\n");
- sectok_print_sw(sw);
- return -1;
- }
-
- return 0;
-}
-
#define DELIMITER " :\t\n"
#define KEY_BLOCK_SIZE 14
diff --git a/usr.bin/sectok/main.c b/usr.bin/sectok/main.c
index e40de8ada4c..edf48c1e464 100644
--- a/usr.bin/sectok/main.c
+++ b/usr.bin/sectok/main.c
@@ -1,4 +1,4 @@
-/* $Id: main.c,v 1.2 2001/07/02 20:15:07 rees Exp $ */
+/* $Id: main.c,v 1.3 2001/07/26 16:10:01 rees Exp $ */
/*
* Smartcard commander.
@@ -69,13 +69,13 @@ char *av[];
int i, tc;
char buf[256], *scriptfile = NULL, *tp, *tv[MAXTOKENS];
- while ((i = getopt(ac, av, "1234c:d:f:Hhs:")) != -1) {
+ while ((i = getopt(ac, av, "0123f:s:h")) != -1) {
switch (i) {
+ case '0':
case '1':
case '2':
case '3':
- case '4':
- port = i - '1';
+ port = i - '0';
break;
case 'f':
scriptfile = optarg;
diff --git a/usr.bin/sectok/sc.h b/usr.bin/sectok/sc.h
index 105ccb0fb1e..64032ef6bd8 100644
--- a/usr.bin/sectok/sc.h
+++ b/usr.bin/sectok/sc.h
@@ -1,4 +1,4 @@
-/* $Id: sc.h,v 1.6 2001/07/17 21:04:14 rees Exp $ */
+/* $Id: sc.h,v 1.7 2001/07/26 16:10:01 rees Exp $ */
/*
* Smartcard commander.
@@ -60,6 +60,4 @@ int jlogin(int ac, char *av[]);
int jaut(int ac, char *av[]);
int jload(int ac, char *av[]);
int junload(int ac, char *av[]);
-int jselect(int ac, char *av[]);
-int jdeselect(int ac, char *av[]);
int jsetpass(int ac, char *av[]);