trace-events: fix first line comment in trace-events
[qemu.git] / hw / usb / ccid-card-passthru.c
blobc0e90e501c996bd510ee22a3dbcc7da0a9712757
1 /*
2 * CCID Passthru Card Device emulation
4 * Copyright (c) 2011 Red Hat.
5 * Written by Alon Levy.
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 */
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"
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)
25 #define D_WARN 1
26 #define D_INFO 2
27 #define D_MORE_INFO 3
28 #define D_VERBOSE 4
30 /* TODO: do we still need this? */
31 static const uint8_t DEFAULT_ATR[] = {
33 * From some example somewhere
34 * 0x3B, 0xB0, 0x18, 0x00, 0xD1, 0x81, 0x05, 0xB1, 0x40, 0x38, 0x1F, 0x03, 0x28
37 /* From an Athena smart card */
38 0x3B, 0xD5, 0x18, 0xFF, 0x80, 0x91, 0xFE, 0x1F, 0xC3, 0x80, 0x73, 0xC8, 0x21,
39 0x13, 0x08
42 #define VSCARD_IN_SIZE 65536
44 /* maximum size of ATR - from 7816-3 */
45 #define MAX_ATR_SIZE 40
47 typedef struct PassthruState PassthruState;
49 struct PassthruState {
50 CCIDCardState base;
51 CharDriverState *cs;
52 uint8_t vscard_in_data[VSCARD_IN_SIZE];
53 uint32_t vscard_in_pos;
54 uint32_t vscard_in_hdr;
55 uint8_t atr[MAX_ATR_SIZE];
56 uint8_t atr_length;
57 uint8_t debug;
60 #define TYPE_CCID_PASSTHRU "ccid-card-passthru"
61 #define PASSTHRU_CCID_CARD(obj) \
62 OBJECT_CHECK(PassthruState, (obj), TYPE_CCID_PASSTHRU)
65 * VSCard protocol over chardev
66 * This code should not depend on the card type.
69 static void ccid_card_vscard_send_msg(PassthruState *s,
70 VSCMsgType type, uint32_t reader_id,
71 const uint8_t *payload, uint32_t length)
73 VSCMsgHeader scr_msg_header;
75 scr_msg_header.type = htonl(type);
76 scr_msg_header.reader_id = htonl(reader_id);
77 scr_msg_header.length = htonl(length);
78 qemu_chr_fe_write(s->cs, (uint8_t *)&scr_msg_header, sizeof(VSCMsgHeader));
79 qemu_chr_fe_write(s->cs, payload, length);
82 static void ccid_card_vscard_send_apdu(PassthruState *s,
83 const uint8_t *apdu, uint32_t length)
85 ccid_card_vscard_send_msg(
86 s, VSC_APDU, VSCARD_MINIMAL_READER_ID, apdu, length);
89 static void ccid_card_vscard_send_error(PassthruState *s,
90 uint32_t reader_id, VSCErrorCode code)
92 VSCMsgError msg = {.code = htonl(code)};
94 ccid_card_vscard_send_msg(
95 s, VSC_Error, reader_id, (uint8_t *)&msg, sizeof(msg));
98 static void ccid_card_vscard_send_init(PassthruState *s)
100 VSCMsgInit msg = {
101 .version = htonl(VSCARD_VERSION),
102 .magic = VSCARD_MAGIC,
103 .capabilities = {0}
106 ccid_card_vscard_send_msg(s, VSC_Init, VSCARD_UNDEFINED_READER_ID,
107 (uint8_t *)&msg, sizeof(msg));
110 static int ccid_card_vscard_can_read(void *opaque)
112 PassthruState *card = opaque;
114 return VSCARD_IN_SIZE >= card->vscard_in_pos ?
115 VSCARD_IN_SIZE - card->vscard_in_pos : 0;
118 static void ccid_card_vscard_handle_init(
119 PassthruState *card, VSCMsgHeader *hdr, VSCMsgInit *init)
121 uint32_t *capabilities;
122 int num_capabilities;
123 int i;
125 capabilities = init->capabilities;
126 num_capabilities =
127 1 + ((hdr->length - sizeof(VSCMsgInit)) / sizeof(uint32_t));
128 init->version = ntohl(init->version);
129 for (i = 0 ; i < num_capabilities; ++i) {
130 capabilities[i] = ntohl(capabilities[i]);
132 if (init->magic != VSCARD_MAGIC) {
133 error_report("wrong magic");
134 /* we can't disconnect the chardev */
136 if (init->version != VSCARD_VERSION) {
137 DPRINTF(card, D_WARN,
138 "got version %d, have %d", init->version, VSCARD_VERSION);
140 /* future handling of capabilities, none exist atm */
141 ccid_card_vscard_send_init(card);
144 static int check_atr(PassthruState *card, uint8_t *data, int len)
146 int historical_length, opt_bytes;
147 int td_count = 0;
148 int td;
150 if (len < 2) {
151 return 0;
153 historical_length = data[1] & 0xf;
154 opt_bytes = 0;
155 if (data[0] != 0x3b && data[0] != 0x3f) {
156 DPRINTF(card, D_WARN, "atr's T0 is 0x%X, not in {0x3b, 0x3f}\n",
157 data[0]);
158 return 0;
160 td_count = 0;
161 td = data[1] >> 4;
162 while (td && td_count < 2 && opt_bytes + historical_length + 2 < len) {
163 td_count++;
164 if (td & 0x1) {
165 opt_bytes++;
167 if (td & 0x2) {
168 opt_bytes++;
170 if (td & 0x4) {
171 opt_bytes++;
173 if (td & 0x8) {
174 opt_bytes++;
175 td = data[opt_bytes + 2] >> 4;
178 if (len < 2 + historical_length + opt_bytes) {
179 DPRINTF(card, D_WARN,
180 "atr too short: len %d, but historical_len %d, T1 0x%X\n",
181 len, historical_length, data[1]);
182 return 0;
184 if (len > 2 + historical_length + opt_bytes) {
185 DPRINTF(card, D_WARN,
186 "atr too long: len %d, but hist/opt %d/%d, T1 0x%X\n",
187 len, historical_length, opt_bytes, data[1]);
188 /* let it through */
190 DPRINTF(card, D_VERBOSE,
191 "atr passes check: %d total length, %d historical, %d optional\n",
192 len, historical_length, opt_bytes);
194 return 1;
197 static void ccid_card_vscard_handle_message(PassthruState *card,
198 VSCMsgHeader *scr_msg_header)
200 uint8_t *data = (uint8_t *)&scr_msg_header[1];
202 switch (scr_msg_header->type) {
203 case VSC_ATR:
204 DPRINTF(card, D_INFO, "VSC_ATR %d\n", scr_msg_header->length);
205 if (scr_msg_header->length > MAX_ATR_SIZE) {
206 error_report("ATR size exceeds spec, ignoring");
207 ccid_card_vscard_send_error(card, scr_msg_header->reader_id,
208 VSC_GENERAL_ERROR);
209 break;
211 if (!check_atr(card, data, scr_msg_header->length)) {
212 error_report("ATR is inconsistent, ignoring");
213 ccid_card_vscard_send_error(card, scr_msg_header->reader_id,
214 VSC_GENERAL_ERROR);
215 break;
217 memcpy(card->atr, data, scr_msg_header->length);
218 card->atr_length = scr_msg_header->length;
219 ccid_card_card_inserted(&card->base);
220 ccid_card_vscard_send_error(card, scr_msg_header->reader_id,
221 VSC_SUCCESS);
222 break;
223 case VSC_APDU:
224 ccid_card_send_apdu_to_guest(
225 &card->base, data, scr_msg_header->length);
226 break;
227 case VSC_CardRemove:
228 DPRINTF(card, D_INFO, "VSC_CardRemove\n");
229 ccid_card_card_removed(&card->base);
230 ccid_card_vscard_send_error(card,
231 scr_msg_header->reader_id, VSC_SUCCESS);
232 break;
233 case VSC_Init:
234 ccid_card_vscard_handle_init(
235 card, scr_msg_header, (VSCMsgInit *)data);
236 break;
237 case VSC_Error:
238 ccid_card_card_error(&card->base, *(uint32_t *)data);
239 break;
240 case VSC_ReaderAdd:
241 if (ccid_card_ccid_attach(&card->base) < 0) {
242 ccid_card_vscard_send_error(card, VSCARD_UNDEFINED_READER_ID,
243 VSC_CANNOT_ADD_MORE_READERS);
244 } else {
245 ccid_card_vscard_send_error(card, VSCARD_MINIMAL_READER_ID,
246 VSC_SUCCESS);
248 break;
249 case VSC_ReaderRemove:
250 ccid_card_ccid_detach(&card->base);
251 ccid_card_vscard_send_error(card,
252 scr_msg_header->reader_id, VSC_SUCCESS);
253 break;
254 default:
255 printf("usb-ccid: chardev: unexpected message of type %X\n",
256 scr_msg_header->type);
257 ccid_card_vscard_send_error(card, scr_msg_header->reader_id,
258 VSC_GENERAL_ERROR);
262 static void ccid_card_vscard_drop_connection(PassthruState *card)
264 qemu_chr_delete(card->cs);
265 card->vscard_in_pos = card->vscard_in_hdr = 0;
268 static void ccid_card_vscard_read(void *opaque, const uint8_t *buf, int size)
270 PassthruState *card = opaque;
271 VSCMsgHeader *hdr;
273 if (card->vscard_in_pos + size > VSCARD_IN_SIZE) {
274 error_report(
275 "no room for data: pos %d + size %d > %d. dropping connection.",
276 card->vscard_in_pos, size, VSCARD_IN_SIZE);
277 ccid_card_vscard_drop_connection(card);
278 return;
280 assert(card->vscard_in_pos < VSCARD_IN_SIZE);
281 assert(card->vscard_in_hdr < VSCARD_IN_SIZE);
282 memcpy(card->vscard_in_data + card->vscard_in_pos, buf, size);
283 card->vscard_in_pos += size;
284 hdr = (VSCMsgHeader *)(card->vscard_in_data + card->vscard_in_hdr);
286 while ((card->vscard_in_pos - card->vscard_in_hdr >= sizeof(VSCMsgHeader))
287 &&(card->vscard_in_pos - card->vscard_in_hdr >=
288 sizeof(VSCMsgHeader) + ntohl(hdr->length))) {
289 hdr->reader_id = ntohl(hdr->reader_id);
290 hdr->length = ntohl(hdr->length);
291 hdr->type = ntohl(hdr->type);
292 ccid_card_vscard_handle_message(card, hdr);
293 card->vscard_in_hdr += hdr->length + sizeof(VSCMsgHeader);
294 hdr = (VSCMsgHeader *)(card->vscard_in_data + card->vscard_in_hdr);
296 if (card->vscard_in_hdr == card->vscard_in_pos) {
297 card->vscard_in_pos = card->vscard_in_hdr = 0;
301 static void ccid_card_vscard_event(void *opaque, int event)
303 PassthruState *card = opaque;
305 switch (event) {
306 case CHR_EVENT_BREAK:
307 card->vscard_in_pos = card->vscard_in_hdr = 0;
308 break;
309 case CHR_EVENT_FOCUS:
310 break;
311 case CHR_EVENT_OPENED:
312 DPRINTF(card, D_INFO, "%s: CHR_EVENT_OPENED\n", __func__);
313 break;
317 /* End VSCard handling */
319 static void passthru_apdu_from_guest(
320 CCIDCardState *base, const uint8_t *apdu, uint32_t len)
322 PassthruState *card = PASSTHRU_CCID_CARD(base);
324 if (!card->cs) {
325 printf("ccid-passthru: no chardev, discarding apdu length %d\n", len);
326 return;
328 ccid_card_vscard_send_apdu(card, apdu, len);
331 static const uint8_t *passthru_get_atr(CCIDCardState *base, uint32_t *len)
333 PassthruState *card = PASSTHRU_CCID_CARD(base);
335 *len = card->atr_length;
336 return card->atr;
339 static int passthru_initfn(CCIDCardState *base)
341 PassthruState *card = PASSTHRU_CCID_CARD(base);
343 card->vscard_in_pos = 0;
344 card->vscard_in_hdr = 0;
345 if (card->cs) {
346 DPRINTF(card, D_INFO, "initing chardev\n");
347 qemu_chr_add_handlers(card->cs,
348 ccid_card_vscard_can_read,
349 ccid_card_vscard_read,
350 ccid_card_vscard_event, card);
351 ccid_card_vscard_send_init(card);
352 } else {
353 error_report("missing chardev");
354 return -1;
356 card->debug = parse_debug_env("QEMU_CCID_PASSTHRU_DEBUG", D_VERBOSE,
357 card->debug);
358 assert(sizeof(DEFAULT_ATR) <= MAX_ATR_SIZE);
359 memcpy(card->atr, DEFAULT_ATR, sizeof(DEFAULT_ATR));
360 card->atr_length = sizeof(DEFAULT_ATR);
361 return 0;
364 static int passthru_exitfn(CCIDCardState *base)
366 return 0;
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()
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(),
389 static void passthru_class_initfn(ObjectClass *klass, void *data)
391 DeviceClass *dc = DEVICE_CLASS(klass);
392 CCIDCardClass *cc = CCID_CARD_CLASS(klass);
394 cc->initfn = passthru_initfn;
395 cc->exitfn = passthru_exitfn;
396 cc->get_atr = passthru_get_atr;
397 cc->apdu_from_guest = passthru_apdu_from_guest;
398 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
399 dc->desc = "passthrough smartcard";
400 dc->vmsd = &passthru_vmstate;
401 dc->props = passthru_card_properties;
404 static const TypeInfo passthru_card_info = {
405 .name = TYPE_CCID_PASSTHRU,
406 .parent = TYPE_CCID_CARD,
407 .instance_size = sizeof(PassthruState),
408 .class_init = passthru_class_initfn,
411 static void ccid_card_passthru_register_types(void)
413 type_register_static(&passthru_card_info);
416 type_init(ccid_card_passthru_register_types)