blob: 1a87208f3de6fe72496b3a750d40aa63d3b3f519 [file] [log] [blame]
Robert Relyea111a38b2010-11-28 16:36:38 +02001/*
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
Paolo Bonzini28507a42015-04-27 12:34:18 +02008#include "glib-compat.h"
9
10#include <string.h>
Robert Relyea111a38b2010-11-28 16:36:38 +020011
12#include "vcard.h"
13#include "vcard_emul.h"
14#include "card_7816t.h"
15
16struct VCardAppletStruct {
17 VCardApplet *next;
18 VCardProcessAPDU process_apdu;
19 VCardResetApplet reset_applet;
20 unsigned char *aid;
21 int aid_len;
22 void *applet_private;
23 VCardAppletPrivateFree applet_private_free;
24};
25
26struct VCardStruct {
27 int reference_count;
28 VCardApplet *applet_list;
29 VCardApplet *current_applet[MAX_CHANNEL];
30 VCardBufferResponse *vcard_buffer_response;
31 VCardType type;
32 VCardEmul *vcard_private;
33 VCardEmulFree vcard_private_free;
34 VCardGetAtr vcard_get_atr;
35};
36
37VCardBufferResponse *
38vcard_buffer_response_new(unsigned char *buffer, int size)
39{
40 VCardBufferResponse *new_buffer;
41
Michael Tokarev78a4b8d2014-05-08 19:51:01 +040042 new_buffer = g_new(VCardBufferResponse, 1);
43 new_buffer->buffer = (unsigned char *)g_memdup(buffer, size);
Robert Relyea111a38b2010-11-28 16:36:38 +020044 new_buffer->buffer_len = size;
45 new_buffer->current = new_buffer->buffer;
46 new_buffer->len = size;
47 return new_buffer;
48}
49
50void
51vcard_buffer_response_delete(VCardBufferResponse *buffer_response)
52{
53 if (buffer_response == NULL) {
54 return;
55 }
Markus Armbrusterec159932014-06-06 18:32:08 +020056 g_free(buffer_response->buffer);
Anthony Liguori7267c092011-08-20 22:09:37 -050057 g_free(buffer_response);
Robert Relyea111a38b2010-11-28 16:36:38 +020058}
59
60
61/*
62 * clean up state after a reset
63 */
64void
65vcard_reset(VCard *card, VCardPower power)
66{
67 int i;
68 VCardApplet *applet = NULL;
69
70 if (card->type == VCARD_DIRECT) {
71 /* select the last applet */
72 VCardApplet *current_applet = NULL;
73 for (current_applet = card->applet_list; current_applet;
74 current_applet = current_applet->next) {
75 applet = current_applet;
76 }
77 }
78 for (i = 0; i < MAX_CHANNEL; i++) {
79 card->current_applet[i] = applet;
80 }
81 if (card->vcard_buffer_response) {
82 vcard_buffer_response_delete(card->vcard_buffer_response);
83 card->vcard_buffer_response = NULL;
84 }
85 vcard_emul_reset(card, power);
86 if (applet) {
87 applet->reset_applet(card, 0);
88 }
89}
90
91/* applet utilities */
92
93/*
94 * applet utilities
95 */
96/* constructor */
97VCardApplet *
98vcard_new_applet(VCardProcessAPDU applet_process_function,
99 VCardResetApplet applet_reset_function,
100 unsigned char *aid, int aid_len)
101{
102 VCardApplet *applet;
103
Michael Tokarev78a4b8d2014-05-08 19:51:01 +0400104 applet = g_new0(VCardApplet, 1);
Robert Relyea111a38b2010-11-28 16:36:38 +0200105 applet->process_apdu = applet_process_function;
106 applet->reset_applet = applet_reset_function;
107
Michael Tokarev78a4b8d2014-05-08 19:51:01 +0400108 applet->aid = g_memdup(aid, aid_len);
Robert Relyea111a38b2010-11-28 16:36:38 +0200109 applet->aid_len = aid_len;
110 return applet;
111}
112
113/* destructor */
114void
115vcard_delete_applet(VCardApplet *applet)
116{
117 if (applet == NULL) {
118 return;
119 }
120 if (applet->applet_private_free) {
121 applet->applet_private_free(applet->applet_private);
Robert Relyea111a38b2010-11-28 16:36:38 +0200122 }
Markus Armbrusterec159932014-06-06 18:32:08 +0200123 g_free(applet->aid);
Anthony Liguori7267c092011-08-20 22:09:37 -0500124 g_free(applet);
Robert Relyea111a38b2010-11-28 16:36:38 +0200125}
126
127/* accessor */
128void
129vcard_set_applet_private(VCardApplet *applet, VCardAppletPrivate *private,
130 VCardAppletPrivateFree private_free)
131{
132 if (applet->applet_private_free) {
133 applet->applet_private_free(applet->applet_private);
134 }
135 applet->applet_private = private;
136 applet->applet_private_free = private_free;
137}
138
139VCard *
140vcard_new(VCardEmul *private, VCardEmulFree private_free)
141{
142 VCard *new_card;
Robert Relyea111a38b2010-11-28 16:36:38 +0200143
Michael Tokarev78a4b8d2014-05-08 19:51:01 +0400144 new_card = g_new0(VCard, 1);
Robert Relyea111a38b2010-11-28 16:36:38 +0200145 new_card->type = VCARD_VM;
146 new_card->vcard_private = private;
147 new_card->vcard_private_free = private_free;
Robert Relyea111a38b2010-11-28 16:36:38 +0200148 new_card->reference_count = 1;
149 return new_card;
150}
151
152VCard *
153vcard_reference(VCard *vcard)
154{
155 if (vcard == NULL) {
156 return NULL;
157 }
158 vcard->reference_count++;
159 return vcard;
160}
161
162void
163vcard_free(VCard *vcard)
164{
Michael Tokarev1687a082014-05-08 21:17:38 +0400165 VCardApplet *current_applet;
166 VCardApplet *next_applet;
Robert Relyea111a38b2010-11-28 16:36:38 +0200167
168 if (vcard == NULL) {
169 return;
170 }
171 vcard->reference_count--;
172 if (vcard->reference_count != 0) {
173 return;
174 }
175 if (vcard->vcard_private_free) {
176 (*vcard->vcard_private_free)(vcard->vcard_private);
Robert Relyea111a38b2010-11-28 16:36:38 +0200177 }
178 for (current_applet = vcard->applet_list; current_applet;
179 current_applet = next_applet) {
180 next_applet = current_applet->next;
181 vcard_delete_applet(current_applet);
182 }
183 vcard_buffer_response_delete(vcard->vcard_buffer_response);
Anthony Liguori7267c092011-08-20 22:09:37 -0500184 g_free(vcard);
Robert Relyea111a38b2010-11-28 16:36:38 +0200185}
186
187void
188vcard_get_atr(VCard *vcard, unsigned char *atr, int *atr_len)
189{
190 if (vcard->vcard_get_atr) {
191 (*vcard->vcard_get_atr)(vcard, atr, atr_len);
192 return;
193 }
194 vcard_emul_get_atr(vcard, atr, atr_len);
195}
196
197void
198vcard_set_atr_func(VCard *card, VCardGetAtr vcard_get_atr)
199{
200 card->vcard_get_atr = vcard_get_atr;
201}
202
203
204VCardStatus
205vcard_add_applet(VCard *card, VCardApplet *applet)
206{
207 applet->next = card->applet_list;
208 card->applet_list = applet;
209 /* if our card-type is direct, always call the applet */
210 if (card->type == VCARD_DIRECT) {
211 int i;
212
213 for (i = 0; i < MAX_CHANNEL; i++) {
214 card->current_applet[i] = applet;
215 }
216 }
217 return VCARD_DONE;
218}
219
220/*
221 * manage applets
222 */
223VCardApplet *
224vcard_find_applet(VCard *card, unsigned char *aid, int aid_len)
225{
226 VCardApplet *current_applet;
227
228 for (current_applet = card->applet_list; current_applet;
229 current_applet = current_applet->next) {
230 if (current_applet->aid_len != aid_len) {
231 continue;
232 }
233 if (memcmp(current_applet->aid, aid, aid_len) == 0) {
234 break;
235 }
236 }
237 return current_applet;
238}
239
240unsigned char *
241vcard_applet_get_aid(VCardApplet *applet, int *aid_len)
242{
243 if (applet == NULL) {
244 return NULL;
245 }
246 *aid_len = applet->aid_len;
247 return applet->aid;
248}
249
250
251void
252vcard_select_applet(VCard *card, int channel, VCardApplet *applet)
253{
254 assert(channel < MAX_CHANNEL);
Ray Strode1223bc42014-10-18 22:12:48 -0400255
256 /* If using an emulated card, make sure to log out of any already logged in
257 * session. */
258 vcard_emul_logout(card);
259
Robert Relyea111a38b2010-11-28 16:36:38 +0200260 card->current_applet[channel] = applet;
261 /* reset the applet */
262 if (applet && applet->reset_applet) {
263 applet->reset_applet(card, channel);
264 }
265}
266
267VCardAppletPrivate *
268vcard_get_current_applet_private(VCard *card, int channel)
269{
270 VCardApplet *applet = card->current_applet[channel];
271
272 if (applet == NULL) {
273 return NULL;
274 }
275 return applet->applet_private;
276}
277
278VCardStatus
279vcard_process_applet_apdu(VCard *card, VCardAPDU *apdu,
280 VCardResponse **response)
281{
282 if (card->current_applet[apdu->a_channel]) {
283 return card->current_applet[apdu->a_channel]->process_apdu(
284 card, apdu, response);
285 }
286 return VCARD_NEXT;
287}
288
289/*
290 * Accessor functions
291 */
292/* accessor functions for the response buffer */
293VCardBufferResponse *
294vcard_get_buffer_response(VCard *card)
295{
296 return card->vcard_buffer_response;
297}
298
299void
300vcard_set_buffer_response(VCard *card, VCardBufferResponse *buffer)
301{
302 card->vcard_buffer_response = buffer;
303}
304
305
306/* accessor functions for the type */
307VCardType
308vcard_get_type(VCard *card)
309{
310 return card->type;
311}
312
313void
314vcard_set_type(VCard *card, VCardType type)
315{
316 card->type = type;
317}
318
319/* accessor for private data */
320VCardEmul *
321vcard_get_private(VCard *vcard)
322{
323 return vcard->vcard_private;
324}
325