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/units.h"
13 #include <libcacard.h>
14 #include "chardev/char-fe.h"
15 #include "qemu/error-report.h"
16 #include "qemu/sockets.h"
18 #include "qapi/error.h"
20 #define DPRINTF(card, lvl, fmt, ...) \
22 if (lvl <= card->debug) { \
23 printf("ccid-card-passthru: " fmt , ## __VA_ARGS__); \
32 /* TODO: do we still need this? */
33 static const uint8_t DEFAULT_ATR
[] = {
35 * From some example somewhere
36 * 0x3B, 0xB0, 0x18, 0x00, 0xD1, 0x81, 0x05, 0xB1, 0x40, 0x38, 0x1F, 0x03, 0x28
39 /* From an Athena smart card */
40 0x3B, 0xD5, 0x18, 0xFF, 0x80, 0x91, 0xFE, 0x1F, 0xC3, 0x80, 0x73, 0xC8, 0x21,
44 #define VSCARD_IN_SIZE (64 * KiB)
46 /* maximum size of ATR - from 7816-3 */
47 #define MAX_ATR_SIZE 40
49 typedef struct PassthruState PassthruState
;
51 struct PassthruState
{
54 uint8_t vscard_in_data
[VSCARD_IN_SIZE
];
55 uint32_t vscard_in_pos
;
56 uint32_t vscard_in_hdr
;
57 uint8_t atr
[MAX_ATR_SIZE
];
62 #define TYPE_CCID_PASSTHRU "ccid-card-passthru"
63 #define PASSTHRU_CCID_CARD(obj) \
64 OBJECT_CHECK(PassthruState, (obj), TYPE_CCID_PASSTHRU)
67 * VSCard protocol over chardev
68 * This code should not depend on the card type.
71 static void ccid_card_vscard_send_msg(PassthruState
*s
,
72 VSCMsgType type
, uint32_t reader_id
,
73 const uint8_t *payload
, uint32_t length
)
75 VSCMsgHeader scr_msg_header
;
77 scr_msg_header
.type
= htonl(type
);
78 scr_msg_header
.reader_id
= htonl(reader_id
);
79 scr_msg_header
.length
= htonl(length
);
80 /* XXX this blocks entire thread. Rewrite to use
81 * qemu_chr_fe_write and background I/O callbacks */
82 qemu_chr_fe_write_all(&s
->cs
, (uint8_t *)&scr_msg_header
,
83 sizeof(VSCMsgHeader
));
84 qemu_chr_fe_write_all(&s
->cs
, payload
, length
);
87 static void ccid_card_vscard_send_apdu(PassthruState
*s
,
88 const uint8_t *apdu
, uint32_t length
)
90 ccid_card_vscard_send_msg(
91 s
, VSC_APDU
, VSCARD_MINIMAL_READER_ID
, apdu
, length
);
94 static void ccid_card_vscard_send_error(PassthruState
*s
,
95 uint32_t reader_id
, VSCErrorCode code
)
97 VSCMsgError msg
= {.code
= htonl(code
)};
99 ccid_card_vscard_send_msg(
100 s
, VSC_Error
, reader_id
, (uint8_t *)&msg
, sizeof(msg
));
103 static void ccid_card_vscard_send_init(PassthruState
*s
)
106 .version
= htonl(VSCARD_VERSION
),
107 .magic
= VSCARD_MAGIC
,
111 ccid_card_vscard_send_msg(s
, VSC_Init
, VSCARD_UNDEFINED_READER_ID
,
112 (uint8_t *)&msg
, sizeof(msg
));
115 static int ccid_card_vscard_can_read(void *opaque
)
117 PassthruState
*card
= opaque
;
119 return VSCARD_IN_SIZE
>= card
->vscard_in_pos
?
120 VSCARD_IN_SIZE
- card
->vscard_in_pos
: 0;
123 static void ccid_card_vscard_handle_init(
124 PassthruState
*card
, VSCMsgHeader
*hdr
, VSCMsgInit
*init
)
126 uint32_t *capabilities
;
127 int num_capabilities
;
130 capabilities
= init
->capabilities
;
132 1 + ((hdr
->length
- sizeof(VSCMsgInit
)) / sizeof(uint32_t));
133 init
->version
= ntohl(init
->version
);
134 for (i
= 0 ; i
< num_capabilities
; ++i
) {
135 capabilities
[i
] = ntohl(capabilities
[i
]);
137 if (init
->magic
!= VSCARD_MAGIC
) {
138 error_report("wrong magic");
139 /* we can't disconnect the chardev */
141 if (init
->version
!= VSCARD_VERSION
) {
142 DPRINTF(card
, D_WARN
,
143 "got version %d, have %d", init
->version
, VSCARD_VERSION
);
145 /* future handling of capabilities, none exist atm */
146 ccid_card_vscard_send_init(card
);
149 static int check_atr(PassthruState
*card
, uint8_t *data
, int len
)
151 int historical_length
, opt_bytes
;
158 historical_length
= data
[1] & 0xf;
160 if (data
[0] != 0x3b && data
[0] != 0x3f) {
161 DPRINTF(card
, D_WARN
, "atr's T0 is 0x%X, not in {0x3b, 0x3f}\n",
167 while (td
&& td_count
< 2 && opt_bytes
+ historical_length
+ 2 < len
) {
180 td
= data
[opt_bytes
+ 2] >> 4;
183 if (len
< 2 + historical_length
+ opt_bytes
) {
184 DPRINTF(card
, D_WARN
,
185 "atr too short: len %d, but historical_len %d, T1 0x%X\n",
186 len
, historical_length
, data
[1]);
189 if (len
> 2 + historical_length
+ opt_bytes
) {
190 DPRINTF(card
, D_WARN
,
191 "atr too long: len %d, but hist/opt %d/%d, T1 0x%X\n",
192 len
, historical_length
, opt_bytes
, data
[1]);
195 DPRINTF(card
, D_VERBOSE
,
196 "atr passes check: %d total length, %d historical, %d optional\n",
197 len
, historical_length
, opt_bytes
);
202 static void ccid_card_vscard_handle_message(PassthruState
*card
,
203 VSCMsgHeader
*scr_msg_header
)
205 uint8_t *data
= (uint8_t *)&scr_msg_header
[1];
207 switch (scr_msg_header
->type
) {
209 DPRINTF(card
, D_INFO
, "VSC_ATR %d\n", scr_msg_header
->length
);
210 if (scr_msg_header
->length
> MAX_ATR_SIZE
) {
211 error_report("ATR size exceeds spec, ignoring");
212 ccid_card_vscard_send_error(card
, scr_msg_header
->reader_id
,
216 if (!check_atr(card
, data
, scr_msg_header
->length
)) {
217 error_report("ATR is inconsistent, ignoring");
218 ccid_card_vscard_send_error(card
, scr_msg_header
->reader_id
,
222 memcpy(card
->atr
, data
, scr_msg_header
->length
);
223 card
->atr_length
= scr_msg_header
->length
;
224 ccid_card_card_inserted(&card
->base
);
225 ccid_card_vscard_send_error(card
, scr_msg_header
->reader_id
,
229 ccid_card_send_apdu_to_guest(
230 &card
->base
, data
, scr_msg_header
->length
);
233 DPRINTF(card
, D_INFO
, "VSC_CardRemove\n");
234 ccid_card_card_removed(&card
->base
);
235 ccid_card_vscard_send_error(card
,
236 scr_msg_header
->reader_id
, VSC_SUCCESS
);
239 ccid_card_vscard_handle_init(
240 card
, scr_msg_header
, (VSCMsgInit
*)data
);
243 ccid_card_card_error(&card
->base
, *(uint32_t *)data
);
246 if (ccid_card_ccid_attach(&card
->base
) < 0) {
247 ccid_card_vscard_send_error(card
, VSCARD_UNDEFINED_READER_ID
,
248 VSC_CANNOT_ADD_MORE_READERS
);
250 ccid_card_vscard_send_error(card
, VSCARD_MINIMAL_READER_ID
,
254 case VSC_ReaderRemove
:
255 ccid_card_ccid_detach(&card
->base
);
256 ccid_card_vscard_send_error(card
,
257 scr_msg_header
->reader_id
, VSC_SUCCESS
);
260 printf("usb-ccid: chardev: unexpected message of type %X\n",
261 scr_msg_header
->type
);
262 ccid_card_vscard_send_error(card
, scr_msg_header
->reader_id
,
267 static void ccid_card_vscard_drop_connection(PassthruState
*card
)
269 qemu_chr_fe_deinit(&card
->cs
, true);
270 card
->vscard_in_pos
= card
->vscard_in_hdr
= 0;
273 static void ccid_card_vscard_read(void *opaque
, const uint8_t *buf
, int size
)
275 PassthruState
*card
= opaque
;
278 if (card
->vscard_in_pos
+ size
> VSCARD_IN_SIZE
) {
279 error_report("no room for data: pos %u + size %d > %" PRId64
"."
280 " dropping connection.",
281 card
->vscard_in_pos
, size
, VSCARD_IN_SIZE
);
282 ccid_card_vscard_drop_connection(card
);
285 assert(card
->vscard_in_pos
< VSCARD_IN_SIZE
);
286 assert(card
->vscard_in_hdr
< VSCARD_IN_SIZE
);
287 memcpy(card
->vscard_in_data
+ card
->vscard_in_pos
, buf
, size
);
288 card
->vscard_in_pos
+= size
;
289 hdr
= (VSCMsgHeader
*)(card
->vscard_in_data
+ card
->vscard_in_hdr
);
291 while ((card
->vscard_in_pos
- card
->vscard_in_hdr
>= sizeof(VSCMsgHeader
))
292 &&(card
->vscard_in_pos
- card
->vscard_in_hdr
>=
293 sizeof(VSCMsgHeader
) + ntohl(hdr
->length
))) {
294 hdr
->reader_id
= ntohl(hdr
->reader_id
);
295 hdr
->length
= ntohl(hdr
->length
);
296 hdr
->type
= ntohl(hdr
->type
);
297 ccid_card_vscard_handle_message(card
, hdr
);
298 card
->vscard_in_hdr
+= hdr
->length
+ sizeof(VSCMsgHeader
);
299 hdr
= (VSCMsgHeader
*)(card
->vscard_in_data
+ card
->vscard_in_hdr
);
301 if (card
->vscard_in_hdr
== card
->vscard_in_pos
) {
302 card
->vscard_in_pos
= card
->vscard_in_hdr
= 0;
306 static void ccid_card_vscard_event(void *opaque
, int event
)
308 PassthruState
*card
= opaque
;
311 case CHR_EVENT_BREAK
:
312 card
->vscard_in_pos
= card
->vscard_in_hdr
= 0;
314 case CHR_EVENT_OPENED
:
315 DPRINTF(card
, D_INFO
, "%s: CHR_EVENT_OPENED\n", __func__
);
320 /* End VSCard handling */
322 static void passthru_apdu_from_guest(
323 CCIDCardState
*base
, const uint8_t *apdu
, uint32_t len
)
325 PassthruState
*card
= PASSTHRU_CCID_CARD(base
);
327 if (!qemu_chr_fe_backend_connected(&card
->cs
)) {
328 printf("ccid-passthru: no chardev, discarding apdu length %d\n", len
);
331 ccid_card_vscard_send_apdu(card
, apdu
, len
);
334 static const uint8_t *passthru_get_atr(CCIDCardState
*base
, uint32_t *len
)
336 PassthruState
*card
= PASSTHRU_CCID_CARD(base
);
338 *len
= card
->atr_length
;
342 static void passthru_realize(CCIDCardState
*base
, Error
**errp
)
344 PassthruState
*card
= PASSTHRU_CCID_CARD(base
);
346 card
->vscard_in_pos
= 0;
347 card
->vscard_in_hdr
= 0;
348 if (qemu_chr_fe_backend_connected(&card
->cs
)) {
349 DPRINTF(card
, D_INFO
, "ccid-card-passthru: initing chardev");
350 qemu_chr_fe_set_handlers(&card
->cs
,
351 ccid_card_vscard_can_read
,
352 ccid_card_vscard_read
,
353 ccid_card_vscard_event
, NULL
, card
, NULL
, true);
354 ccid_card_vscard_send_init(card
);
356 error_setg(errp
, "missing chardev");
359 card
->debug
= parse_debug_env("QEMU_CCID_PASSTHRU_DEBUG", D_VERBOSE
,
361 assert(sizeof(DEFAULT_ATR
) <= MAX_ATR_SIZE
);
362 memcpy(card
->atr
, DEFAULT_ATR
, sizeof(DEFAULT_ATR
));
363 card
->atr_length
= sizeof(DEFAULT_ATR
);
366 static VMStateDescription passthru_vmstate
= {
367 .name
= "ccid-card-passthru",
369 .minimum_version_id
= 1,
370 .fields
= (VMStateField
[]) {
371 VMSTATE_BUFFER(vscard_in_data
, PassthruState
),
372 VMSTATE_UINT32(vscard_in_pos
, PassthruState
),
373 VMSTATE_UINT32(vscard_in_hdr
, PassthruState
),
374 VMSTATE_BUFFER(atr
, PassthruState
),
375 VMSTATE_UINT8(atr_length
, PassthruState
),
376 VMSTATE_END_OF_LIST()
380 static Property passthru_card_properties
[] = {
381 DEFINE_PROP_CHR("chardev", PassthruState
, cs
),
382 DEFINE_PROP_UINT8("debug", PassthruState
, debug
, 0),
383 DEFINE_PROP_END_OF_LIST(),
386 static void passthru_class_initfn(ObjectClass
*klass
, void *data
)
388 DeviceClass
*dc
= DEVICE_CLASS(klass
);
389 CCIDCardClass
*cc
= CCID_CARD_CLASS(klass
);
391 cc
->realize
= passthru_realize
;
392 cc
->get_atr
= passthru_get_atr
;
393 cc
->apdu_from_guest
= passthru_apdu_from_guest
;
394 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
395 dc
->desc
= "passthrough smartcard";
396 dc
->vmsd
= &passthru_vmstate
;
397 dc
->props
= passthru_card_properties
;
400 static const TypeInfo passthru_card_info
= {
401 .name
= TYPE_CCID_PASSTHRU
,
402 .parent
= TYPE_CCID_CARD
,
403 .instance_size
= sizeof(PassthruState
),
404 .class_init
= passthru_class_initfn
,
407 static void ccid_card_passthru_register_types(void)
409 type_register_static(&passthru_card_info
);
412 type_init(ccid_card_passthru_register_types
)