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