FreeCalypso > hg > fc-pcsc-tools
annotate uicc/createfile.c @ 158:3698c8192d2d
libutil: hex digit functions factored out
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Fri, 26 Feb 2021 20:41:31 +0000 |
parents | 878d66dce6d3 |
children |
rev | line source |
---|---|
139
c13ed9194ecd
fc-uicc-tool create-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
c13ed9194ecd
fc-uicc-tool create-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 * This module implements commands that exercise ETSI TS 102 222 |
c13ed9194ecd
fc-uicc-tool create-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 * CREATE FILE and DELETE FILE operations. |
c13ed9194ecd
fc-uicc-tool create-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 */ |
c13ed9194ecd
fc-uicc-tool create-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 |
c13ed9194ecd
fc-uicc-tool create-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 #include <sys/types.h> |
140
13ab44761ea6
fc-uicc-tool delete-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
139
diff
changeset
|
7 #include <ctype.h> |
139
c13ed9194ecd
fc-uicc-tool create-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 #include <stdio.h> |
140
13ab44761ea6
fc-uicc-tool delete-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
139
diff
changeset
|
9 #include <stdlib.h> |
139
c13ed9194ecd
fc-uicc-tool create-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 #include <string.h> |
c13ed9194ecd
fc-uicc-tool create-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 #include <strings.h> |
c13ed9194ecd
fc-uicc-tool create-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 #include "simresp.h" |
c13ed9194ecd
fc-uicc-tool create-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 |
c13ed9194ecd
fc-uicc-tool create-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 cmd_create_file(argc, argv) |
c13ed9194ecd
fc-uicc-tool create-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 char **argv; |
c13ed9194ecd
fc-uicc-tool create-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 { |
c13ed9194ecd
fc-uicc-tool create-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 u_char apdu[260], inbuf[252], *dp; |
c13ed9194ecd
fc-uicc-tool create-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 unsigned len1, len2; |
c13ed9194ecd
fc-uicc-tool create-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 int rc; |
c13ed9194ecd
fc-uicc-tool create-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 |
142
878d66dce6d3
fc-uicc-tool create-file needs to read the hex voodoo from a file
Mychaela Falconia <falcon@freecalypso.org>
parents:
140
diff
changeset
|
21 rc = read_hex_data_file(argv[1], inbuf, 252); |
139
c13ed9194ecd
fc-uicc-tool create-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 if (rc < 0) |
c13ed9194ecd
fc-uicc-tool create-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 return(rc); |
c13ed9194ecd
fc-uicc-tool create-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 len1 = rc; |
c13ed9194ecd
fc-uicc-tool create-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 dp = apdu + 5; |
c13ed9194ecd
fc-uicc-tool create-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 *dp++ = 0x62; |
c13ed9194ecd
fc-uicc-tool create-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 if (len1 < 0x80) { |
c13ed9194ecd
fc-uicc-tool create-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 *dp++ = len1; |
c13ed9194ecd
fc-uicc-tool create-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 len2 = len1 + 2; |
c13ed9194ecd
fc-uicc-tool create-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 } else { |
c13ed9194ecd
fc-uicc-tool create-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 *dp++ = 0x81; |
c13ed9194ecd
fc-uicc-tool create-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 *dp++ = len1; |
c13ed9194ecd
fc-uicc-tool create-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 len2 = len1 + 3; |
c13ed9194ecd
fc-uicc-tool create-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 } |
c13ed9194ecd
fc-uicc-tool create-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 bcopy(inbuf, dp, len1); |
c13ed9194ecd
fc-uicc-tool create-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 /* command header */ |
c13ed9194ecd
fc-uicc-tool create-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 apdu[0] = 0x00; |
c13ed9194ecd
fc-uicc-tool create-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 apdu[1] = 0xE0; |
c13ed9194ecd
fc-uicc-tool create-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 apdu[2] = 0; |
c13ed9194ecd
fc-uicc-tool create-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 apdu[3] = 0; |
c13ed9194ecd
fc-uicc-tool create-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 apdu[4] = len2; |
c13ed9194ecd
fc-uicc-tool create-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 rc = apdu_exchange(apdu, len2 + 5); |
c13ed9194ecd
fc-uicc-tool create-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 if (rc < 0) |
c13ed9194ecd
fc-uicc-tool create-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 return(rc); |
c13ed9194ecd
fc-uicc-tool create-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 if (sim_resp_sw != 0x9000) { |
c13ed9194ecd
fc-uicc-tool create-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 fprintf(stderr, "bad SW response: %04X\n", sim_resp_sw); |
c13ed9194ecd
fc-uicc-tool create-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 return(-1); |
c13ed9194ecd
fc-uicc-tool create-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 } |
c13ed9194ecd
fc-uicc-tool create-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 return(0); |
c13ed9194ecd
fc-uicc-tool create-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 } |
140
13ab44761ea6
fc-uicc-tool delete-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
139
diff
changeset
|
51 |
13ab44761ea6
fc-uicc-tool delete-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
139
diff
changeset
|
52 cmd_delete_file(argc, argv) |
13ab44761ea6
fc-uicc-tool delete-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
139
diff
changeset
|
53 char **argv; |
13ab44761ea6
fc-uicc-tool delete-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
139
diff
changeset
|
54 { |
13ab44761ea6
fc-uicc-tool delete-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
139
diff
changeset
|
55 u_char apdu[7]; |
13ab44761ea6
fc-uicc-tool delete-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
139
diff
changeset
|
56 unsigned file_id; |
13ab44761ea6
fc-uicc-tool delete-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
139
diff
changeset
|
57 int rc; |
13ab44761ea6
fc-uicc-tool delete-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
139
diff
changeset
|
58 |
13ab44761ea6
fc-uicc-tool delete-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
139
diff
changeset
|
59 if (!isxdigit(argv[1][0]) || !isxdigit(argv[1][1]) || |
13ab44761ea6
fc-uicc-tool delete-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
139
diff
changeset
|
60 !isxdigit(argv[1][2]) || !isxdigit(argv[1][3]) || argv[1][4]) { |
13ab44761ea6
fc-uicc-tool delete-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
139
diff
changeset
|
61 fprintf(stderr, "error: 4-digit hex argument required\n"); |
13ab44761ea6
fc-uicc-tool delete-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
139
diff
changeset
|
62 return(-1); |
13ab44761ea6
fc-uicc-tool delete-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
139
diff
changeset
|
63 } |
13ab44761ea6
fc-uicc-tool delete-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
139
diff
changeset
|
64 file_id = strtoul(argv[1], 0, 16); |
13ab44761ea6
fc-uicc-tool delete-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
139
diff
changeset
|
65 /* form command APDU */ |
13ab44761ea6
fc-uicc-tool delete-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
139
diff
changeset
|
66 apdu[0] = 0x00; |
13ab44761ea6
fc-uicc-tool delete-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
139
diff
changeset
|
67 apdu[1] = 0xE4; |
13ab44761ea6
fc-uicc-tool delete-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
139
diff
changeset
|
68 apdu[2] = 0; |
13ab44761ea6
fc-uicc-tool delete-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
139
diff
changeset
|
69 apdu[3] = 0; |
13ab44761ea6
fc-uicc-tool delete-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
139
diff
changeset
|
70 apdu[4] = 2; |
13ab44761ea6
fc-uicc-tool delete-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
139
diff
changeset
|
71 apdu[5] = file_id >> 8; |
13ab44761ea6
fc-uicc-tool delete-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
139
diff
changeset
|
72 apdu[6] = file_id; |
13ab44761ea6
fc-uicc-tool delete-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
139
diff
changeset
|
73 rc = apdu_exchange(apdu, 7); |
13ab44761ea6
fc-uicc-tool delete-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
139
diff
changeset
|
74 if (rc < 0) |
13ab44761ea6
fc-uicc-tool delete-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
139
diff
changeset
|
75 return(rc); |
13ab44761ea6
fc-uicc-tool delete-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
139
diff
changeset
|
76 if (sim_resp_sw != 0x9000) { |
13ab44761ea6
fc-uicc-tool delete-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
139
diff
changeset
|
77 fprintf(stderr, "bad SW response: %04X\n", sim_resp_sw); |
13ab44761ea6
fc-uicc-tool delete-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
139
diff
changeset
|
78 return(-1); |
13ab44761ea6
fc-uicc-tool delete-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
139
diff
changeset
|
79 } |
13ab44761ea6
fc-uicc-tool delete-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
139
diff
changeset
|
80 return(0); |
13ab44761ea6
fc-uicc-tool delete-file implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
139
diff
changeset
|
81 } |