comparison uicc/select.c @ 132:5af88fa11b54

fc-uicc-tool: SELECT response parsing implemented (basic)
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 04 Feb 2021 01:36:54 +0000
parents d0929cb42e1c
children 69628bcfec17
comparison
equal deleted inserted replaced
131:d0929cb42e1c 132:5af88fa11b54
54 return(-1); 54 return(-1);
55 } 55 }
56 return(0); 56 return(0);
57 } 57 }
58 58
59 select_resp_header_check(ret_offset, ret_length)
60 unsigned *ret_offset, *ret_length;
61 {
62 unsigned offset, len;
63
64 if (sim_resp_data_len < 2) {
65 tooshort: fprintf(stderr, "error: SELECT response is too short\n");
66 return(-1);
67 }
68 if (sim_resp_data[0] != 0x62) {
69 fprintf(stderr, "error: SELECT response first byte != 0x62\n");
70 return(-1);
71 }
72 len = sim_resp_data[1];
73 if (len <= 0x7F) {
74 offset = 2;
75 return_check: if (offset + len > sim_resp_data_len)
76 goto tooshort;
77 if (ret_offset)
78 *ret_offset = offset;
79 if (ret_length)
80 *ret_length = len;
81 return(0);
82 }
83 if (len != 0x81) {
84 fprintf(stderr, "SELECT response: first length byte is bad\n");
85 return(-1);
86 }
87 if (sim_resp_data_len < 3)
88 goto tooshort;
89 len = sim_resp_data[2];
90 offset = 3;
91 goto return_check;
92 }
93
94 parse_and_display_select_response()
95 {
96 unsigned offset, totlen, reclen, n;
97 u_char *dp, *endp;
98 int rc;
99
100 rc = select_resp_header_check(&offset, &totlen);
101 if (rc < 0)
102 return(rc);
103 dp = sim_resp_data + offset;
104 endp = sim_resp_data + offset + totlen;
105 while (dp < endp) {
106 if (endp - dp < 2) {
107 trunc_error: fprintf(stderr,
108 "error: truncated TLV record in SELECT response\n");
109 return(-1);
110 }
111 if ((dp[0] & 0x1F) == 0x1F) {
112 fprintf(stderr,
113 "error: extended tag not supported in SELECT response\n");
114 return(-1);
115 }
116 if (dp[1] & 0x80) {
117 fprintf(stderr,
118 "error: extended length not supported in SELECT response\n");
119 return(-1);
120 }
121 reclen = dp[1] + 2;
122 if (endp - dp < reclen)
123 goto trunc_error;
124 for (n = 0; n < reclen; n++) {
125 if (n)
126 putchar(' ');
127 printf("%02X", *dp++);
128 }
129 putchar('\n');
130 }
131 return(0);
132 }
133
59 cmd_select(argc, argv) 134 cmd_select(argc, argv)
60 char **argv; 135 char **argv;
61 { 136 {
62 int file_id; 137 int file_id, rc;
63 138
64 if (isxdigit(argv[1][0]) && isxdigit(argv[1][1]) && 139 if (isxdigit(argv[1][0]) && isxdigit(argv[1][1]) &&
65 isxdigit(argv[1][2]) && isxdigit(argv[1][3]) && !argv[1][4]) 140 isxdigit(argv[1][2]) && isxdigit(argv[1][3]) && !argv[1][4])
66 file_id = strtoul(argv[1], 0, 16); 141 file_id = strtoul(argv[1], 0, 16);
67 else 142 else
69 if (file_id < 0) { 144 if (file_id < 0) {
70 fprintf(stderr, 145 fprintf(stderr,
71 "error: file ID argument is not a hex value or a recognized symbolic name\n"); 146 "error: file ID argument is not a hex value or a recognized symbolic name\n");
72 return(-1); 147 return(-1);
73 } 148 }
74 return select_op(file_id); 149 rc = select_op(file_id);
150 if (rc < 0)
151 return(rc);
152 return parse_and_display_select_response();
75 } 153 }