comparison simtool/dumpdir.c @ 143:431194b772e1

fc-simtool: uicc-dump command ported over
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 04 Feb 2021 05:12:24 +0000
parents
children
comparison
equal deleted inserted replaced
142:74c5dc7408e9 143:431194b772e1
1 /*
2 * This module implements the dump of EF_DIR.
3 */
4
5 #include <sys/types.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <pcsclite.h>
9 #include <winscard.h>
10 #include "globals.h"
11 #include "file_id.h"
12
13 static
14 check_all_blank()
15 {
16 u_char *dp, *endp;
17
18 dp = sim_resp_data;
19 endp = sim_resp_data + sim_resp_data_len;
20 while (dp < endp)
21 if (*dp++ != 0xFF)
22 return(0);
23 return(1);
24 }
25
26 static void
27 dump_aid(tlv)
28 u_char *tlv;
29 {
30 unsigned reclen, n;
31
32 reclen = tlv[1];
33 printf(" AID:");
34 for (n = 0; n < reclen; n++)
35 printf(" %02X", tlv[n+2]);
36 putchar('\n');
37 }
38
39 static void
40 dump_label(tlv)
41 u_char *tlv;
42 {
43 int rc;
44 unsigned textlen;
45
46 printf(" Label: ");
47 rc = validate_alpha_field(tlv + 2, tlv[1], &textlen);
48 if (rc < 0) {
49 printf("malformed\n");
50 return;
51 }
52 print_alpha_field(tlv + 2, textlen, stdout);
53 putchar('\n');
54 }
55
56 static void
57 dump_unknown_tlv(tlv)
58 u_char *tlv;
59 {
60 unsigned reclen, n;
61
62 reclen = tlv[1] + 2;
63 printf(" TLV:");
64 for (n = 0; n < reclen; n++)
65 printf(" %02X", tlv[n]);
66 putchar('\n');
67 }
68
69 static void
70 dump_record(recno)
71 unsigned recno;
72 {
73 unsigned totlen, reclen;
74 u_char *dp, *endp;
75
76 printf("Record #%u:\n", recno);
77 if (sim_resp_data[0] != 0x61) {
78 printf(" bad: first byte != 0x61\n");
79 return;
80 }
81 totlen = sim_resp_data[1];
82 if (totlen < 3 || totlen > 0x7F) {
83 printf(" bad: global length byte 0x%02X is invalid\n", totlen);
84 return;
85 }
86 if (totlen + 2 > sim_resp_data_len) {
87 printf(" bad: TLV global length exceeds EF record length\n");
88 return;
89 }
90 dp = sim_resp_data + 2;
91 endp = sim_resp_data + 2 + totlen;
92 while (dp < endp) {
93 if (endp - dp < 2) {
94 trunc_error: printf(" bad: truncated TLV record\n");
95 return;
96 }
97 if ((dp[0] & 0x1F) == 0x1F) {
98 printf(" bad: extended tag not supported\n");
99 return;
100 }
101 if (dp[1] & 0x80) {
102 printf(" bad: extended length not supported\n");
103 return;
104 }
105 reclen = dp[1] + 2;
106 if (endp - dp < reclen)
107 goto trunc_error;
108 switch (dp[0]) {
109 case 0x4F:
110 dump_aid(dp);
111 break;
112 case 0x50:
113 dump_label(dp);
114 break;
115 default:
116 dump_unknown_tlv(dp);
117 }
118 dp += reclen;
119 }
120 }
121
122 cmd_uicc_dir()
123 {
124 int rc;
125 unsigned recno;
126
127 rc = select_op(FILEID_MF);
128 if (rc < 0)
129 return(rc);
130 rc = select_op(EF_DIR);
131 if (rc < 0)
132 return(rc);
133 rc = parse_ef_select_response();
134 if (rc < 0)
135 return(rc);
136 if (curfile_structure != 0x01) {
137 fprintf(stderr, "error: EF_DIR is not linear fixed\n");
138 return(-1);
139 }
140 if (curfile_record_len < 5) {
141 fprintf(stderr, "error: EF_DIR record length is too short\n");
142 return(-1);
143 }
144 for (recno = 1; recno <= curfile_record_count; recno++) {
145 rc = readrec_op(recno, 0x04, curfile_record_len);
146 if (rc < 0)
147 return(rc);
148 if (check_all_blank())
149 continue;
150 dump_record(recno);
151 }
152 return(0);
153 }