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