summaryrefslogtreecommitdiff
path: root/usr.bin/sectok/cmds.c
blob: fd3e709c22bcf3beff98203f0114cad157f1cda7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
/*	$OpenBSD: cmds.c,v 1.21 2003/04/04 00:42:34 deraadt Exp $ */

/*
 * Smartcard commander.
 * Written by Jim Rees and others at University of Michigan.
 */

/*
 * copyright 2001
 * the regents of the university of michigan
 * all rights reserved
 *
 * permission is granted to use, copy, create derivative works
 * and redistribute this software and such derivative works
 * for any purpose, so long as the name of the university of
 * michigan is not used in any advertising or publicity
 * pertaining to the use or distribution of this software
 * without specific, written prior authorization.  if the
 * above copyright notice or any other identification of the
 * university of michigan is included in any copy of any
 * portion of this software, then the disclaimer below must
 * also be included.
 *
 * this software is provided as is, without representation
 * from the university of michigan as to its fitness for any
 * purpose, and without warranty by the university of
 * michigan of any kind, either express or implied, including
 * without limitation the implied warranties of
 * merchantability and fitness for a particular purpose. the
 * regents of the university of michigan shall not be liable
 * for any damages, including special, indirect, incidental, or
 * consequential damages, with respect to any claim arising
 * out of or in connection with the use of the software, even
 * if it has been or is hereafter advised of the possibility of
 * such damages.
 */

#ifdef __palmos__
#pragma pack(2)
#include <Common.h>
#include <System/SysAll.h>
#include <UI/UIAll.h>
#include <System/Unix/sys_types.h>
#include <System/Unix/unix_stdio.h>
#include <System/Unix/unix_stdlib.h>
#include <System/Unix/unix_string.h>
#include <string.h>
#include "getopt.h"
#include "sectok.h"
#include "field.h"
#else
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <sectok.h>
#endif

#include "sc.h"

#define MAXFILELEN 0xffff
#define CARDIOSIZE 200

struct dispatchtable dispatch_table[] = {
	/* Non-card commands */
	{ "help", "[command]", help },
	{ "?", "[command]", help },
	{ "reset", "[-1234ivf]", reset },
	{ "open", "[-1234ivf]", reset },
	{ "close", "", dclose },
	{ "quit", "", quit },

	/* 7816-4 commands */
	{ "apdu", "[-c class] ins p1 p2 p3 data ...", apdu },
	{ "fid", "[-v] fid/aid", selfid },
	{ "isearch", "", isearch },
	{ "csearch", "", csearch },
	{ "class", "[class]", class },
	{ "read", "[-x] [filesize]", dread },
	{ "write", "input-filename", dwrite },
	{ "challenge", "[size]", challenge },
	{ "pin", "[-k keyno] [PIN]", vfypin },
#ifndef __palmos__
	{ "chpin", "[-k keyno]", chpin },
#endif

	/* Cyberflex commands */
	{ "ls", "[-l]", ls },
	{ "acl", "[-x] fid [principal: r1 r2 ...]", acl },
	{ "create", "fid size", jcreate },
	{ "delete", "fid", jdelete },
	{ "jdefault", "[-d]", jdefault },
	{ "jatr", "", jatr },
	{ "jdata", "", jdata },
	{ "login", "[-d] [-k keyno] [-v] [-x hex-aut0]", jlogin },
#ifndef __palmos__
	{ "jaut", "", jaut },
	{ "jload", "[-p progID] [-c contID] [-s cont_size] [-i inst_size] [-a aid] [-v] filename", jload },
#endif
	{ "junload", "[-p progID] [-c contID]", junload },
#ifndef __palmos__
	{ "setpass", "[-d] [-x hex-aut0]", jsetpass },
#endif
	{ NULL, NULL, NULL }
};

int     curlen;

int
dispatch(int argc, char *argv[])
{
	int     i;

	if (argc < 1)
		return 0;

	for (i = 0; dispatch_table[i].cmd; i++) {
		if (!strncmp(argv[0], dispatch_table[i].cmd, strlen(argv[0]))) {
			(dispatch_table[i].action) (argc, argv);
			break;
		}
	}
	if (!dispatch_table[i].cmd) {
		printf("unknown command \"%s\"\n", argv[0]);
		return -1;
	}
	return 0;
}

int
help(int argc, char *argv[])
{
	int     i, j;

	if (argc < 2) {
		for (i = 0; dispatch_table[i].cmd; i++)
			printf("%s\n", dispatch_table[i].cmd);
	} else {
		for (j = 1; j < argc; j++) {
			for (i = 0; dispatch_table[i].cmd; i++)
				if (!strncmp(argv[j], dispatch_table[i].cmd,
				    strlen(argv[j])))
					break;
			if (dispatch_table[i].help)
				printf("%s %s\n", dispatch_table[i].cmd,
				    dispatch_table[i].help);
			else
				printf("no help on \"%s\"\n", argv[j]);
		}
	}

	return 0;
}

int
reset(int argc, char *argv[])
{
	int     i, n, oflags = 0, rflags = 0, vflag = 0, sw;
	unsigned char atr[34];
	struct scparam param;

	optind = optreset = 1;

	while ((i = getopt(argc, argv, "0123ivf")) != -1) {
		switch (i) {
		case '0':
		case '1':
		case '2':
		case '3':
			port = i - '0';
			break;
		case 'i':
			oflags |= STONOWAIT;
			break;
		case 'v':
			vflag = 1;
			break;
		case 'f':
			rflags |= STRFORCE;
			break;
		}
	}

	if (fd < 0) {
		fd = sectok_open(port, oflags, &sw);
		if (fd < 0) {
			sectok_print_sw(sw);
			return -1;
		}
	}
	aut0_vfyd = 0;

	n = sectok_reset(fd, rflags, atr, &sw);
	if (vflag) {
#ifdef __palmos__
		hidefield(printfield->id);
		sectok_parse_atr(fd, STRV, atr, n, &param);
		showfield(printfield->id);
#else
		sectok_parse_atr(fd, STRV, atr, n, &param);
#endif
	}
	if (!sectok_swOK(sw)) {
		printf("sectok_reset: %s\n", sectok_get_sw(sw));
		dclose(0, NULL);
		return -1;
	}
	return 0;
}

int
dclose(int argc, char *argv[])
{
	if (fd >= 0) {
		sectok_close(fd);
		fd = -1;
	}
	return 0;
}

int
quit(int argc, char *argv[])
{
	dclose(0, NULL);
#ifndef __palmos__
	exit(0);
#else
	return -1;
#endif
}

int
apdu(int argc, char *argv[])
{
	int     i, ilen, olen, n, ins, xcl = cla, p1, p2, p3, sw;
	unsigned char ibuf[256], obuf[256], *bp;

	optind = optreset = 1;

	while ((i = getopt(argc, argv, "c:")) != -1) {
		switch (i) {
		case 'c':
			sscanf(optarg, "%x", &xcl);
			break;
		}
	}

	if (argc - optind < 4) {
		printf("usage: apdu [-c class] ins p1 p2 p3 data ...\n");
		return -1;
	}
	sscanf(argv[optind++], "%x", &ins);
	sscanf(argv[optind++], "%x", &p1);
	sscanf(argv[optind++], "%x", &p2);
	sscanf(argv[optind++], "%x", &p3);

	for (bp = ibuf, i = optind, ilen = 0; i < argc; i++) {
		sscanf(argv[i], "%x", &n);
		if (bp == &ibuf[sizeof ibuf-1]) {
			printf("truncation\n");
			break;
		}
		*bp++ = n;
		ilen++;
	}

	if (fd < 0 && reset(0, NULL) < 0)
		return -1;

	olen = (p3 && !ilen) ? p3 : sizeof obuf;

	n = sectok_apdu(fd, xcl, ins, p1, p2, ilen, ibuf, olen, obuf, &sw);

	sectok_dump_reply(obuf, n, sw);

	return 0;
}

int
selfid(int argc, char *argv[])
{
	unsigned char fid[16], obuf[256];
	char   *fname;
	int     i, n, sel, fidlen, vflag = 0, sw;

	optind = optreset = 1;

	while ((i = getopt(argc, argv, "v")) != -1) {
		switch (i) {
		case 'v':
			vflag = 1;
			break;
		}
	}

	if (argc - optind == 0) {
		/* No fid/aid given; select null aid (default loader for
		 * Cyberflex) */
		sel = 4;
		fidlen = 0;
	} else {
		fname = argv[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);
				if (fname[0] == '#') {
					/* Prepend 0xfc to the aid to make it
					 * a "proprietary aid". */
					fid[0] = 0xfc;
				}
			}
	}

	if (fd < 0 && reset(0, NULL) < 0)
		return -1;

	n = sectok_apdu(fd, cla, 0xa4, sel, 0, fidlen, fid, 256, obuf, &sw);
	if (!sectok_swOK(sw)) {
		printf("Select %02x%02x: %s\n", fid[0], fid[1], sectok_get_sw(sw));
		return -1;
	}
	if (vflag && !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 (n >= 4) {
		/* Some cards put the file length here. No guarantees. */
		curlen = (obuf[2] << 8) | obuf[3];
	}
	if (vflag)
		sectok_dump_reply(obuf, n, sw);

	return 0;
}

int
isearch(int argc, char *argv[])
{
	int     i, r1, sw;

	if (fd < 0 && reset(0, NULL) < 0)
		return -1;

	/* find instructions */
	for (i = 0; i < 0xff; i += 2) {
		sectok_apdu(fd, cla, i, 0, 0, 0, NULL, 0, NULL, &sw);
		r1 = sectok_r1(sw);
		if (r1 != 0x06 && r1 != 0x6d && r1 != 0x6e)
			printf("%02x %s %s\n", i, sectok_get_ins(i),
			    sectok_get_sw(sw));
	}
	return 0;
}

int
csearch(int argc, char *argv[])
{
	int     i, r1, sw;

	if (fd < 0 && reset(0, NULL) < 0)
		return -1;

	/* find app classes */
	for (i = 0; i <= 0xff; i++) {
		sectok_apdu(fd, i, 0xa4, 0, 0, 2, root_fid, 0, NULL, &sw);
		r1 = sectok_r1(sw);
		if (r1 != 0x06 && r1 != 0x6d && r1 != 0x6e)
			printf("%02x %s\n", i, sectok_get_sw(sw));
	}
	return 0;
}

int
class(int argc, char *argv[])
{
	if (argc > 1)
		sscanf(argv[1], "%x", &cla);
	else
		printf("Class %02x\n", cla);
	return 0;
}

int
dread(int argc, char *argv[])
{
	int     i, n, col = 0, fsize, xflag = 0, sw;
	unsigned int p3;
	unsigned char buf[CARDIOSIZE + 1];

	optind = optreset = 1;

	while ((i = getopt(argc, argv, "x")) != -1) {
		switch (i) {
		case 'x':
			xflag = 1;
			break;
		}
	}

	if (argc - optind < 1)
		fsize = curlen;
	else
		sscanf(argv[optind++], "%d", &fsize);

	if (!fsize) {
		printf("please specify filesize\n");
		return -1;
	}
	if (fd < 0 && reset(0, NULL) < 0)
		return -1;

	for (p3 = 0; fsize && p3 < MAXFILELEN; p3 += n) {
		n = (fsize < CARDIOSIZE) ? fsize : CARDIOSIZE;
		n = sectok_apdu(fd, cla, 0xb0, p3 >> 8, p3 & 0xff, 0,
		    NULL, n, buf, &sw);
		if (!sectok_swOK(sw)) {
			printf("ReadBinary: %s\n", sectok_get_sw(sw));
			break;
		}
#ifdef __palmos__
		if (xflag) {
			hidefield(printfield->id);
			for (i = 0; i < n; i++) {
				printf("%02x ", buf[i]);
				if (col++ % 12 == 11)
					printf("\n");
			}
			showfield(printfield->id);
		} else {
			buf[n] = '\0';
			printf("%s", buf);
		}
#else
		if (xflag) {
			for (i = 0; i < n; i++) {
				printf("%02x ", buf[i]);
				if (col++ % 16 == 15)
					printf("\n");
			}
		} else
			fwrite(buf, 1, n, stdout);
#endif
		fsize -= n;
	}

	if (xflag && col % 16 != 0)
		printf("\n");

	return 0;
}

#ifndef __palmos__
int
dwrite(int argc, char *argv[])
{
	int     n, p3, sw;
	FILE   *f;
	unsigned char buf[CARDIOSIZE];

	if (argc != 2) {
		printf("usage: write input-filename\n");
		return -1;
	}
	if (fd < 0 && reset(0, NULL) < 0)
		return -1;

	f = fopen(argv[1], "r");
	if (!f) {
		printf("can't open %s\n", argv[1]);
		return -1;
	}
	n = 0;
	while ((p3 = fread(buf, 1, CARDIOSIZE, f)) > 0) {
		sectok_apdu(fd, cla, 0xd6, n >> 8, n & 0xff, p3, buf, 0, NULL, &sw);
		if (!sectok_swOK(sw)) {
			printf("UpdateBinary: %s\n", sectok_get_sw(sw));
			break;
		}
		n += p3;
	}
	fclose(f);

	return (n ? 0 : -1);
}

#else

int
dwrite(int argc, char *argv[])
{
	int     n, sw;
	char   *s;

	if (argc != 2) {
		printf("usage: write text\n");
		return -1;
	}
	s = argv[1];
	n = strlen(s);
	sectok_apdu(fd, cla, 0xd6, 0, 0, n, s, 0, NULL, &sw);
	if (!sectok_swOK(sw)) {
		printf("UpdateBinary: %s\n", sectok_get_sw(sw));
		return -1;
	}
	return 0;
}
#endif

int
challenge(int argc, char *argv[])
{
	int     n = 8, sw;
	unsigned char buf[256];

	if (argc > 1)
		n = atoi(argv[1]);

	n = sectok_apdu(fd, cla, 0x84, 0, 0, 0, NULL, n, buf, &sw);

	if (!sectok_swOK(sw)) {
		printf("GetChallenge: %s\n", sectok_get_sw(sw));
		return -1;
	}
	sectok_dump_reply(buf, n, sw);
	return 0;
}

int
vfypin(int argc, char *argv[])
{
	int     keyno = 1, i, sw;
	char   *pin;

	optind = optreset = 1;

	while ((i = getopt(argc, argv, "k:")) != -1) {
		switch (i) {
		case 'k':
			keyno = atoi(optarg);
			break;
		}
	}

	if (argc - optind >= 1)
		pin = argv[optind++];
	else {
#ifndef __palmos__
		pin = getpass("Enter PIN: ");
#else
		printf("usage: pin PIN\n");
		return -1;
#endif
	}

	sectok_apdu(fd, cla, 0x20, 0, keyno, strlen(pin), pin, 0, NULL, &sw);
	bzero(pin, strlen(pin));

	if (!sectok_swOK(sw)) {
		printf("VerifyCHV: %s\n", sectok_get_sw(sw));
		return -1;
	}
	return 0;
}

#ifndef __palmos__
int
chpin(int argc, char *argv[])
{
	int     keyno = 1, i, sw;
	char    pin[255];
	char	*pass;

	optind = optreset = 1;

	while ((i = getopt(argc, argv, "k:")) != -1) {
		switch (i) {
		case 'k':
			keyno = atoi(optarg);
			break;
		}
	}

	pass = getpass("Enter Old PIN: ");
	strlcpy(pin, pass, sizeof pin);
	pass = getpass("Enter New PIN: ");
	strlcat(pin, pass, sizeof pin);

	sectok_apdu(fd, cla, 0x24, 0, keyno, strlen(pin), pin, 0, NULL, &sw);
	bzero(pin, strlen(pin));

	if (!sectok_swOK(sw)) {
		printf("UpdateCHV: %s\n", sectok_get_sw(sw));
		return -1;
	}
	return 0;
}
#endif