2 * implement the Java card standard.
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.
8 #include "qemu-common.h"
11 #include "vcard_emul.h"
12 #include "card_7816t.h"
14 struct VCardAppletStruct {
16 VCardProcessAPDU process_apdu;
17 VCardResetApplet reset_applet;
21 VCardAppletPrivateFree applet_private_free;
26 VCardApplet *applet_list;
27 VCardApplet *current_applet[MAX_CHANNEL];
28 VCardBufferResponse *vcard_buffer_response;
30 VCardEmul *vcard_private;
31 VCardEmulFree vcard_private_free;
32 VCardGetAtr vcard_get_atr;
36 vcard_buffer_response_new(unsigned char *buffer, int size)
38 VCardBufferResponse *new_buffer;
40 new_buffer = g_new(VCardBufferResponse, 1);
41 new_buffer->buffer = (unsigned char *)g_memdup(buffer, size);
42 new_buffer->buffer_len = size;
43 new_buffer->current = new_buffer->buffer;
44 new_buffer->len = size;
49 vcard_buffer_response_delete(VCardBufferResponse *buffer_response)
51 if (buffer_response == NULL) {
54 g_free(buffer_response->buffer);
55 g_free(buffer_response);
60 * clean up state after a reset
63 vcard_reset(VCard *card, VCardPower power)
66 VCardApplet *applet = NULL;
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;
76 for (i = 0; i < MAX_CHANNEL; i++) {
77 card->current_applet[i] = applet;
79 if (card->vcard_buffer_response) {
80 vcard_buffer_response_delete(card->vcard_buffer_response);
81 card->vcard_buffer_response = NULL;
83 vcard_emul_reset(card, power);
85 applet->reset_applet(card, 0);
89 /* applet utilities */
96 vcard_new_applet(VCardProcessAPDU applet_process_function,
97 VCardResetApplet applet_reset_function,
98 unsigned char *aid, int aid_len)
102 applet = g_new0(VCardApplet, 1);
103 applet->process_apdu = applet_process_function;
104 applet->reset_applet = applet_reset_function;
106 applet->aid = g_memdup(aid, aid_len);
107 applet->aid_len = aid_len;
113 vcard_delete_applet(VCardApplet *applet)
115 if (applet == NULL) {
118 if (applet->applet_private_free) {
119 applet->applet_private_free(applet->applet_private);
120 applet->applet_private = NULL;
129 vcard_set_applet_private(VCardApplet *applet, VCardAppletPrivate *private,
130 VCardAppletPrivateFree private_free)
132 if (applet->applet_private_free) {
133 applet->applet_private_free(applet->applet_private);
135 applet->applet_private = private;
136 applet->applet_private_free = private_free;
140 vcard_new(VCardEmul *private, VCardEmulFree private_free)
144 new_card = g_new0(VCard, 1);
145 new_card->type = VCARD_VM;
146 new_card->vcard_private = private;
147 new_card->vcard_private_free = private_free;
148 new_card->reference_count = 1;
153 vcard_reference(VCard *vcard)
158 vcard->reference_count++;
163 vcard_free(VCard *vcard)
165 VCardApplet *current_applet;
166 VCardApplet *next_applet;
171 vcard->reference_count--;
172 if (vcard->reference_count != 0) {
175 if (vcard->vcard_private_free) {
176 (*vcard->vcard_private_free)(vcard->vcard_private);
177 vcard->vcard_private_free = 0;
178 vcard->vcard_private = 0;
180 for (current_applet = vcard->applet_list; current_applet;
181 current_applet = next_applet) {
182 next_applet = current_applet->next;
183 vcard_delete_applet(current_applet);
185 vcard_buffer_response_delete(vcard->vcard_buffer_response);
190 vcard_get_atr(VCard *vcard, unsigned char *atr, int *atr_len)
192 if (vcard->vcard_get_atr) {
193 (*vcard->vcard_get_atr)(vcard, atr, atr_len);
196 vcard_emul_get_atr(vcard, atr, atr_len);
200 vcard_set_atr_func(VCard *card, VCardGetAtr vcard_get_atr)
202 card->vcard_get_atr = vcard_get_atr;
207 vcard_add_applet(VCard *card, VCardApplet *applet)
209 applet->next = card->applet_list;
210 card->applet_list = applet;
211 /* if our card-type is direct, always call the applet */
212 if (card->type == VCARD_DIRECT) {
215 for (i = 0; i < MAX_CHANNEL; i++) {
216 card->current_applet[i] = applet;
226 vcard_find_applet(VCard *card, unsigned char *aid, int aid_len)
228 VCardApplet *current_applet;
230 for (current_applet = card->applet_list; current_applet;
231 current_applet = current_applet->next) {
232 if (current_applet->aid_len != aid_len) {
235 if (memcmp(current_applet->aid, aid, aid_len) == 0) {
239 return current_applet;
243 vcard_applet_get_aid(VCardApplet *applet, int *aid_len)
245 if (applet == NULL) {
248 *aid_len = applet->aid_len;
254 vcard_select_applet(VCard *card, int channel, VCardApplet *applet)
256 assert(channel < MAX_CHANNEL);
257 card->current_applet[channel] = applet;
258 /* reset the applet */
259 if (applet && applet->reset_applet) {
260 applet->reset_applet(card, channel);
265 vcard_get_current_applet_private(VCard *card, int channel)
267 VCardApplet *applet = card->current_applet[channel];
269 if (applet == NULL) {
272 return applet->applet_private;
276 vcard_process_applet_apdu(VCard *card, VCardAPDU *apdu,
277 VCardResponse **response)
279 if (card->current_applet[apdu->a_channel]) {
280 return card->current_applet[apdu->a_channel]->process_apdu(
281 card, apdu, response);
289 /* accessor functions for the response buffer */
290 VCardBufferResponse *
291 vcard_get_buffer_response(VCard *card)
293 return card->vcard_buffer_response;
297 vcard_set_buffer_response(VCard *card, VCardBufferResponse *buffer)
299 card->vcard_buffer_response = buffer;
303 /* accessor functions for the type */
305 vcard_get_type(VCard *card)
311 vcard_set_type(VCard *card, VCardType type)
316 /* accessor for private data */
318 vcard_get_private(VCard *vcard)
320 return vcard->vcard_private;