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