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 "chardev/char-fe.h"
13 #include "qemu/error-report.h"
14 #include "qemu/sockets.h"
16 #include "cacard/vscard_common.h"
18 #define DPRINTF(card, lvl, fmt, ...) \
20 if (lvl <= card->debug) { \
21 printf("ccid-card-passthru: " fmt , ## __VA_ARGS__); \
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,
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
{
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
];
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 /* XXX this blocks entire thread. Rewrite to use
79 * qemu_chr_fe_write and background I/O callbacks */
80 qemu_chr_fe_write_all(&s
->cs
, (uint8_t *)&scr_msg_header
,
81 sizeof(VSCMsgHeader
));
82 qemu_chr_fe_write_all(&s
->cs
, payload
, length
);
85 static void ccid_card_vscard_send_apdu(PassthruState
*s
,
86 const uint8_t *apdu
, uint32_t length
)
88 ccid_card_vscard_send_msg(
89 s
, VSC_APDU
, VSCARD_MINIMAL_READER_ID
, apdu
, length
);
92 static void ccid_card_vscard_send_error(PassthruState
*s
,
93 uint32_t reader_id
, VSCErrorCode code
)
95 VSCMsgError msg
= {.code
= htonl(code
)};
97 ccid_card_vscard_send_msg(
98 s
, VSC_Error
, reader_id
, (uint8_t *)&msg
, sizeof(msg
));
101 static void ccid_card_vscard_send_init(PassthruState
*s
)
104 .version
= htonl(VSCARD_VERSION
),
105 .magic
= VSCARD_MAGIC
,
109 ccid_card_vscard_send_msg(s
, VSC_Init
, VSCARD_UNDEFINED_READER_ID
,
110 (uint8_t *)&msg
, sizeof(msg
));
113 static int ccid_card_vscard_can_read(void *opaque
)
115 PassthruState
*card
= opaque
;
117 return VSCARD_IN_SIZE
>= card
->vscard_in_pos
?
118 VSCARD_IN_SIZE
- card
->vscard_in_pos
: 0;
121 static void ccid_card_vscard_handle_init(
122 PassthruState
*card
, VSCMsgHeader
*hdr
, VSCMsgInit
*init
)
124 uint32_t *capabilities
;
125 int num_capabilities
;
128 capabilities
= init
->capabilities
;
130 1 + ((hdr
->length
- sizeof(VSCMsgInit
)) / sizeof(uint32_t));
131 init
->version
= ntohl(init
->version
);
132 for (i
= 0 ; i
< num_capabilities
; ++i
) {
133 capabilities
[i
] = ntohl(capabilities
[i
]);
135 if (init
->magic
!= VSCARD_MAGIC
) {
136 error_report("wrong magic");
137 /* we can't disconnect the chardev */
139 if (init
->version
!= VSCARD_VERSION
) {
140 DPRINTF(card
, D_WARN
,
141 "got version %d, have %d", init
->version
, VSCARD_VERSION
);
143 /* future handling of capabilities, none exist atm */
144 ccid_card_vscard_send_init(card
);
147 static int check_atr(PassthruState
*card
, uint8_t *data
, int len
)
149 int historical_length
, opt_bytes
;
156 historical_length
= data
[1] & 0xf;
158 if (data
[0] != 0x3b && data
[0] != 0x3f) {
159 DPRINTF(card
, D_WARN
, "atr's T0 is 0x%X, not in {0x3b, 0x3f}\n",
165 while (td
&& td_count
< 2 && opt_bytes
+ historical_length
+ 2 < len
) {
178 td
= data
[opt_bytes
+ 2] >> 4;
181 if (len
< 2 + historical_length
+ opt_bytes
) {
182 DPRINTF(card
, D_WARN
,
183 "atr too short: len %d, but historical_len %d, T1 0x%X\n",
184 len
, historical_length
, data
[1]);
187 if (len
> 2 + historical_length
+ opt_bytes
) {
188 DPRINTF(card
, D_WARN
,
189 "atr too long: len %d, but hist/opt %d/%d, T1 0x%X\n",
190 len
, historical_length
, opt_bytes
, data
[1]);
193 DPRINTF(card
, D_VERBOSE
,
194 "atr passes check: %d total length, %d historical, %d optional\n",
195 len
, historical_length
, opt_bytes
);
200 static void ccid_card_vscard_handle_message(PassthruState
*card
,
201 VSCMsgHeader
*scr_msg_header
)
203 uint8_t *data
= (uint8_t *)&scr_msg_header
[1];
205 switch (scr_msg_header
->type
) {
207 DPRINTF(card
, D_INFO
, "VSC_ATR %d\n", scr_msg_header
->length
);
208 if (scr_msg_header
->length
> MAX_ATR_SIZE
) {
209 error_report("ATR size exceeds spec, ignoring");
210 ccid_card_vscard_send_error(card
, scr_msg_header
->reader_id
,
214 if (!check_atr(card
, data
, scr_msg_header
->length
)) {
215 error_report("ATR is inconsistent, ignoring");
216 ccid_card_vscard_send_error(card
, scr_msg_header
->reader_id
,
220 memcpy(card
->atr
, data
, scr_msg_header
->length
);
221 card
->atr_length
= scr_msg_header
->length
;
222 ccid_card_card_inserted(&card
->base
);
223 ccid_card_vscard_send_error(card
, scr_msg_header
->reader_id
,
227 ccid_card_send_apdu_to_guest(
228 &card
->base
, data
, scr_msg_header
->length
);
231 DPRINTF(card
, D_INFO
, "VSC_CardRemove\n");
232 ccid_card_card_removed(&card
->base
);
233 ccid_card_vscard_send_error(card
,
234 scr_msg_header
->reader_id
, VSC_SUCCESS
);
237 ccid_card_vscard_handle_init(
238 card
, scr_msg_header
, (VSCMsgInit
*)data
);
241 ccid_card_card_error(&card
->base
, *(uint32_t *)data
);
244 if (ccid_card_ccid_attach(&card
->base
) < 0) {
245 ccid_card_vscard_send_error(card
, VSCARD_UNDEFINED_READER_ID
,
246 VSC_CANNOT_ADD_MORE_READERS
);
248 ccid_card_vscard_send_error(card
, VSCARD_MINIMAL_READER_ID
,
252 case VSC_ReaderRemove
:
253 ccid_card_ccid_detach(&card
->base
);
254 ccid_card_vscard_send_error(card
,
255 scr_msg_header
->reader_id
, VSC_SUCCESS
);
258 printf("usb-ccid: chardev: unexpected message of type %X\n",
259 scr_msg_header
->type
);
260 ccid_card_vscard_send_error(card
, scr_msg_header
->reader_id
,
265 static void ccid_card_vscard_drop_connection(PassthruState
*card
)
267 qemu_chr_fe_deinit(&card
->cs
, true);
268 card
->vscard_in_pos
= card
->vscard_in_hdr
= 0;
271 static void ccid_card_vscard_read(void *opaque
, const uint8_t *buf
, int size
)
273 PassthruState
*card
= opaque
;
276 if (card
->vscard_in_pos
+ size
> VSCARD_IN_SIZE
) {
278 "no room for data: pos %d + size %d > %d. dropping connection.",
279 card
->vscard_in_pos
, size
, VSCARD_IN_SIZE
);
280 ccid_card_vscard_drop_connection(card
);
283 assert(card
->vscard_in_pos
< VSCARD_IN_SIZE
);
284 assert(card
->vscard_in_hdr
< VSCARD_IN_SIZE
);
285 memcpy(card
->vscard_in_data
+ card
->vscard_in_pos
, buf
, size
);
286 card
->vscard_in_pos
+= size
;
287 hdr
= (VSCMsgHeader
*)(card
->vscard_in_data
+ card
->vscard_in_hdr
);
289 while ((card
->vscard_in_pos
- card
->vscard_in_hdr
>= sizeof(VSCMsgHeader
))
290 &&(card
->vscard_in_pos
- card
->vscard_in_hdr
>=
291 sizeof(VSCMsgHeader
) + ntohl(hdr
->length
))) {
292 hdr
->reader_id
= ntohl(hdr
->reader_id
);
293 hdr
->length
= ntohl(hdr
->length
);
294 hdr
->type
= ntohl(hdr
->type
);
295 ccid_card_vscard_handle_message(card
, hdr
);
296 card
->vscard_in_hdr
+= hdr
->length
+ sizeof(VSCMsgHeader
);
297 hdr
= (VSCMsgHeader
*)(card
->vscard_in_data
+ card
->vscard_in_hdr
);
299 if (card
->vscard_in_hdr
== card
->vscard_in_pos
) {
300 card
->vscard_in_pos
= card
->vscard_in_hdr
= 0;
304 static void ccid_card_vscard_event(void *opaque
, int event
)
306 PassthruState
*card
= opaque
;
309 case CHR_EVENT_BREAK
:
310 card
->vscard_in_pos
= card
->vscard_in_hdr
= 0;
312 case CHR_EVENT_OPENED
:
313 DPRINTF(card
, D_INFO
, "%s: CHR_EVENT_OPENED\n", __func__
);
318 /* End VSCard handling */
320 static void passthru_apdu_from_guest(
321 CCIDCardState
*base
, const uint8_t *apdu
, uint32_t len
)
323 PassthruState
*card
= PASSTHRU_CCID_CARD(base
);
325 if (!qemu_chr_fe_backend_connected(&card
->cs
)) {
326 printf("ccid-passthru: no chardev, discarding apdu length %d\n", len
);
329 ccid_card_vscard_send_apdu(card
, apdu
, len
);
332 static const uint8_t *passthru_get_atr(CCIDCardState
*base
, uint32_t *len
)
334 PassthruState
*card
= PASSTHRU_CCID_CARD(base
);
336 *len
= card
->atr_length
;
340 static int passthru_initfn(CCIDCardState
*base
)
342 PassthruState
*card
= PASSTHRU_CCID_CARD(base
);
344 card
->vscard_in_pos
= 0;
345 card
->vscard_in_hdr
= 0;
346 if (qemu_chr_fe_backend_connected(&card
->cs
)) {
347 DPRINTF(card
, D_INFO
, "initing chardev\n");
348 qemu_chr_fe_set_handlers(&card
->cs
,
349 ccid_card_vscard_can_read
,
350 ccid_card_vscard_read
,
351 ccid_card_vscard_event
, NULL
, card
, NULL
, true);
352 ccid_card_vscard_send_init(card
);
354 error_report("missing chardev");
357 card
->debug
= parse_debug_env("QEMU_CCID_PASSTHRU_DEBUG", D_VERBOSE
,
359 assert(sizeof(DEFAULT_ATR
) <= MAX_ATR_SIZE
);
360 memcpy(card
->atr
, DEFAULT_ATR
, sizeof(DEFAULT_ATR
));
361 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
->initfn
= passthru_initfn
;
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
)