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 <libcacard.h>
13 #include "chardev/char-fe.h"
14 #include "qemu/error-report.h"
15 #include "qemu/sockets.h"
17 #include "qapi/error.h"
19 #define DPRINTF(card, lvl, fmt, ...) \
21 if (lvl <= card->debug) { \
22 printf("ccid-card-passthru: " fmt , ## __VA_ARGS__); \
31 /* TODO: do we still need this? */
32 static const uint8_t DEFAULT_ATR
[] = {
34 * From some example somewhere
35 * 0x3B, 0xB0, 0x18, 0x00, 0xD1, 0x81, 0x05, 0xB1, 0x40, 0x38, 0x1F, 0x03, 0x28
38 /* From an Athena smart card */
39 0x3B, 0xD5, 0x18, 0xFF, 0x80, 0x91, 0xFE, 0x1F, 0xC3, 0x80, 0x73, 0xC8, 0x21,
43 #define VSCARD_IN_SIZE 65536
45 /* maximum size of ATR - from 7816-3 */
46 #define MAX_ATR_SIZE 40
48 typedef struct PassthruState PassthruState
;
50 struct PassthruState
{
53 uint8_t vscard_in_data
[VSCARD_IN_SIZE
];
54 uint32_t vscard_in_pos
;
55 uint32_t vscard_in_hdr
;
56 uint8_t atr
[MAX_ATR_SIZE
];
61 #define TYPE_CCID_PASSTHRU "ccid-card-passthru"
62 #define PASSTHRU_CCID_CARD(obj) \
63 OBJECT_CHECK(PassthruState, (obj), TYPE_CCID_PASSTHRU)
66 * VSCard protocol over chardev
67 * This code should not depend on the card type.
70 static void ccid_card_vscard_send_msg(PassthruState
*s
,
71 VSCMsgType type
, uint32_t reader_id
,
72 const uint8_t *payload
, uint32_t length
)
74 VSCMsgHeader scr_msg_header
;
76 scr_msg_header
.type
= htonl(type
);
77 scr_msg_header
.reader_id
= htonl(reader_id
);
78 scr_msg_header
.length
= htonl(length
);
79 /* XXX this blocks entire thread. Rewrite to use
80 * qemu_chr_fe_write and background I/O callbacks */
81 qemu_chr_fe_write_all(&s
->cs
, (uint8_t *)&scr_msg_header
,
82 sizeof(VSCMsgHeader
));
83 qemu_chr_fe_write_all(&s
->cs
, payload
, length
);
86 static void ccid_card_vscard_send_apdu(PassthruState
*s
,
87 const uint8_t *apdu
, uint32_t length
)
89 ccid_card_vscard_send_msg(
90 s
, VSC_APDU
, VSCARD_MINIMAL_READER_ID
, apdu
, length
);
93 static void ccid_card_vscard_send_error(PassthruState
*s
,
94 uint32_t reader_id
, VSCErrorCode code
)
96 VSCMsgError msg
= {.code
= htonl(code
)};
98 ccid_card_vscard_send_msg(
99 s
, VSC_Error
, reader_id
, (uint8_t *)&msg
, sizeof(msg
));
102 static void ccid_card_vscard_send_init(PassthruState
*s
)
105 .version
= htonl(VSCARD_VERSION
),
106 .magic
= VSCARD_MAGIC
,
110 ccid_card_vscard_send_msg(s
, VSC_Init
, VSCARD_UNDEFINED_READER_ID
,
111 (uint8_t *)&msg
, sizeof(msg
));
114 static int ccid_card_vscard_can_read(void *opaque
)
116 PassthruState
*card
= opaque
;
118 return VSCARD_IN_SIZE
>= card
->vscard_in_pos
?
119 VSCARD_IN_SIZE
- card
->vscard_in_pos
: 0;
122 static void ccid_card_vscard_handle_init(
123 PassthruState
*card
, VSCMsgHeader
*hdr
, VSCMsgInit
*init
)
125 uint32_t *capabilities
;
126 int num_capabilities
;
129 capabilities
= init
->capabilities
;
131 1 + ((hdr
->length
- sizeof(VSCMsgInit
)) / sizeof(uint32_t));
132 init
->version
= ntohl(init
->version
);
133 for (i
= 0 ; i
< num_capabilities
; ++i
) {
134 capabilities
[i
] = ntohl(capabilities
[i
]);
136 if (init
->magic
!= VSCARD_MAGIC
) {
137 error_report("wrong magic");
138 /* we can't disconnect the chardev */
140 if (init
->version
!= VSCARD_VERSION
) {
141 DPRINTF(card
, D_WARN
,
142 "got version %d, have %d", init
->version
, VSCARD_VERSION
);
144 /* future handling of capabilities, none exist atm */
145 ccid_card_vscard_send_init(card
);
148 static int check_atr(PassthruState
*card
, uint8_t *data
, int len
)
150 int historical_length
, opt_bytes
;
157 historical_length
= data
[1] & 0xf;
159 if (data
[0] != 0x3b && data
[0] != 0x3f) {
160 DPRINTF(card
, D_WARN
, "atr's T0 is 0x%X, not in {0x3b, 0x3f}\n",
166 while (td
&& td_count
< 2 && opt_bytes
+ historical_length
+ 2 < len
) {
179 td
= data
[opt_bytes
+ 2] >> 4;
182 if (len
< 2 + historical_length
+ opt_bytes
) {
183 DPRINTF(card
, D_WARN
,
184 "atr too short: len %d, but historical_len %d, T1 0x%X\n",
185 len
, historical_length
, data
[1]);
188 if (len
> 2 + historical_length
+ opt_bytes
) {
189 DPRINTF(card
, D_WARN
,
190 "atr too long: len %d, but hist/opt %d/%d, T1 0x%X\n",
191 len
, historical_length
, opt_bytes
, data
[1]);
194 DPRINTF(card
, D_VERBOSE
,
195 "atr passes check: %d total length, %d historical, %d optional\n",
196 len
, historical_length
, opt_bytes
);
201 static void ccid_card_vscard_handle_message(PassthruState
*card
,
202 VSCMsgHeader
*scr_msg_header
)
204 uint8_t *data
= (uint8_t *)&scr_msg_header
[1];
206 switch (scr_msg_header
->type
) {
208 DPRINTF(card
, D_INFO
, "VSC_ATR %d\n", scr_msg_header
->length
);
209 if (scr_msg_header
->length
> MAX_ATR_SIZE
) {
210 error_report("ATR size exceeds spec, ignoring");
211 ccid_card_vscard_send_error(card
, scr_msg_header
->reader_id
,
215 if (!check_atr(card
, data
, scr_msg_header
->length
)) {
216 error_report("ATR is inconsistent, ignoring");
217 ccid_card_vscard_send_error(card
, scr_msg_header
->reader_id
,
221 memcpy(card
->atr
, data
, scr_msg_header
->length
);
222 card
->atr_length
= scr_msg_header
->length
;
223 ccid_card_card_inserted(&card
->base
);
224 ccid_card_vscard_send_error(card
, scr_msg_header
->reader_id
,
228 ccid_card_send_apdu_to_guest(
229 &card
->base
, data
, scr_msg_header
->length
);
232 DPRINTF(card
, D_INFO
, "VSC_CardRemove\n");
233 ccid_card_card_removed(&card
->base
);
234 ccid_card_vscard_send_error(card
,
235 scr_msg_header
->reader_id
, VSC_SUCCESS
);
238 ccid_card_vscard_handle_init(
239 card
, scr_msg_header
, (VSCMsgInit
*)data
);
242 ccid_card_card_error(&card
->base
, *(uint32_t *)data
);
245 if (ccid_card_ccid_attach(&card
->base
) < 0) {
246 ccid_card_vscard_send_error(card
, VSCARD_UNDEFINED_READER_ID
,
247 VSC_CANNOT_ADD_MORE_READERS
);
249 ccid_card_vscard_send_error(card
, VSCARD_MINIMAL_READER_ID
,
253 case VSC_ReaderRemove
:
254 ccid_card_ccid_detach(&card
->base
);
255 ccid_card_vscard_send_error(card
,
256 scr_msg_header
->reader_id
, VSC_SUCCESS
);
259 printf("usb-ccid: chardev: unexpected message of type %X\n",
260 scr_msg_header
->type
);
261 ccid_card_vscard_send_error(card
, scr_msg_header
->reader_id
,
266 static void ccid_card_vscard_drop_connection(PassthruState
*card
)
268 qemu_chr_fe_deinit(&card
->cs
, true);
269 card
->vscard_in_pos
= card
->vscard_in_hdr
= 0;
272 static void ccid_card_vscard_read(void *opaque
, const uint8_t *buf
, int size
)
274 PassthruState
*card
= opaque
;
277 if (card
->vscard_in_pos
+ size
> VSCARD_IN_SIZE
) {
279 "no room for data: pos %d + size %d > %d. dropping connection.",
280 card
->vscard_in_pos
, size
, VSCARD_IN_SIZE
);
281 ccid_card_vscard_drop_connection(card
);
284 assert(card
->vscard_in_pos
< VSCARD_IN_SIZE
);
285 assert(card
->vscard_in_hdr
< VSCARD_IN_SIZE
);
286 memcpy(card
->vscard_in_data
+ card
->vscard_in_pos
, buf
, size
);
287 card
->vscard_in_pos
+= size
;
288 hdr
= (VSCMsgHeader
*)(card
->vscard_in_data
+ card
->vscard_in_hdr
);
290 while ((card
->vscard_in_pos
- card
->vscard_in_hdr
>= sizeof(VSCMsgHeader
))
291 &&(card
->vscard_in_pos
- card
->vscard_in_hdr
>=
292 sizeof(VSCMsgHeader
) + ntohl(hdr
->length
))) {
293 hdr
->reader_id
= ntohl(hdr
->reader_id
);
294 hdr
->length
= ntohl(hdr
->length
);
295 hdr
->type
= ntohl(hdr
->type
);
296 ccid_card_vscard_handle_message(card
, hdr
);
297 card
->vscard_in_hdr
+= hdr
->length
+ sizeof(VSCMsgHeader
);
298 hdr
= (VSCMsgHeader
*)(card
->vscard_in_data
+ card
->vscard_in_hdr
);
300 if (card
->vscard_in_hdr
== card
->vscard_in_pos
) {
301 card
->vscard_in_pos
= card
->vscard_in_hdr
= 0;
305 static void ccid_card_vscard_event(void *opaque
, int event
)
307 PassthruState
*card
= opaque
;
310 case CHR_EVENT_BREAK
:
311 card
->vscard_in_pos
= card
->vscard_in_hdr
= 0;
313 case CHR_EVENT_OPENED
:
314 DPRINTF(card
, D_INFO
, "%s: CHR_EVENT_OPENED\n", __func__
);
319 /* End VSCard handling */
321 static void passthru_apdu_from_guest(
322 CCIDCardState
*base
, const uint8_t *apdu
, uint32_t len
)
324 PassthruState
*card
= PASSTHRU_CCID_CARD(base
);
326 if (!qemu_chr_fe_backend_connected(&card
->cs
)) {
327 printf("ccid-passthru: no chardev, discarding apdu length %d\n", len
);
330 ccid_card_vscard_send_apdu(card
, apdu
, len
);
333 static const uint8_t *passthru_get_atr(CCIDCardState
*base
, uint32_t *len
)
335 PassthruState
*card
= PASSTHRU_CCID_CARD(base
);
337 *len
= card
->atr_length
;
341 static void passthru_realize(CCIDCardState
*base
, Error
**errp
)
343 PassthruState
*card
= PASSTHRU_CCID_CARD(base
);
345 card
->vscard_in_pos
= 0;
346 card
->vscard_in_hdr
= 0;
347 if (qemu_chr_fe_backend_connected(&card
->cs
)) {
348 error_setg(errp
, "ccid-card-passthru: initing chardev");
349 qemu_chr_fe_set_handlers(&card
->cs
,
350 ccid_card_vscard_can_read
,
351 ccid_card_vscard_read
,
352 ccid_card_vscard_event
, NULL
, card
, NULL
, true);
353 ccid_card_vscard_send_init(card
);
355 error_setg(errp
, "missing chardev");
358 card
->debug
= parse_debug_env("QEMU_CCID_PASSTHRU_DEBUG", D_VERBOSE
,
360 assert(sizeof(DEFAULT_ATR
) <= MAX_ATR_SIZE
);
361 memcpy(card
->atr
, DEFAULT_ATR
, sizeof(DEFAULT_ATR
));
362 card
->atr_length
= sizeof(DEFAULT_ATR
);
365 static VMStateDescription passthru_vmstate
= {
366 .name
= "ccid-card-passthru",
368 .minimum_version_id
= 1,
369 .fields
= (VMStateField
[]) {
370 VMSTATE_BUFFER(vscard_in_data
, PassthruState
),
371 VMSTATE_UINT32(vscard_in_pos
, PassthruState
),
372 VMSTATE_UINT32(vscard_in_hdr
, PassthruState
),
373 VMSTATE_BUFFER(atr
, PassthruState
),
374 VMSTATE_UINT8(atr_length
, PassthruState
),
375 VMSTATE_END_OF_LIST()
379 static Property passthru_card_properties
[] = {
380 DEFINE_PROP_CHR("chardev", PassthruState
, cs
),
381 DEFINE_PROP_UINT8("debug", PassthruState
, debug
, 0),
382 DEFINE_PROP_END_OF_LIST(),
385 static void passthru_class_initfn(ObjectClass
*klass
, void *data
)
387 DeviceClass
*dc
= DEVICE_CLASS(klass
);
388 CCIDCardClass
*cc
= CCID_CARD_CLASS(klass
);
390 cc
->realize
= passthru_realize
;
391 cc
->get_atr
= passthru_get_atr
;
392 cc
->apdu_from_guest
= passthru_apdu_from_guest
;
393 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
394 dc
->desc
= "passthrough smartcard";
395 dc
->vmsd
= &passthru_vmstate
;
396 dc
->props
= passthru_card_properties
;
399 static const TypeInfo passthru_card_info
= {
400 .name
= TYPE_CCID_PASSTHRU
,
401 .parent
= TYPE_CCID_CARD
,
402 .instance_size
= sizeof(PassthruState
),
403 .class_init
= passthru_class_initfn
,
406 static void ccid_card_passthru_register_types(void)
408 type_register_static(&passthru_card_info
);
411 type_init(ccid_card_passthru_register_types
)