Add qemu 2.4.0
[kvmfornfv.git] / qemu / hw / usb / ccid-card-passthru.c
1 /*
2  * CCID Passthru Card Device emulation
3  *
4  * Copyright (c) 2011 Red Hat.
5  * Written by Alon Levy.
6  *
7  * This work is licensed under the terms of the GNU GPL, version 2.1 or later.
8  * See the COPYING file in the top-level directory.
9  */
10
11 #include "sysemu/char.h"
12 #include "qemu/error-report.h"
13 #include "qemu/sockets.h"
14 #include "ccid.h"
15 #include "libcacard/vscard_common.h"
16
17 #define DPRINTF(card, lvl, fmt, ...)                    \
18 do {                                                    \
19     if (lvl <= card->debug) {                           \
20         printf("ccid-card-passthru: " fmt , ## __VA_ARGS__);     \
21     }                                                   \
22 } while (0)
23
24 #define D_WARN 1
25 #define D_INFO 2
26 #define D_MORE_INFO 3
27 #define D_VERBOSE 4
28
29 /* TODO: do we still need this? */
30 static const uint8_t DEFAULT_ATR[] = {
31 /*
32  * From some example somewhere
33  * 0x3B, 0xB0, 0x18, 0x00, 0xD1, 0x81, 0x05, 0xB1, 0x40, 0x38, 0x1F, 0x03, 0x28
34  */
35
36 /* From an Athena smart card */
37  0x3B, 0xD5, 0x18, 0xFF, 0x80, 0x91, 0xFE, 0x1F, 0xC3, 0x80, 0x73, 0xC8, 0x21,
38  0x13, 0x08
39 };
40
41
42 #define PASSTHRU_DEV_NAME "ccid-card-passthru"
43 #define VSCARD_IN_SIZE 65536
44
45 /* maximum size of ATR - from 7816-3 */
46 #define MAX_ATR_SIZE        40
47
48 typedef struct PassthruState PassthruState;
49
50 struct PassthruState {
51     CCIDCardState base;
52     CharDriverState *cs;
53     uint8_t  vscard_in_data[VSCARD_IN_SIZE];
54     uint32_t vscard_in_pos;
55     uint32_t vscard_in_hdr;
56     uint8_t  atr[MAX_ATR_SIZE];
57     uint8_t  atr_length;
58     uint8_t  debug;
59 };
60
61 /*
62  * VSCard protocol over chardev
63  * This code should not depend on the card type.
64  */
65
66 static void ccid_card_vscard_send_msg(PassthruState *s,
67         VSCMsgType type, uint32_t reader_id,
68         const uint8_t *payload, uint32_t length)
69 {
70     VSCMsgHeader scr_msg_header;
71
72     scr_msg_header.type = htonl(type);
73     scr_msg_header.reader_id = htonl(reader_id);
74     scr_msg_header.length = htonl(length);
75     qemu_chr_fe_write(s->cs, (uint8_t *)&scr_msg_header, sizeof(VSCMsgHeader));
76     qemu_chr_fe_write(s->cs, payload, length);
77 }
78
79 static void ccid_card_vscard_send_apdu(PassthruState *s,
80     const uint8_t *apdu, uint32_t length)
81 {
82     ccid_card_vscard_send_msg(
83         s, VSC_APDU, VSCARD_MINIMAL_READER_ID, apdu, length);
84 }
85
86 static void ccid_card_vscard_send_error(PassthruState *s,
87                     uint32_t reader_id, VSCErrorCode code)
88 {
89     VSCMsgError msg = {.code = htonl(code)};
90
91     ccid_card_vscard_send_msg(
92         s, VSC_Error, reader_id, (uint8_t *)&msg, sizeof(msg));
93 }
94
95 static void ccid_card_vscard_send_init(PassthruState *s)
96 {
97     VSCMsgInit msg = {
98         .version = htonl(VSCARD_VERSION),
99         .magic = VSCARD_MAGIC,
100         .capabilities = {0}
101     };
102
103     ccid_card_vscard_send_msg(s, VSC_Init, VSCARD_UNDEFINED_READER_ID,
104                          (uint8_t *)&msg, sizeof(msg));
105 }
106
107 static int ccid_card_vscard_can_read(void *opaque)
108 {
109     PassthruState *card = opaque;
110
111     return VSCARD_IN_SIZE >= card->vscard_in_pos ?
112            VSCARD_IN_SIZE - card->vscard_in_pos : 0;
113 }
114
115 static void ccid_card_vscard_handle_init(
116     PassthruState *card, VSCMsgHeader *hdr, VSCMsgInit *init)
117 {
118     uint32_t *capabilities;
119     int num_capabilities;
120     int i;
121
122     capabilities = init->capabilities;
123     num_capabilities =
124         1 + ((hdr->length - sizeof(VSCMsgInit)) / sizeof(uint32_t));
125     init->version = ntohl(init->version);
126     for (i = 0 ; i < num_capabilities; ++i) {
127         capabilities[i] = ntohl(capabilities[i]);
128     }
129     if (init->magic != VSCARD_MAGIC) {
130         error_report("wrong magic");
131         /* we can't disconnect the chardev */
132     }
133     if (init->version != VSCARD_VERSION) {
134         DPRINTF(card, D_WARN,
135             "got version %d, have %d", init->version, VSCARD_VERSION);
136     }
137     /* future handling of capabilities, none exist atm */
138     ccid_card_vscard_send_init(card);
139 }
140
141 static int check_atr(PassthruState *card, uint8_t *data, int len)
142 {
143     int historical_length, opt_bytes;
144     int td_count = 0;
145     int td;
146
147     if (len < 2) {
148         return 0;
149     }
150     historical_length = data[1] & 0xf;
151     opt_bytes = 0;
152     if (data[0] != 0x3b && data[0] != 0x3f) {
153         DPRINTF(card, D_WARN, "atr's T0 is 0x%X, not in {0x3b, 0x3f}\n",
154                 data[0]);
155         return 0;
156     }
157     td_count = 0;
158     td = data[1] >> 4;
159     while (td && td_count < 2 && opt_bytes + historical_length + 2 < len) {
160         td_count++;
161         if (td & 0x1) {
162             opt_bytes++;
163         }
164         if (td & 0x2) {
165             opt_bytes++;
166         }
167         if (td & 0x4) {
168             opt_bytes++;
169         }
170         if (td & 0x8) {
171             opt_bytes++;
172             td = data[opt_bytes + 2] >> 4;
173         }
174     }
175     if (len < 2 + historical_length + opt_bytes) {
176         DPRINTF(card, D_WARN,
177             "atr too short: len %d, but historical_len %d, T1 0x%X\n",
178             len, historical_length, data[1]);
179         return 0;
180     }
181     if (len > 2 + historical_length + opt_bytes) {
182         DPRINTF(card, D_WARN,
183             "atr too long: len %d, but hist/opt %d/%d, T1 0x%X\n",
184             len, historical_length, opt_bytes, data[1]);
185         /* let it through */
186     }
187     DPRINTF(card, D_VERBOSE,
188             "atr passes check: %d total length, %d historical, %d optional\n",
189             len, historical_length, opt_bytes);
190
191     return 1;
192 }
193
194 static void ccid_card_vscard_handle_message(PassthruState *card,
195     VSCMsgHeader *scr_msg_header)
196 {
197     uint8_t *data = (uint8_t *)&scr_msg_header[1];
198
199     switch (scr_msg_header->type) {
200     case VSC_ATR:
201         DPRINTF(card, D_INFO, "VSC_ATR %d\n", scr_msg_header->length);
202         if (scr_msg_header->length > MAX_ATR_SIZE) {
203             error_report("ATR size exceeds spec, ignoring");
204             ccid_card_vscard_send_error(card, scr_msg_header->reader_id,
205                                         VSC_GENERAL_ERROR);
206             break;
207         }
208         if (!check_atr(card, data, scr_msg_header->length)) {
209             error_report("ATR is inconsistent, ignoring");
210             ccid_card_vscard_send_error(card, scr_msg_header->reader_id,
211                                         VSC_GENERAL_ERROR);
212             break;
213         }
214         memcpy(card->atr, data, scr_msg_header->length);
215         card->atr_length = scr_msg_header->length;
216         ccid_card_card_inserted(&card->base);
217         ccid_card_vscard_send_error(card, scr_msg_header->reader_id,
218                                     VSC_SUCCESS);
219         break;
220     case VSC_APDU:
221         ccid_card_send_apdu_to_guest(
222             &card->base, data, scr_msg_header->length);
223         break;
224     case VSC_CardRemove:
225         DPRINTF(card, D_INFO, "VSC_CardRemove\n");
226         ccid_card_card_removed(&card->base);
227         ccid_card_vscard_send_error(card,
228             scr_msg_header->reader_id, VSC_SUCCESS);
229         break;
230     case VSC_Init:
231         ccid_card_vscard_handle_init(
232             card, scr_msg_header, (VSCMsgInit *)data);
233         break;
234     case VSC_Error:
235         ccid_card_card_error(&card->base, *(uint32_t *)data);
236         break;
237     case VSC_ReaderAdd:
238         if (ccid_card_ccid_attach(&card->base) < 0) {
239             ccid_card_vscard_send_error(card, VSCARD_UNDEFINED_READER_ID,
240                                       VSC_CANNOT_ADD_MORE_READERS);
241         } else {
242             ccid_card_vscard_send_error(card, VSCARD_MINIMAL_READER_ID,
243                                         VSC_SUCCESS);
244         }
245         break;
246     case VSC_ReaderRemove:
247         ccid_card_ccid_detach(&card->base);
248         ccid_card_vscard_send_error(card,
249             scr_msg_header->reader_id, VSC_SUCCESS);
250         break;
251     default:
252         printf("usb-ccid: chardev: unexpected message of type %X\n",
253                scr_msg_header->type);
254         ccid_card_vscard_send_error(card, scr_msg_header->reader_id,
255             VSC_GENERAL_ERROR);
256     }
257 }
258
259 static void ccid_card_vscard_drop_connection(PassthruState *card)
260 {
261     qemu_chr_delete(card->cs);
262     card->vscard_in_pos = card->vscard_in_hdr = 0;
263 }
264
265 static void ccid_card_vscard_read(void *opaque, const uint8_t *buf, int size)
266 {
267     PassthruState *card = opaque;
268     VSCMsgHeader *hdr;
269
270     if (card->vscard_in_pos + size > VSCARD_IN_SIZE) {
271         error_report(
272             "no room for data: pos %d +  size %d > %d. dropping connection.",
273             card->vscard_in_pos, size, VSCARD_IN_SIZE);
274         ccid_card_vscard_drop_connection(card);
275         return;
276     }
277     assert(card->vscard_in_pos < VSCARD_IN_SIZE);
278     assert(card->vscard_in_hdr < VSCARD_IN_SIZE);
279     memcpy(card->vscard_in_data + card->vscard_in_pos, buf, size);
280     card->vscard_in_pos += size;
281     hdr = (VSCMsgHeader *)(card->vscard_in_data + card->vscard_in_hdr);
282
283     while ((card->vscard_in_pos - card->vscard_in_hdr >= sizeof(VSCMsgHeader))
284          &&(card->vscard_in_pos - card->vscard_in_hdr >=
285                                   sizeof(VSCMsgHeader) + ntohl(hdr->length))) {
286         hdr->reader_id = ntohl(hdr->reader_id);
287         hdr->length = ntohl(hdr->length);
288         hdr->type = ntohl(hdr->type);
289         ccid_card_vscard_handle_message(card, hdr);
290         card->vscard_in_hdr += hdr->length + sizeof(VSCMsgHeader);
291         hdr = (VSCMsgHeader *)(card->vscard_in_data + card->vscard_in_hdr);
292     }
293     if (card->vscard_in_hdr == card->vscard_in_pos) {
294         card->vscard_in_pos = card->vscard_in_hdr = 0;
295     }
296 }
297
298 static void ccid_card_vscard_event(void *opaque, int event)
299 {
300     PassthruState *card = opaque;
301
302     switch (event) {
303     case CHR_EVENT_BREAK:
304         card->vscard_in_pos = card->vscard_in_hdr = 0;
305         break;
306     case CHR_EVENT_FOCUS:
307         break;
308     case CHR_EVENT_OPENED:
309         DPRINTF(card, D_INFO, "%s: CHR_EVENT_OPENED\n", __func__);
310         break;
311     }
312 }
313
314 /* End VSCard handling */
315
316 static void passthru_apdu_from_guest(
317     CCIDCardState *base, const uint8_t *apdu, uint32_t len)
318 {
319     PassthruState *card = DO_UPCAST(PassthruState, base, base);
320
321     if (!card->cs) {
322         printf("ccid-passthru: no chardev, discarding apdu length %d\n", len);
323         return;
324     }
325     ccid_card_vscard_send_apdu(card, apdu, len);
326 }
327
328 static const uint8_t *passthru_get_atr(CCIDCardState *base, uint32_t *len)
329 {
330     PassthruState *card = DO_UPCAST(PassthruState, base, base);
331
332     *len = card->atr_length;
333     return card->atr;
334 }
335
336 static int passthru_initfn(CCIDCardState *base)
337 {
338     PassthruState *card = DO_UPCAST(PassthruState, base, base);
339
340     card->vscard_in_pos = 0;
341     card->vscard_in_hdr = 0;
342     if (card->cs) {
343         DPRINTF(card, D_INFO, "initing chardev\n");
344         qemu_chr_add_handlers(card->cs,
345             ccid_card_vscard_can_read,
346             ccid_card_vscard_read,
347             ccid_card_vscard_event, card);
348         ccid_card_vscard_send_init(card);
349     } else {
350         error_report("missing chardev");
351         return -1;
352     }
353     card->debug = parse_debug_env("QEMU_CCID_PASSTHRU_DEBUG", D_VERBOSE,
354                                   card->debug);
355     assert(sizeof(DEFAULT_ATR) <= MAX_ATR_SIZE);
356     memcpy(card->atr, DEFAULT_ATR, sizeof(DEFAULT_ATR));
357     card->atr_length = sizeof(DEFAULT_ATR);
358     return 0;
359 }
360
361 static int passthru_exitfn(CCIDCardState *base)
362 {
363     return 0;
364 }
365
366 static VMStateDescription passthru_vmstate = {
367     .name = "ccid-card-passthru",
368     .version_id = 1,
369     .minimum_version_id = 1,
370     .fields = (VMStateField[]) {
371         VMSTATE_BUFFER(vscard_in_data, PassthruState),
372         VMSTATE_UINT32(vscard_in_pos, PassthruState),
373         VMSTATE_UINT32(vscard_in_hdr, PassthruState),
374         VMSTATE_BUFFER(atr, PassthruState),
375         VMSTATE_UINT8(atr_length, PassthruState),
376         VMSTATE_END_OF_LIST()
377     }
378 };
379
380 static Property passthru_card_properties[] = {
381     DEFINE_PROP_CHR("chardev", PassthruState, cs),
382     DEFINE_PROP_UINT8("debug", PassthruState, debug, 0),
383     DEFINE_PROP_END_OF_LIST(),
384 };
385
386 static void passthru_class_initfn(ObjectClass *klass, void *data)
387 {
388     DeviceClass *dc = DEVICE_CLASS(klass);
389     CCIDCardClass *cc = CCID_CARD_CLASS(klass);
390
391     cc->initfn = passthru_initfn;
392     cc->exitfn = passthru_exitfn;
393     cc->get_atr = passthru_get_atr;
394     cc->apdu_from_guest = passthru_apdu_from_guest;
395     set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
396     dc->desc = "passthrough smartcard";
397     dc->vmsd = &passthru_vmstate;
398     dc->props = passthru_card_properties;
399 }
400
401 static const TypeInfo passthru_card_info = {
402     .name          = PASSTHRU_DEV_NAME,
403     .parent        = TYPE_CCID_CARD,
404     .instance_size = sizeof(PassthruState),
405     .class_init    = passthru_class_initfn,
406 };
407
408 static void ccid_card_passthru_register_types(void)
409 {
410     type_register_static(&passthru_card_info);
411 }
412
413 type_init(ccid_card_passthru_register_types)