-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcard.c
More file actions
221 lines (171 loc) · 3.94 KB
/
card.c
File metadata and controls
221 lines (171 loc) · 3.94 KB
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
/* Copyright (C) 2011 znuh <Zn000h AT gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdint.h>
#include <string.h>
#include <strings.h>
#include <stdio.h>
#include <endian.h>
#include <assert.h>
#include "terminal.h"
#include "utils.h"
extern int class_byte;
int get_status(uint8_t *buf, int len) {
apdu_t apdu;
int res;
apdu.cla = class_byte;
apdu.ins = 0xc0;
apdu.p1 = apdu.p2 = 0;
apdu.p3 = len;
apdu.din = buf;
apdu.dout = NULL;
res = term_apdu(&apdu);
return res;
}
int select_fid(uint16_t fid, uint8_t *resp, int resp_len) {
apdu_t apdu;
int res;
int get_len;
apdu.cla = class_byte;
apdu.ins = 0xa4;
apdu.p1 = apdu.p2 = 0;
// USIM
if(!class_byte)
apdu.p2=4;
apdu.p3 = 2;
fid = htobe16(fid);
apdu.din = NULL;
apdu.dout = (uint8_t *) &fid;
res = term_apdu(&apdu);
if(res < 0)
return res;
if((apdu.sw[0] == 0x94) && (apdu.sw[1] == 0x04))
return 0;
if((apdu.sw[0] == 0x6a) && (apdu.sw[1] == 0x82))
return 0;
if((apdu.sw[0] != 0x9f) && (apdu.sw[0] != 0x61)) {
fprintf(stderr, "select FID %02x SW: %02x%02x\n",
be16toh(fid),apdu.sw[0],apdu.sw[1]);
return -1;
}
get_len = apdu.sw[1];
if(resp) {
assert(resp_len >= get_len);
res = get_status(resp, get_len);
}
if(res < 0)
return res;
return get_len;
}
int read_binary(uint8_t *buf, int len) {
apdu_t apdu;
int rcnt=0, res;
apdu.cla = class_byte;
apdu.ins = 0xb0;
apdu.dout = NULL;
while(len) {
int chunklen = len > 255 ? 255 : len;
apdu.p1 = rcnt>>8;
apdu.p2 = rcnt&0xff;
apdu.din = buf;
apdu.p3 = chunklen;
res = term_apdu(&apdu);
if(res < 0)
return res;
if((apdu.sw[0] != 0x90) || (apdu.sw[1] != 0x00)) {
fprintf(stderr,"read ofs %d len %d %02x%02x\n",
rcnt, chunklen, apdu.sw[0], apdu.sw[1]);
return -1;
}
len -= chunklen;
buf += chunklen;
rcnt += chunklen;
}
return rcnt;
}
int read_record(uint8_t *buf, int len) {
apdu_t apdu;
int res;
apdu.cla = class_byte;
apdu.ins = 0xb2;
apdu.p1 = 0;
apdu.p2 = 2;
apdu.p3 = len;
apdu.din = buf;
apdu.dout = NULL;
res = term_apdu(&apdu);
if(res < 0)
return res;
// ok
if((apdu.sw[0] == 0x90) && (apdu.sw[1] == 0x00))
return len;
// last read
if((apdu.sw[0] == 0x94) && (apdu.sw[1] == 0x02))
return 0;
// access conditions
if((apdu.sw[0] == 0x98) && (apdu.sw[1] == 0x04))
return 0;
// if(apdu.sw[0] != 0x67) {
fprintf(stderr,"read rec sw %02x%02x\n",apdu.sw[0],apdu.sw[1]);
return 0;
// }
apdu.p3 = apdu.sw[1];
assert(len >= apdu.p3);
res = term_apdu(&apdu);
if(!res)
return apdu.p3;
else
return res;
}
int auth(char *pin) {
uint8_t buf[8];
apdu_t apdu;
int res;
memset(buf, 0xff, sizeof(buf));
memcpy(buf,pin, strlen(pin));
apdu.cla = class_byte;
apdu.ins = 0x20;
apdu.p1 = 0;
apdu.p2 = 1;
apdu.p3 = 8;
apdu.din = NULL;
apdu.dout = buf;
res = term_apdu(&apdu);
if(res < 0)
return res;
if((apdu.sw[0] == 0x90) && (apdu.sw[1] == 0))
return 0;
return -1;
}
int usim_detect(void) {
apdu_t apdu;
uint16_t fid;
int res;
apdu.cla = 0;
apdu.ins = 0xa4;
apdu.p1 = apdu.p2 = 0;
apdu.p3 = 2;
fid = htobe16(0x3f00);
apdu.din = NULL;
apdu.dout = (uint8_t *) &fid;
res = term_apdu(&apdu);
if(res < 0)
return res;
if((apdu.sw[0] == 0x6e) && (apdu.sw[1] == 0x00))
return 0;
else
return 1;
}