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