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.
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"
22 #include "qapi/error.h"
23 #include "qom/object.h"
25 #define DPRINTF(card, lvl, fmt, ...) \
27 if (lvl <= card->debug) { \
28 printf("ccid-card-passthru: " fmt , ## __VA_ARGS__); \
37 /* TODO: do we still need this? */
38 static const uint8_t DEFAULT_ATR
[] = {
40 * From some example somewhere
41 * 0x3B, 0xB0, 0x18, 0x00, 0xD1, 0x81, 0x05, 0xB1, 0x40, 0x38, 0x1F, 0x03, 0x28
44 /* From an Athena smart card */
45 0x3B, 0xD5, 0x18, 0xFF, 0x80, 0x91, 0xFE, 0x1F, 0xC3, 0x80, 0x73, 0xC8, 0x21,
49 #define VSCARD_IN_SIZE (64 * KiB)
51 /* maximum size of ATR - from 7816-3 */
52 #define MAX_ATR_SIZE 40
54 typedef struct PassthruState PassthruState
;
56 struct PassthruState
{
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
];
67 #define TYPE_CCID_PASSTHRU "ccid-card-passthru"
68 DECLARE_INSTANCE_CHECKER(PassthruState
, PASSTHRU_CCID_CARD
,
72 * VSCard protocol over chardev
73 * This code should not depend on the card type.
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
)
80 VSCMsgHeader scr_msg_header
;
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
);
92 static void ccid_card_vscard_send_apdu(PassthruState
*s
,
93 const uint8_t *apdu
, uint32_t length
)
95 ccid_card_vscard_send_msg(
96 s
, VSC_APDU
, VSCARD_MINIMAL_READER_ID
, apdu
, length
);
99 static void ccid_card_vscard_send_error(PassthruState
*s
,
100 uint32_t reader_id
, VSCErrorCode code
)
102 VSCMsgError msg
= {.code
= htonl(code
)};
104 ccid_card_vscard_send_msg(
105 s
, VSC_Error
, reader_id
, (uint8_t *)&msg
, sizeof(msg
));
108 static void ccid_card_vscard_send_init(PassthruState
*s
)
111 .version
= htonl(VSCARD_VERSION
),
112 .magic
= VSCARD_MAGIC
,
116 ccid_card_vscard_send_msg(s
, VSC_Init
, VSCARD_UNDEFINED_READER_ID
,
117 (uint8_t *)&msg
, sizeof(msg
));
120 static int ccid_card_vscard_can_read(void *opaque
)
122 PassthruState
*card
= opaque
;
124 return VSCARD_IN_SIZE
>= card
->vscard_in_pos
?
125 VSCARD_IN_SIZE
- card
->vscard_in_pos
: 0;
128 static void ccid_card_vscard_handle_init(
129 PassthruState
*card
, VSCMsgHeader
*hdr
, VSCMsgInit
*init
)
131 uint32_t *capabilities
;
132 int num_capabilities
;
135 capabilities
= init
->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
]);
142 if (init
->magic
!= VSCARD_MAGIC
) {
143 error_report("wrong magic");
144 /* we can't disconnect the chardev */
146 if (init
->version
!= VSCARD_VERSION
) {
147 DPRINTF(card
, D_WARN
,
148 "got version %d, have %d", init
->version
, VSCARD_VERSION
);
150 /* future handling of capabilities, none exist atm */
151 ccid_card_vscard_send_init(card
);
154 static int check_atr(PassthruState
*card
, uint8_t *data
, int len
)
156 int historical_length
, opt_bytes
;
163 historical_length
= data
[1] & 0xf;
165 if (data
[0] != 0x3b && data
[0] != 0x3f) {
166 DPRINTF(card
, D_WARN
, "atr's T0 is 0x%X, not in {0x3b, 0x3f}\n",
172 while (td
&& td_count
< 2 && opt_bytes
+ historical_length
+ 2 < len
) {
185 td
= data
[opt_bytes
+ 2] >> 4;
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]);
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]);
200 DPRINTF(card
, D_VERBOSE
,
201 "atr passes check: %d total length, %d historical, %d optional\n",
202 len
, historical_length
, opt_bytes
);
207 static void ccid_card_vscard_handle_message(PassthruState
*card
,
208 VSCMsgHeader
*scr_msg_header
)
210 uint8_t *data
= (uint8_t *)&scr_msg_header
[1];
212 switch (scr_msg_header
->type
) {
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
,
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
,
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
,
234 ccid_card_send_apdu_to_guest(
235 &card
->base
, data
, scr_msg_header
->length
);
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
);
244 ccid_card_vscard_handle_init(
245 card
, scr_msg_header
, (VSCMsgInit
*)data
);
248 ccid_card_card_error(&card
->base
, *(uint32_t *)data
);
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
);
255 ccid_card_vscard_send_error(card
, VSCARD_MINIMAL_READER_ID
,
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
);
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
,
272 static void ccid_card_vscard_drop_connection(PassthruState
*card
)
274 qemu_chr_fe_deinit(&card
->cs
, true);
275 card
->vscard_in_pos
= card
->vscard_in_hdr
= 0;
278 static void ccid_card_vscard_read(void *opaque
, const uint8_t *buf
, int size
)
280 PassthruState
*card
= opaque
;
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
);
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
);
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
);
306 if (card
->vscard_in_hdr
== card
->vscard_in_pos
) {
307 card
->vscard_in_pos
= card
->vscard_in_hdr
= 0;
311 static void ccid_card_vscard_event(void *opaque
, QEMUChrEvent event
)
313 PassthruState
*card
= opaque
;
316 case CHR_EVENT_BREAK
:
317 card
->vscard_in_pos
= card
->vscard_in_hdr
= 0;
319 case CHR_EVENT_OPENED
:
320 DPRINTF(card
, D_INFO
, "%s: CHR_EVENT_OPENED\n", __func__
);
322 case CHR_EVENT_MUX_IN
:
323 case CHR_EVENT_MUX_OUT
:
324 case CHR_EVENT_CLOSED
:
330 /* End VSCard handling */
332 static void passthru_apdu_from_guest(
333 CCIDCardState
*base
, const uint8_t *apdu
, uint32_t len
)
335 PassthruState
*card
= PASSTHRU_CCID_CARD(base
);
337 if (!qemu_chr_fe_backend_connected(&card
->cs
)) {
338 printf("ccid-passthru: no chardev, discarding apdu length %d\n", len
);
341 ccid_card_vscard_send_apdu(card
, apdu
, len
);
344 static const uint8_t *passthru_get_atr(CCIDCardState
*base
, uint32_t *len
)
346 PassthruState
*card
= PASSTHRU_CCID_CARD(base
);
348 *len
= card
->atr_length
;
352 static void passthru_realize(CCIDCardState
*base
, Error
**errp
)
354 PassthruState
*card
= PASSTHRU_CCID_CARD(base
);
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
);
366 error_setg(errp
, "missing chardev");
369 card
->debug
= parse_debug_env("QEMU_CCID_PASSTHRU_DEBUG", D_VERBOSE
,
371 assert(sizeof(DEFAULT_ATR
) <= MAX_ATR_SIZE
);
372 memcpy(card
->atr
, DEFAULT_ATR
, sizeof(DEFAULT_ATR
));
373 card
->atr_length
= sizeof(DEFAULT_ATR
);
376 static VMStateDescription passthru_vmstate
= {
377 .name
= "ccid-card-passthru",
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()
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(),
396 static void passthru_class_initfn(ObjectClass
*klass
, void *data
)
398 DeviceClass
*dc
= DEVICE_CLASS(klass
);
399 CCIDCardClass
*cc
= CCID_CARD_CLASS(klass
);
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
);
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
,
417 static void ccid_card_passthru_register_types(void)
419 type_register_static(&passthru_card_info
);
422 type_init(ccid_card_passthru_register_types
)