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