]>
Commit | Line | Data |
---|---|---|
111a38b0 RR |
1 | /* |
2 | * implement the Java card standard. | |
3 | * | |
4 | * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. | |
5 | * See the COPYING.LIB file in the top-level directory. | |
6 | */ | |
7 | ||
8 | #include "qemu-common.h" | |
9 | ||
10 | #include "vcard.h" | |
11 | #include "vcard_emul.h" | |
12 | #include "card_7816t.h" | |
13 | ||
14 | struct VCardAppletStruct { | |
15 | VCardApplet *next; | |
16 | VCardProcessAPDU process_apdu; | |
17 | VCardResetApplet reset_applet; | |
18 | unsigned char *aid; | |
19 | int aid_len; | |
20 | void *applet_private; | |
21 | VCardAppletPrivateFree applet_private_free; | |
22 | }; | |
23 | ||
24 | struct VCardStruct { | |
25 | int reference_count; | |
26 | VCardApplet *applet_list; | |
27 | VCardApplet *current_applet[MAX_CHANNEL]; | |
28 | VCardBufferResponse *vcard_buffer_response; | |
29 | VCardType type; | |
30 | VCardEmul *vcard_private; | |
31 | VCardEmulFree vcard_private_free; | |
32 | VCardGetAtr vcard_get_atr; | |
33 | }; | |
34 | ||
35 | VCardBufferResponse * | |
36 | vcard_buffer_response_new(unsigned char *buffer, int size) | |
37 | { | |
38 | VCardBufferResponse *new_buffer; | |
39 | ||
78a4b8d2 MT |
40 | new_buffer = g_new(VCardBufferResponse, 1); |
41 | new_buffer->buffer = (unsigned char *)g_memdup(buffer, size); | |
111a38b0 RR |
42 | new_buffer->buffer_len = size; |
43 | new_buffer->current = new_buffer->buffer; | |
44 | new_buffer->len = size; | |
45 | return new_buffer; | |
46 | } | |
47 | ||
48 | void | |
49 | vcard_buffer_response_delete(VCardBufferResponse *buffer_response) | |
50 | { | |
51 | if (buffer_response == NULL) { | |
52 | return; | |
53 | } | |
ec15993d | 54 | g_free(buffer_response->buffer); |
7267c094 | 55 | g_free(buffer_response); |
111a38b0 RR |
56 | } |
57 | ||
58 | ||
59 | /* | |
60 | * clean up state after a reset | |
61 | */ | |
62 | void | |
63 | vcard_reset(VCard *card, VCardPower power) | |
64 | { | |
65 | int i; | |
66 | VCardApplet *applet = NULL; | |
67 | ||
68 | if (card->type == VCARD_DIRECT) { | |
69 | /* select the last applet */ | |
70 | VCardApplet *current_applet = NULL; | |
71 | for (current_applet = card->applet_list; current_applet; | |
72 | current_applet = current_applet->next) { | |
73 | applet = current_applet; | |
74 | } | |
75 | } | |
76 | for (i = 0; i < MAX_CHANNEL; i++) { | |
77 | card->current_applet[i] = applet; | |
78 | } | |
79 | if (card->vcard_buffer_response) { | |
80 | vcard_buffer_response_delete(card->vcard_buffer_response); | |
81 | card->vcard_buffer_response = NULL; | |
82 | } | |
83 | vcard_emul_reset(card, power); | |
84 | if (applet) { | |
85 | applet->reset_applet(card, 0); | |
86 | } | |
87 | } | |
88 | ||
89 | /* applet utilities */ | |
90 | ||
91 | /* | |
92 | * applet utilities | |
93 | */ | |
94 | /* constructor */ | |
95 | VCardApplet * | |
96 | vcard_new_applet(VCardProcessAPDU applet_process_function, | |
97 | VCardResetApplet applet_reset_function, | |
98 | unsigned char *aid, int aid_len) | |
99 | { | |
100 | VCardApplet *applet; | |
101 | ||
78a4b8d2 | 102 | applet = g_new0(VCardApplet, 1); |
111a38b0 RR |
103 | applet->process_apdu = applet_process_function; |
104 | applet->reset_applet = applet_reset_function; | |
105 | ||
78a4b8d2 | 106 | applet->aid = g_memdup(aid, aid_len); |
111a38b0 RR |
107 | applet->aid_len = aid_len; |
108 | return applet; | |
109 | } | |
110 | ||
111 | /* destructor */ | |
112 | void | |
113 | vcard_delete_applet(VCardApplet *applet) | |
114 | { | |
115 | if (applet == NULL) { | |
116 | return; | |
117 | } | |
118 | if (applet->applet_private_free) { | |
119 | applet->applet_private_free(applet->applet_private); | |
111a38b0 | 120 | } |
ec15993d | 121 | g_free(applet->aid); |
7267c094 | 122 | g_free(applet); |
111a38b0 RR |
123 | } |
124 | ||
125 | /* accessor */ | |
126 | void | |
127 | vcard_set_applet_private(VCardApplet *applet, VCardAppletPrivate *private, | |
128 | VCardAppletPrivateFree private_free) | |
129 | { | |
130 | if (applet->applet_private_free) { | |
131 | applet->applet_private_free(applet->applet_private); | |
132 | } | |
133 | applet->applet_private = private; | |
134 | applet->applet_private_free = private_free; | |
135 | } | |
136 | ||
137 | VCard * | |
138 | vcard_new(VCardEmul *private, VCardEmulFree private_free) | |
139 | { | |
140 | VCard *new_card; | |
111a38b0 | 141 | |
78a4b8d2 | 142 | new_card = g_new0(VCard, 1); |
111a38b0 RR |
143 | new_card->type = VCARD_VM; |
144 | new_card->vcard_private = private; | |
145 | new_card->vcard_private_free = private_free; | |
111a38b0 RR |
146 | new_card->reference_count = 1; |
147 | return new_card; | |
148 | } | |
149 | ||
150 | VCard * | |
151 | vcard_reference(VCard *vcard) | |
152 | { | |
153 | if (vcard == NULL) { | |
154 | return NULL; | |
155 | } | |
156 | vcard->reference_count++; | |
157 | return vcard; | |
158 | } | |
159 | ||
160 | void | |
161 | vcard_free(VCard *vcard) | |
162 | { | |
1687a089 MT |
163 | VCardApplet *current_applet; |
164 | VCardApplet *next_applet; | |
111a38b0 RR |
165 | |
166 | if (vcard == NULL) { | |
167 | return; | |
168 | } | |
169 | vcard->reference_count--; | |
170 | if (vcard->reference_count != 0) { | |
171 | return; | |
172 | } | |
173 | if (vcard->vcard_private_free) { | |
174 | (*vcard->vcard_private_free)(vcard->vcard_private); | |
111a38b0 RR |
175 | } |
176 | for (current_applet = vcard->applet_list; current_applet; | |
177 | current_applet = next_applet) { | |
178 | next_applet = current_applet->next; | |
179 | vcard_delete_applet(current_applet); | |
180 | } | |
181 | vcard_buffer_response_delete(vcard->vcard_buffer_response); | |
7267c094 | 182 | g_free(vcard); |
111a38b0 RR |
183 | } |
184 | ||
185 | void | |
186 | vcard_get_atr(VCard *vcard, unsigned char *atr, int *atr_len) | |
187 | { | |
188 | if (vcard->vcard_get_atr) { | |
189 | (*vcard->vcard_get_atr)(vcard, atr, atr_len); | |
190 | return; | |
191 | } | |
192 | vcard_emul_get_atr(vcard, atr, atr_len); | |
193 | } | |
194 | ||
195 | void | |
196 | vcard_set_atr_func(VCard *card, VCardGetAtr vcard_get_atr) | |
197 | { | |
198 | card->vcard_get_atr = vcard_get_atr; | |
199 | } | |
200 | ||
201 | ||
202 | VCardStatus | |
203 | vcard_add_applet(VCard *card, VCardApplet *applet) | |
204 | { | |
205 | applet->next = card->applet_list; | |
206 | card->applet_list = applet; | |
207 | /* if our card-type is direct, always call the applet */ | |
208 | if (card->type == VCARD_DIRECT) { | |
209 | int i; | |
210 | ||
211 | for (i = 0; i < MAX_CHANNEL; i++) { | |
212 | card->current_applet[i] = applet; | |
213 | } | |
214 | } | |
215 | return VCARD_DONE; | |
216 | } | |
217 | ||
218 | /* | |
219 | * manage applets | |
220 | */ | |
221 | VCardApplet * | |
222 | vcard_find_applet(VCard *card, unsigned char *aid, int aid_len) | |
223 | { | |
224 | VCardApplet *current_applet; | |
225 | ||
226 | for (current_applet = card->applet_list; current_applet; | |
227 | current_applet = current_applet->next) { | |
228 | if (current_applet->aid_len != aid_len) { | |
229 | continue; | |
230 | } | |
231 | if (memcmp(current_applet->aid, aid, aid_len) == 0) { | |
232 | break; | |
233 | } | |
234 | } | |
235 | return current_applet; | |
236 | } | |
237 | ||
238 | unsigned char * | |
239 | vcard_applet_get_aid(VCardApplet *applet, int *aid_len) | |
240 | { | |
241 | if (applet == NULL) { | |
242 | return NULL; | |
243 | } | |
244 | *aid_len = applet->aid_len; | |
245 | return applet->aid; | |
246 | } | |
247 | ||
248 | ||
249 | void | |
250 | vcard_select_applet(VCard *card, int channel, VCardApplet *applet) | |
251 | { | |
252 | assert(channel < MAX_CHANNEL); | |
1223bc4c RS |
253 | |
254 | /* If using an emulated card, make sure to log out of any already logged in | |
255 | * session. */ | |
256 | vcard_emul_logout(card); | |
257 | ||
111a38b0 RR |
258 | card->current_applet[channel] = applet; |
259 | /* reset the applet */ | |
260 | if (applet && applet->reset_applet) { | |
261 | applet->reset_applet(card, channel); | |
262 | } | |
263 | } | |
264 | ||
265 | VCardAppletPrivate * | |
266 | vcard_get_current_applet_private(VCard *card, int channel) | |
267 | { | |
268 | VCardApplet *applet = card->current_applet[channel]; | |
269 | ||
270 | if (applet == NULL) { | |
271 | return NULL; | |
272 | } | |
273 | return applet->applet_private; | |
274 | } | |
275 | ||
276 | VCardStatus | |
277 | vcard_process_applet_apdu(VCard *card, VCardAPDU *apdu, | |
278 | VCardResponse **response) | |
279 | { | |
280 | if (card->current_applet[apdu->a_channel]) { | |
281 | return card->current_applet[apdu->a_channel]->process_apdu( | |
282 | card, apdu, response); | |
283 | } | |
284 | return VCARD_NEXT; | |
285 | } | |
286 | ||
287 | /* | |
288 | * Accessor functions | |
289 | */ | |
290 | /* accessor functions for the response buffer */ | |
291 | VCardBufferResponse * | |
292 | vcard_get_buffer_response(VCard *card) | |
293 | { | |
294 | return card->vcard_buffer_response; | |
295 | } | |
296 | ||
297 | void | |
298 | vcard_set_buffer_response(VCard *card, VCardBufferResponse *buffer) | |
299 | { | |
300 | card->vcard_buffer_response = buffer; | |
301 | } | |
302 | ||
303 | ||
304 | /* accessor functions for the type */ | |
305 | VCardType | |
306 | vcard_get_type(VCard *card) | |
307 | { | |
308 | return card->type; | |
309 | } | |
310 | ||
311 | void | |
312 | vcard_set_type(VCard *card, VCardType type) | |
313 | { | |
314 | card->type = type; | |
315 | } | |
316 | ||
317 | /* accessor for private data */ | |
318 | VCardEmul * | |
319 | vcard_get_private(VCard *vcard) | |
320 | { | |
321 | return vcard->vcard_private; | |
322 | } | |
323 |