Annotation of src/usr.bin/sectok/cmds.c, Revision 1.6
1.6 ! rees 1: /* $Id: cmds.c,v 1.5 2001/07/16 23:09:36 rees Exp $ */
1.1 rees 2:
3: /*
4: * Smartcard commander.
5: * Written by Jim Rees and others at University of Michigan.
6: */
7:
8: /*
9: copyright 2001
10: the regents of the university of michigan
11: all rights reserved
12:
13: permission is granted to use, copy, create derivative works
14: and redistribute this software and such derivative works
15: for any purpose, so long as the name of the university of
16: michigan is not used in any advertising or publicity
17: pertaining to the use or distribution of this software
18: without specific, written prior authorization. if the
19: above copyright notice or any other identification of the
20: university of michigan is included in any copy of any
21: portion of this software, then the disclaimer below must
22: also be included.
23:
24: this software is provided as is, without representation
25: from the university of michigan as to its fitness for any
26: purpose, and without warranty by the university of
27: michigan of any kind, either express or implied, including
28: without limitation the implied warranties of
29: merchantability and fitness for a particular purpose. the
30: regents of the university of michigan shall not be liable
31: for any damages, including special, indirect, incidental, or
32: consequential damages, with respect to any claim arising
33: out of or in connection with the use of the software, even
34: if it has been or is hereafter advised of the possibility of
35: such damages.
36: */
37:
38: #include <unistd.h>
39: #include <stdlib.h>
40: #include <stdio.h>
41: #include <signal.h>
42: #include <string.h>
43: #include <sectok.h>
1.3 rees 44: #include <sc7816.h>
1.1 rees 45:
46: #include "sc.h"
47:
48: #define CARDIOSIZE 200
49:
50: struct {
1.4 rees 51: char *cmd, *help;
1.1 rees 52: int (*action) (int ac, char *av[]);
53: } dispatch_table[] = {
54: /* Non-card commands */
1.4 rees 55: { "help", "[command]", help },
56: { "?", "[command]", help },
57: { "reset", "[ -1234ivf ]", reset },
58: { "open", "[ -1234ivf ]", reset },
59: { "close", "", dclose },
60: { "quit", "", quit },
1.1 rees 61:
62: /* 7816-4 commands */
1.4 rees 63: { "apdu", "[ -c class ] ins p1 p2 p3 data ...", apdu },
64: { "fid", "fid", selfid },
65: { "isearch", "", isearch },
66: { "class", "[ class ]", class },
67: { "read", "filesize", dread },
68: { "write", "input-filename", dwrite },
1.1 rees 69:
70: /* Cyberflex commands */
1.4 rees 71: { "ls", "", ls },
72: { "create", "fid size", jcreate },
73: { "delete", "fid", jdelete },
74: { "jdefault", "[ -d ]", jdefault },
75: { "jatr", "", jatr },
76: { "jdata", "", jdata },
1.6 ! rees 77: { "login", "[ -d ] [ -v ] [ -x hex-aut0 ]", jlogin },
! 78: { "jaut", "", jaut },
1.4 rees 79: { "jload", "[ -p progID ] [ -c contID ] [ -s cont_size ] [ -i inst_size ] [ -a aid ] filename", jload },
80: { "junload", "[ -p progID ] [ -c contID ]", junload },
81: { "jselect", "[ -a aid ]", jselect },
82: { "jdeselect", "", jdeselect },
1.5 rees 83: { "setpass", "[ -d ] [ -x hex-aut0 ]", jsetpass },
1.4 rees 84: { NULL, NULL, NULL }
1.1 rees 85: };
86:
87: int dispatch(int ac, char *av[])
88: {
89: int i;
90:
1.2 rees 91: if (ac < 1)
92: return 0;
93:
1.1 rees 94: for (i = 0; dispatch_table[i].cmd; i++) {
1.2 rees 95: if (!strncmp(av[0], dispatch_table[i].cmd, strlen(av[0]))) {
1.1 rees 96: (dispatch_table[i].action) (ac, av);
97: break;
98: }
99: }
100: if (!dispatch_table[i].cmd) {
101: printf("unknown command \"%s\"\n", av[0]);
102: return -1;
103: }
104: return 0;
105: }
106:
107: int help(int ac, char *av[])
108: {
1.4 rees 109: int i, j;
1.1 rees 110:
1.4 rees 111: if (ac < 2) {
112: for (i = 0; dispatch_table[i].cmd; i++)
1.1 rees 113: printf("%s\n", dispatch_table[i].cmd);
1.4 rees 114: } else {
115: for (j = 1; j < ac; j++) {
116: for (i = 0; dispatch_table[i].cmd; i++)
117: if (!strncmp(av[j], dispatch_table[i].cmd, strlen(av[0])))
118: break;
119: if (dispatch_table[i].help)
120: printf("%s %s\n", dispatch_table[i].cmd, dispatch_table[i].help);
121: else
122: printf("no help on \"%s\"\n", av[j]);
123: }
1.1 rees 124: }
125:
126: return 0;
127: }
128:
129: int reset(int ac, char *av[])
130: {
1.3 rees 131: int i, n, oflags = 0, rflags = 0, vflag = 0, sw;
132: unsigned char atr[34];
133: struct scparam param;
1.1 rees 134:
135: optind = optreset = 1;
136:
137: while ((i = getopt(ac, av, "1234ivf")) != -1) {
138: switch (i) {
139: case '1':
140: case '2':
141: case '3':
142: case '4':
143: port = i - '1';
144: break;
145: case 'i':
1.3 rees 146: oflags |= STONOWAIT;
1.1 rees 147: break;
148: case 'v':
1.3 rees 149: vflag = 1;
1.1 rees 150: break;
151: case 'f':
152: rflags |= SCRFORCE;
153: break;
154: }
155: }
156:
157: if (fd < 0) {
1.3 rees 158: fd = sectok_open(port, oflags, &sw);
1.1 rees 159: if (fd < 0) {
1.3 rees 160: sectok_print_sw(sw);
1.1 rees 161: return -1;
162: }
163: }
164:
1.4 rees 165: aut0_vfyd = 0;
166:
1.3 rees 167: n = scxreset(fd, rflags, atr, &sw);
168: if (vflag)
169: parse_atr(fd, SCRV, atr, n, ¶m);
170: if (sw != SCEOK) {
171: printf("%s\n", scerrtab[sw]);
1.1 rees 172: return -1;
173: }
174:
175: return 0;
176: }
177:
178: int dclose(int ac, char *av[])
179: {
180: if (fd >= 0) {
181: scclose(fd);
182: fd = -1;
183: }
184: return 0;
185: }
186:
187: int quit(int ac, char *av[])
188: {
189: exit(0);
190: }
191:
192: int apdu(int ac, char *av[])
193: {
194: int i, n, ins, xcl = cla, p1, p2, p3, r1, r2;
195: unsigned char buf[256], obuf[256], *bp;
196:
197: optind = optreset = 1;
198:
199: while ((i = getopt(ac, av, "c:")) != -1) {
200: switch (i) {
201: case 'c':
202: sscanf(optarg, "%x", &xcl);
203: break;
204: }
205: }
206:
207: if (ac - optind < 4) {
1.4 rees 208: printf("usage: apdu [ -c class ] ins p1 p2 p3 data ...\n");
1.1 rees 209: return -1;
210: }
211:
212: sscanf(av[optind++], "%x", &ins);
213: sscanf(av[optind++], "%x", &p1);
214: sscanf(av[optind++], "%x", &p2);
215: sscanf(av[optind++], "%x", &p3);
216:
217: #if 0
218: for (bp = buf, i = optind; i < ac; i++)
219: bp += parse_input(av[i], bp, (int) (sizeof buf - (bp - buf)));
220: #else
221: for (bp = buf, i = optind; i < ac; i++) {
222: sscanf(av[i], "%x", &n);
223: *bp++ = n;
224: }
225: #endif
226:
227: if (fd < 0)
228: reset(0, NULL);
229:
230: n = scrw(fd, xcl, ins, p1, p2, p3, buf, sizeof obuf, obuf, &r1, &r2);
231:
232: if (n < 0) {
233: printf("scrw failed\n");
234: return -1;
235: }
236:
237: dump_reply(obuf, n, r1, r2);
238:
239: return 0;
240: }
241:
242: int selfid(int ac, char *av[])
243: {
244: unsigned char fid[2];
1.3 rees 245: int sw;
1.1 rees 246:
247: if (ac != 2) {
248: printf("usage: f fid\n");
249: return -1;
250: }
251:
252: if (fd < 0)
253: reset(0, NULL);
254:
255: sectok_parse_fname(av[1], fid);
1.3 rees 256: if (sectok_selectfile(fd, cla, fid, &sw) < 0) {
257: printf("selectfile: %s\n", sectok_get_sw(sw));
1.2 rees 258: return -1;
259: }
1.1 rees 260:
1.3 rees 261: return 0;
262: }
263:
264: int isearch(int ac, char *av[])
265: {
266: int i, r1, r2;
267: unsigned char buf[256];
268:
269: if (fd < 0)
270: reset(0, NULL);
271:
272: /* find instructions */
273: for (i = 0; i < 0xff; i += 2)
274: if (scread(fd, cla, i, 0, 0, 0, buf, &r1, &r2) == 0
275: && r1 != 0x6d && r1 != 0x6e)
276: printf("%02x %s %s\n", i, lookup_cmdname(i), get_r1r2s(r1, r2));
1.1 rees 277: return 0;
278: }
279:
280: int class(int ac, char *av[])
281: {
282: if (ac > 1)
283: sscanf(av[1], "%x", &cla);
284: else
285: printf("Class %02x\n", cla);
286: return 0;
287: }
288:
289: int dread(int ac, char *av[])
290: {
291: int n, p3, fsize, r1, r2;
292: unsigned char buf[CARDIOSIZE];
293:
294: if (ac != 2) {
295: printf("usage: read filesize\n");
296: return -1;
297: }
298:
299: sscanf(av[1], "%d", &fsize);
300:
301: if (fd < 0)
302: reset(0, NULL);
303:
304: for (p3 = 0; fsize && p3 < 100000; p3 += n) {
305: n = (fsize < CARDIOSIZE) ? fsize : CARDIOSIZE;
306: if (scread(fd, cla, 0xb0, p3 >> 8, p3 & 0xff, n, buf, &r1, &r2) < 0) {
307: printf("scread failed\n");
308: break;
309: }
310: if (r1 != 0x90 && r1 != 0x61) {
311: print_r1r2(r1, r2);
312: break;
313: }
314: fwrite(buf, 1, n, stdout);
315: fsize -= n;
316: }
317:
318: return 0;
319: }
320:
321: int dwrite(int ac, char *av[])
322: {
323: int n, p3, r1, r2;
324: FILE *f;
325: unsigned char buf[CARDIOSIZE];
326:
327: if (ac != 2) {
328: printf("usage: write input-filename\n");
329: return -1;
330: }
331:
332: if (fd < 0)
333: reset(0, NULL);
334:
335: f = fopen(av[1], "r");
336: if (!f) {
337: printf("can't open %s\n", av[1]);
338: return -1;
339: }
340:
341: n = 0;
342: while ((p3 = fread(buf, 1, CARDIOSIZE, f)) > 0) {
343: if (scwrite(fd, cla, 0xd6, n >> 8, n & 0xff, p3, buf, &r1, &r2) < 0) {
344: printf("scwrite failed\n");
345: break;
346: }
347: if (r1 != 0x90 && r1 != 0x61) {
348: print_r1r2(r1, r2);
349: break;
350: }
351: n += p3;
352: }
353: fclose(f);
354:
355: return (n ? 0 : -1);
356: }