aboutsummaryrefslogtreecommitdiffstats
path: root/host/opcd_sh.c
blob: c9b38b47114e466ec1387d1b15b071e16bb8c3ed (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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>

#include "zebvty/vty.h"
#include "zebvty/command.h"

#define MAX_BUFFER_SIZE		2048
#define MAX_LENC	MAX_BUFFER_SIZE
#define MAX_LENR	MAX_LENC
#define MAX_COMMAND_SIZE	2048

static void strcompact(char *str)
{
	int i, j = 0;

	for (i = 0; i < strlen(str); i++) {
		if (!isspace(str[i]))
			str[j++] = tolower(str[i]);
	}
	str[j] = 0;
}

static int strtobin(char *s, unsigned char *d, unsigned int *len)
{
	long l;
	int i, ret;

	if (*s == ':')
		s++;

	for (i = 0; i < (strlen(s) >> 1); i++) {
		if (i >= MAX_LENC || i >> *len)
			return 0;
		ret = sscanf(s + (i << 1), "%2lx", &l);
		if (ret != 1)
			return 0;
		d[i] = l & 0xff;
	}
	*len = i;

	return 1;
}

DEFUN(rc632_reg_read,
      rc632_reg_read_cmd, 
      "reg_read WORD", 
      "Read register of RC632\n")
{
#if 0
	if (send_hex_command(vty, argv[0]) < 0)
		return CMD_ERR_NO_MATCH;
#endif
	return CMD_SUCCESS;
}


#if 0
static int select_file(struct vty *v, int absolute, int path, 
			char *selector, unsigned int sellen, 
			unsigned char *rsp, unsigned int *rlen)
{
	unsigned char cmd[MAX_LENC];

	cmd[0] = 0x00;
	cmd[1] = 0xa4;
	if (absolute) {
		if (path)
			cmd[2] = 0x08;
		else
			cmd[2] = 0x00;
	} else {
		if (path)
			cmd[2] = 0x09;
		else
			cmd[2] = 0x02;
	}
	cmd[3] = 0x00; // FIXME: other templates

	cmd[4] = sellen & 0xff;

	memcpy(cmd[5], selector, sellen);
	cmd[5+sellen] = 0x00;

	send_apdu(hCard, v, cmd, 5+cmd[4], rsp, rlen);
	parse_selectfile_response(hCard, v, rsp, *rlen);
	return CMD_SUCCESS;

}

DEFUN(send_7816_select_file_absolute_fid,
	send_7816_select_file_absolute_fid_cmd,
	"select file absolute fid WORD",
	"Selects a file on the ICC\n")
{
	char *file = argv[0];
	unsigned char rsp[MAX_LENR];
	unsigned int lenr = MAX_LENR;
	unsigned char selector[255];
	unsigned int sellen = 255;

	if (strtobin(file, selector, &sellen) < 0)
		return CMD_ERR_NO_MATCH;
	
	return select_file(vty, 1, 0, selector, sellen, rsp, &lenr);
}

DEFUN(send_7816_select_file_absolute_path,
	send_7816_select_file_absolute_path_cmd,
	"select file absolute path WORD",
	"Selects a file on the ICC\n")
{
	char *file = argv[0];
	unsigned char rsp[MAX_LENR];
	unsigned int lenr = MAX_LENR;
	unsigned char selector[255];
	unsigned int sellen = 255;

	if (strtobin(file, selector, &sellen) < 0)
		return CMD_ERR_NO_MATCH;
	

	return select_file(vty, 1, 1, selector, sellen, rsp, &lenr);
}

DEFUN(send_7816_select_file_relative_fid,
	send_7816_select_file_relative_fid_cmd,
	"select file absolute fid WORD",
	"Selects a file on the ICC\n")
{
	char *file = argv[0];
	unsigned char rsp[MAX_LENR];
	unsigned int lenr = MAX_LENR;
	unsigned char selector[255];
	unsigned int sellen = 255;

	if (strtobin(file, selector, &sellen) < 0)
		return CMD_ERR_NO_MATCH;
	

	return select_file(vty, 0, 0, selector, sellen, rsp, &lenr);
}


DEFUN(send_7816_select_file_relative_path,
	send_7816_select_file_relative_path_cmd,
	"select file relative path WORD",
	"Selects a file on the ICC\n")
{
	char *file = argv[0];
	unsigned char rsp[MAX_LENR];
	unsigned int lenr = MAX_LENR;
	unsigned char selector[255];
	unsigned int sellen = 255;

	if (strtobin(file, selector, &sellen) < 0)
		return CMD_ERR_NO_MATCH;
	

	return select_file(vty, 0, 1, selector, sellen, rsp, &lenr);
}

DEFUN(send_7816_select_dir,
	send_7816_select_dir_cmd,
	"select directory (MF|PARENT|CHILD) (FID|AID)",
	"Selects a directory on the ICC\n")
{
	char *file = argv[1];
	char *type = argv[0];
	char cmd[22];
	char rsp[MAX_LENR];
	int len, lenr = MAX_LENR;
	int empty = 0;

	if (!type)
		return CMD_ERR_NO_MATCH;

	memset(cmd, 0, sizeof(cmd));
	cmd[1] = 0xa4;

	if (!strcmp(type, "MF")) {
		cmd[2] = 0x00;
		cmd[3] = 0x00;
		cmd[4] = 2;	/* length */
		cmd[5] = 0x3f;	/* 3ff0 */
		cmd[6] = 0xf0;
		empty = 1;
	} else if (!strcmp(type, "PARENT")) {
		cmd[2] = 0x03;
		cmd[3] = 0x00;
		cmd[4] = 0x00;
		empty = 1;
	} else if (!strcmp(file, "CHILD")) {
		cmd[2] = 0x01;
		cmd[3] = 0x00;
	} else {
		cmd[2] = 0x00;
		cmd[3] = 0x00;
	}

	if (!empty) {
		len = 16;
		/* convert hex string of identifier to bytecode */
		strtobin(file, &cmd[5], &len);
		cmd[4] = len & 0xff;
	}
	send_apdu(hCard, vty, cmd, 5+cmd[4], rsp, &lenr);
	parse_selectfile_response(hCard, vty, rsp, lenr);
	return CMD_SUCCESS;
}

DEFUN(send_7816_ls,
	send_7816_ls_cmd,
	"ls",
	"Tries to list all files on a 7816-4 compliant ICC\n")
{
	return CMD_SUCCESS;
}

DEFUN(send_7816_tree,
	send_7816_tree_cmd,
	"tree",
	"Tries to list a full DF hiararchy tree on a 7816-4 compliant ICC\n")
{

	return CMD_SUCCESS;
}

DEFUN(send_7816_read_binary,
	send_7816_read_binary_cmd,
	"read binary OFFSET LENGTH",
	"Read bytes form a previously selected EF\n")
{
	unsigned char cmd[] = { 0x00, 0xb0, 0x00, 0x00, 0x00 };
	unsigned char rsp[MAX_LENR]; // FIXME
	unsigned int lenr = MAX_LENR;
	
	unsigned long datalen;
	unsigned long offset;

	offset = strtoul(argv[0], NULL, 0);
	if (offset == ULONG_MAX || offset > 0xffff)
		return CMD_ERR_NO_MATCH;

	datalen = strtoul(argv[1], NULL, 0);
	if (datalen == ULONG_MAX || datalen > 0xff)
		return CMD_ERR_NO_MATCH;

	cmd[2] = (offset >> 8) & 0xff;
	cmd[3] = offset & 0xff;
	cmd[4] = datalen & 0xff;

	send_apdu(hCard, vty, cmd, 5+cmd[4], rsp, &lenr);
	if (lenr < 2)
		return CMD_SUCCESS; // FIXME

	parse_sw(vty, rsp[lenr-2], rsp[lenr-1]);

	return CMD_SUCCESS;
}

DEFUN(send_7816_write_binary,
	send_7816_write_binary_cmd,
	"write binary OFFSET LENGTH DATA",
	"Write bytes to a previously selected EF\n")
{
	unsigned char cmd[MAX_LENC];
	unsigned char rsp[MAX_LENR];
	unsigned int lenr;

	unsigned long datalen;
	unsigned long offset;

	offset = strtoul(argv[0], NULL, 0);
	if (offset == ULONG_MAX || offset > 0xffff)
		return CMD_ERR_NO_MATCH;

	datalen = strtoul(argv[1], NULL, 0);
	if (datalen == ULONG_MAX || datalen > 0xff)
		return CMD_ERR_NO_MATCH;

	memset(cmd, 0, sizeof(cmd));

	cmd[1] = 0xd0;
	cmd[2] = (offset >> 8) & 0xff;
	cmd[3] = offset & 0xff;

	if (!strtobin(argv[2], cmd+5, &datalen)) {
		vty_out(vty, "command decoding error%s", vty_newline(vty));
		return -1;
	}

	cmd[4] = datalen & 0xff;

	send_apdu(hCard, vty, cmd, 5+cmd[4], rsp, &lenr);
	if (lenr < 2)
		return CMD_SUCCESS; // FIXME

	parse_sw(vty, rsp[lenr-2], rsp[lenr-1]);

	return CMD_SUCCESS;

	return CMD_SUCCESS;
} 

DEFUN(send_7816_update_binary,
	send_7816_update_binary_cmd,
	"update binary OFFSET LENGTH DATA",
	"Update bytes of a previously selected EF\n")
{
	unsigned char cmd[MAX_LENC];
	unsigned char rsp[MAX_LENR];
	unsigned int lenr;

	unsigned long datalen;
	unsigned long offset;

	offset = strtoul(argv[0], NULL, 0);
	if (offset == ULONG_MAX || offset > 0xffff)
		return CMD_ERR_NO_MATCH;

	datalen = strtoul(argv[1], NULL, 0);
	if (datalen == ULONG_MAX || datalen > 0xff)
		return CMD_ERR_NO_MATCH;

	memset(cmd, 0, sizeof(cmd));

	cmd[1] = 0xd6;
	cmd[2] = (offset >> 8) & 0xff;
	cmd[3] = offset & 0xff;

	if (!strtobin(argv[2], cmd+5, &datalen)) {
		vty_out(vty, "command decoding error%s", vty_newline(vty));
		return -1;
	}

	cmd[4] = datalen & 0xff;

	send_apdu(hCard, vty, cmd, 5+cmd[4], rsp, &lenr);
	if (lenr < 2)
		return CMD_SUCCESS; // FIXME

	parse_sw(vty, rsp[lenr-2], rsp[lenr-1]);

	return CMD_SUCCESS;
} 

DEFUN(send_7816_erase_binary,
	send_7816_erase_binary_cmd,
	"erase binary OFFSET LENGTH",
	"Erase bytes of a previously selected EF\n")
{
	unsigned char cmd[8] = { 0x00, 0x0e, 0x00, 0x00,
				 0x00, 0x00, 0x00, 0x00 };
	unsigned char rsp[MAX_LENR];
	unsigned int lenr;

	unsigned long datalen;
	unsigned long offset;

	offset = strtoul(argv[0], NULL, 0);
	if (offset == ULONG_MAX || offset > 0xffff)
		return CMD_ERR_NO_MATCH;

	datalen = strtoul(argv[1], NULL, 0);
	if (datalen == ULONG_MAX || (offset+datalen > 0xffff))
		return CMD_ERR_NO_MATCH;

	cmd[2] = (offset >> 8) & 0xff;
	cmd[3] = offset & 0xff;
	cmd[4] = 0x02;
	cmd[5] = ((offset+datalen) >> 8) & 0xff;
	cmd[6] = (offset+datalen) & 0xff;

	send_apdu(hCard, vty, cmd, 5+cmd[4], rsp, &lenr);
	if (lenr < 2)
		return CMD_SUCCESS; // FIXME

	parse_sw(vty, rsp[lenr-2], rsp[lenr-1]);

	return CMD_SUCCESS;
}

DEFUN(send_7816_get_response,
	send_7816_get_response_cmd,
	"get response LENGTH",
	"Get more data from the ICC\n")
{
	unsigned char cmd[6] = { 0x00, 0xc0, 0x00, 0x00, 0x00, 0x00 };
	unsigned char rsp[MAX_LENR];
	unsigned int lenr = MAX_LENR;

	unsigned long length;

	length = strtoul(argv[0], NULL, 0);
	if (length == ULONG_MAX || length > 0xff)
		return CMD_ERR_NO_MATCH;

	cmd[5] = length & 0xff;

	send_apdu(hCard, vty, cmd, 6, rsp, &lenr);
	if (lenr < 2)
		return CMD_SUCCESS; // FIXME

	parse_sw(vty, rsp[lenr-2], rsp[lenr-1]);

	return CMD_SUCCESS;

}
#endif
 
DEFUN(rc632,
	rc632_cmd,
	"rc632", "Commands related to low-level RC632 access\n")
{
	vty->node = RC632_NODE;
	return CMD_SUCCESS;
}

/* dummy */
static int send_config_write(struct vty *v)
{
	return CMD_SUCCESS;
}

struct cmd_node rc632_node = {
	RC632_NODE,
	"%s(rc632)# ",
	1,
};

static int opcdshell_init()
{
	cmd_init(1);
	vty_init();

	install_node(&rc632_node, send_config_write);

	install_element(RC632_NODE, &rc632_reg_read_cmd);

#if 0
	install_element(RC632_NODE, &send_7816_select_file_absolute_fid_cmd);
	install_element(RC632_NODE, &send_7816_select_file_absolute_path_cmd);
	install_element(RC632_NODE, &send_7816_select_file_relative_fid_cmd);
	install_element(RC632_NODE, &send_7816_select_file_relative_path_cmd);

	install_element(RC632_NODE, &send_7816_select_dir_cmd);
	install_element(RC632_NODE, &send_7816_ls_cmd);
	install_element(RC632_NODE, &send_7816_tree_cmd);

	install_element(RC632_NODE, &send_7816_read_binary_cmd);
	install_element(RC632_NODE, &send_7816_write_binary_cmd);
	install_element(RC632_NODE, &send_7816_update_binary_cmd);
	install_element(RC632_NODE, &send_7816_erase_binary_cmd);

	install_element(RC632_NODE, &send_7816_get_response_cmd);
#endif

	install_default(RC632_NODE);

	install_element(VIEW_NODE, &rc632_cmd);

	return 0;
}

static int opcdshell(void)
{
	struct vty *v;

	v = vty_create(0);
	if (!v)
		return -1;
	while (1) {
		vty_read(v);
	}
	return 0;
}


/***********************************************************************
 * main program, copied from pcsc-lite 'testpcsc.c'
 ***********************************************************************/

int main(int argc, char **argv)
{
	opcdshell_init();

	printf("\nopcd_shell (C) 2006 Harald Welte <hwelte@hmw-consulting.de>\n\n");

	opcdshell();

	return 0;
}