2 * CCID Card Device. Emulated card.
4 * Copyright (c) 2011 Red Hat.
5 * Written by Alon Levy.
7 * This code is licensed under the GNU LGPL, version 2 or later.
11 * It can be used to provide access to the local hardware in a non exclusive
12 * way, or it can use certificates. It requires the usb-ccid bus.
14 * Usage 1: standard, mirror hardware reader+card:
15 * qemu .. -usb -device usb-ccid -device ccid-card-emulated
17 * Usage 2: use certificates, no hardware required
18 * one time: create the certificates:
20 * certutil -d /etc/pki/nssdb -x -t "CT,CT,CT" -S -s "CN=user$i" -n user$i
22 * qemu .. -usb -device usb-ccid \
23 * -device ccid-card-emulated,cert1=user1,cert2=user2,cert3=user3
25 * If you use a non default db for the certificates you can specify it using
29 #include "qemu/osdep.h"
30 #include <libcacard.h>
32 #include "qemu/thread.h"
33 #include "qemu/main-loop.h"
34 #include "qemu/module.h"
36 #include "qapi/error.h"
38 #define DPRINTF(card, lvl, fmt, ...) \
40 if (lvl <= card->debug) {\
41 printf("ccid-card-emul: %s: " fmt , __func__, ## __VA_ARGS__);\
46 #define TYPE_EMULATED_CCID "ccid-card-emulated"
47 #define EMULATED_CCID_CARD(obj) \
48 OBJECT_CHECK(EmulatedState, (obj), TYPE_EMULATED_CCID)
50 #define BACKEND_NSS_EMULATED_NAME "nss-emulated"
51 #define BACKEND_CERTIFICATES_NAME "certificates"
54 BACKEND_NSS_EMULATED
= 1,
58 #define DEFAULT_BACKEND BACKEND_NSS_EMULATED
60 typedef struct EmulatedState EmulatedState
;
63 EMUL_READER_INSERT
= 0,
72 static const char *emul_event_to_string(uint32_t emul_event
)
75 case EMUL_READER_INSERT
:
76 return "EMUL_READER_INSERT";
77 case EMUL_READER_REMOVE
:
78 return "EMUL_READER_REMOVE";
79 case EMUL_CARD_INSERT
:
80 return "EMUL_CARD_INSERT";
81 case EMUL_CARD_REMOVE
:
82 return "EMUL_CARD_REMOVE";
84 return "EMUL_GUEST_APDU";
85 case EMUL_RESPONSE_APDU
:
86 return "EMUL_RESPONSE_APDU";
93 typedef struct EmulEvent
{
94 QSIMPLEQ_ENTRY(EmulEvent
) entry
;
111 #define MAX_ATR_SIZE 40
112 struct EmulatedState
{
121 uint8_t atr
[MAX_ATR_SIZE
];
123 QSIMPLEQ_HEAD(, EmulEvent
) event_list
;
124 QemuMutex event_list_mutex
;
125 QemuThread event_thread_id
;
127 QSIMPLEQ_HEAD(, EmulEvent
) guest_apdu_list
;
128 QemuMutex vreader_mutex
; /* and guest_apdu_list mutex */
129 QemuMutex handle_apdu_mutex
;
130 QemuCond handle_apdu_cond
;
131 EventNotifier notifier
;
132 int quit_apdu_thread
;
133 QemuThread apdu_thread_id
;
136 static void emulated_apdu_from_guest(CCIDCardState
*base
,
137 const uint8_t *apdu
, uint32_t len
)
139 EmulatedState
*card
= EMULATED_CCID_CARD(base
);
140 EmulEvent
*event
= (EmulEvent
*)g_malloc(sizeof(EmulEvent
) + len
);
143 event
->p
.data
.type
= EMUL_GUEST_APDU
;
144 event
->p
.data
.len
= len
;
145 memcpy(event
->p
.data
.data
, apdu
, len
);
146 qemu_mutex_lock(&card
->vreader_mutex
);
147 QSIMPLEQ_INSERT_TAIL(&card
->guest_apdu_list
, event
, entry
);
148 qemu_mutex_unlock(&card
->vreader_mutex
);
149 qemu_mutex_lock(&card
->handle_apdu_mutex
);
150 qemu_cond_signal(&card
->handle_apdu_cond
);
151 qemu_mutex_unlock(&card
->handle_apdu_mutex
);
154 static const uint8_t *emulated_get_atr(CCIDCardState
*base
, uint32_t *len
)
156 EmulatedState
*card
= EMULATED_CCID_CARD(base
);
158 *len
= card
->atr_length
;
162 static void emulated_push_event(EmulatedState
*card
, EmulEvent
*event
)
164 qemu_mutex_lock(&card
->event_list_mutex
);
165 QSIMPLEQ_INSERT_TAIL(&(card
->event_list
), event
, entry
);
166 qemu_mutex_unlock(&card
->event_list_mutex
);
167 event_notifier_set(&card
->notifier
);
170 static void emulated_push_type(EmulatedState
*card
, uint32_t type
)
172 EmulEvent
*event
= g_new(EmulEvent
, 1);
175 event
->p
.gen
.type
= type
;
176 emulated_push_event(card
, event
);
179 static void emulated_push_error(EmulatedState
*card
, uint64_t code
)
181 EmulEvent
*event
= g_new(EmulEvent
, 1);
184 event
->p
.error
.type
= EMUL_ERROR
;
185 event
->p
.error
.code
= code
;
186 emulated_push_event(card
, event
);
189 static void emulated_push_data_type(EmulatedState
*card
, uint32_t type
,
190 const uint8_t *data
, uint32_t len
)
192 EmulEvent
*event
= (EmulEvent
*)g_malloc(sizeof(EmulEvent
) + len
);
195 event
->p
.data
.type
= type
;
196 event
->p
.data
.len
= len
;
197 memcpy(event
->p
.data
.data
, data
, len
);
198 emulated_push_event(card
, event
);
201 static void emulated_push_reader_insert(EmulatedState
*card
)
203 emulated_push_type(card
, EMUL_READER_INSERT
);
206 static void emulated_push_reader_remove(EmulatedState
*card
)
208 emulated_push_type(card
, EMUL_READER_REMOVE
);
211 static void emulated_push_card_insert(EmulatedState
*card
,
212 const uint8_t *atr
, uint32_t len
)
214 emulated_push_data_type(card
, EMUL_CARD_INSERT
, atr
, len
);
217 static void emulated_push_card_remove(EmulatedState
*card
)
219 emulated_push_type(card
, EMUL_CARD_REMOVE
);
222 static void emulated_push_response_apdu(EmulatedState
*card
,
223 const uint8_t *apdu
, uint32_t len
)
225 emulated_push_data_type(card
, EMUL_RESPONSE_APDU
, apdu
, len
);
228 #define APDU_BUF_SIZE 270
229 static void *handle_apdu_thread(void* arg
)
231 EmulatedState
*card
= arg
;
232 uint8_t recv_data
[APDU_BUF_SIZE
];
234 VReaderStatus reader_status
;
238 qemu_mutex_lock(&card
->handle_apdu_mutex
);
239 qemu_cond_wait(&card
->handle_apdu_cond
, &card
->handle_apdu_mutex
);
240 qemu_mutex_unlock(&card
->handle_apdu_mutex
);
241 if (card
->quit_apdu_thread
) {
242 card
->quit_apdu_thread
= 0; /* debugging */
245 qemu_mutex_lock(&card
->vreader_mutex
);
246 while (!QSIMPLEQ_EMPTY(&card
->guest_apdu_list
)) {
247 event
= QSIMPLEQ_FIRST(&card
->guest_apdu_list
);
248 assert((unsigned long)event
> 1000);
249 QSIMPLEQ_REMOVE_HEAD(&card
->guest_apdu_list
, entry
);
250 if (event
->p
.data
.type
!= EMUL_GUEST_APDU
) {
251 DPRINTF(card
, 1, "unexpected message in handle_apdu_thread\n");
255 if (card
->reader
== NULL
) {
256 DPRINTF(card
, 1, "reader is NULL\n");
260 recv_len
= sizeof(recv_data
);
261 reader_status
= vreader_xfr_bytes(card
->reader
,
262 event
->p
.data
.data
, event
->p
.data
.len
,
263 recv_data
, &recv_len
);
264 DPRINTF(card
, 2, "got back apdu of length %d\n", recv_len
);
265 if (reader_status
== VREADER_OK
) {
266 emulated_push_response_apdu(card
, recv_data
, recv_len
);
268 emulated_push_error(card
, reader_status
);
272 qemu_mutex_unlock(&card
->vreader_mutex
);
277 static void *event_thread(void *arg
)
279 int atr_len
= MAX_ATR_SIZE
;
280 uint8_t atr
[MAX_ATR_SIZE
];
281 VEvent
*event
= NULL
;
282 EmulatedState
*card
= arg
;
285 const char *reader_name
;
287 event
= vevent_wait_next_vevent();
288 if (event
== NULL
|| event
->type
== VEVENT_LAST
) {
291 if (event
->type
!= VEVENT_READER_INSERT
) {
292 if (card
->reader
== NULL
&& event
->reader
!= NULL
) {
293 /* Happens after device_add followed by card remove or insert.
294 * XXX: create synthetic add_reader events if vcard_emul_init
295 * already called, which happens if device_del and device_add
297 card
->reader
= vreader_reference(event
->reader
);
299 if (event
->reader
!= card
->reader
) {
301 "ERROR: wrong reader: quiting event_thread\n");
306 switch (event
->type
) {
307 case VEVENT_READER_INSERT
:
308 /* TODO: take a specific reader. i.e. track which reader
309 * we are seeing here, check it is the one we want (the first,
310 * or by a particular name), and ignore if we don't want it.
312 reader_name
= vreader_get_name(event
->reader
);
313 if (card
->reader
!= NULL
) {
314 DPRINTF(card
, 2, "READER INSERT - replacing %s with %s\n",
315 vreader_get_name(card
->reader
), reader_name
);
316 qemu_mutex_lock(&card
->vreader_mutex
);
317 vreader_free(card
->reader
);
318 qemu_mutex_unlock(&card
->vreader_mutex
);
319 emulated_push_reader_remove(card
);
321 qemu_mutex_lock(&card
->vreader_mutex
);
322 DPRINTF(card
, 2, "READER INSERT %s\n", reader_name
);
323 card
->reader
= vreader_reference(event
->reader
);
324 qemu_mutex_unlock(&card
->vreader_mutex
);
325 emulated_push_reader_insert(card
);
327 case VEVENT_READER_REMOVE
:
328 DPRINTF(card
, 2, " READER REMOVE: %s\n",
329 vreader_get_name(event
->reader
));
330 qemu_mutex_lock(&card
->vreader_mutex
);
331 vreader_free(card
->reader
);
333 qemu_mutex_unlock(&card
->vreader_mutex
);
334 emulated_push_reader_remove(card
);
336 case VEVENT_CARD_INSERT
:
337 /* get the ATR (intended as a response to a power on from the
339 atr_len
= MAX_ATR_SIZE
;
340 vreader_power_on(event
->reader
, atr
, &atr_len
);
341 card
->atr_length
= (uint8_t)atr_len
;
342 DPRINTF(card
, 2, " CARD INSERT\n");
343 emulated_push_card_insert(card
, atr
, atr_len
);
345 case VEVENT_CARD_REMOVE
:
346 DPRINTF(card
, 2, " CARD REMOVE\n");
347 emulated_push_card_remove(card
);
349 case VEVENT_LAST
: /* quit */
350 vevent_delete(event
);
356 vevent_delete(event
);
361 static void card_event_handler(EventNotifier
*notifier
)
363 EmulatedState
*card
= container_of(notifier
, EmulatedState
, notifier
);
364 EmulEvent
*event
, *next
;
366 event_notifier_test_and_clear(&card
->notifier
);
367 qemu_mutex_lock(&card
->event_list_mutex
);
368 QSIMPLEQ_FOREACH_SAFE(event
, &card
->event_list
, entry
, next
) {
369 DPRINTF(card
, 2, "event %s\n", emul_event_to_string(event
->p
.gen
.type
));
370 switch (event
->p
.gen
.type
) {
371 case EMUL_RESPONSE_APDU
:
372 ccid_card_send_apdu_to_guest(&card
->base
, event
->p
.data
.data
,
375 case EMUL_READER_INSERT
:
376 ccid_card_ccid_attach(&card
->base
);
378 case EMUL_READER_REMOVE
:
379 ccid_card_ccid_detach(&card
->base
);
381 case EMUL_CARD_INSERT
:
382 assert(event
->p
.data
.len
<= MAX_ATR_SIZE
);
383 card
->atr_length
= event
->p
.data
.len
;
384 memcpy(card
->atr
, event
->p
.data
.data
, card
->atr_length
);
385 ccid_card_card_inserted(&card
->base
);
387 case EMUL_CARD_REMOVE
:
388 ccid_card_card_removed(&card
->base
);
391 ccid_card_card_error(&card
->base
, event
->p
.error
.code
);
394 DPRINTF(card
, 2, "unexpected event\n");
399 QSIMPLEQ_INIT(&card
->event_list
);
400 qemu_mutex_unlock(&card
->event_list_mutex
);
403 static int init_event_notifier(EmulatedState
*card
, Error
**errp
)
405 if (event_notifier_init(&card
->notifier
, false) < 0) {
406 error_setg(errp
, "ccid-card-emul: event notifier creation failed");
409 event_notifier_set_handler(&card
->notifier
, card_event_handler
);
413 static void clean_event_notifier(EmulatedState
*card
)
415 event_notifier_set_handler(&card
->notifier
, NULL
);
416 event_notifier_cleanup(&card
->notifier
);
419 #define CERTIFICATES_DEFAULT_DB "/etc/pki/nssdb"
420 #define CERTIFICATES_ARGS_TEMPLATE\
421 "db=\"%s\" use_hw=no soft=(,Virtual Reader,CAC,,%s,%s,%s)"
423 static int wrap_vcard_emul_init(VCardEmulOptions
*options
)
426 static int options_was_null
;
429 if ((options
== NULL
) != options_was_null
) {
430 printf("%s: warning: running emulated with certificates"
431 " and emulated side by side is not supported\n",
433 return VCARD_EMUL_FAIL
;
435 vcard_emul_replay_insertion_events();
436 return VCARD_EMUL_OK
;
438 options_was_null
= (options
== NULL
);
440 return vcard_emul_init(options
);
443 static int emulated_initialize_vcard_from_certificates(EmulatedState
*card
)
446 VCardEmulOptions
*options
= NULL
;
448 snprintf(emul_args
, sizeof(emul_args
) - 1, CERTIFICATES_ARGS_TEMPLATE
,
449 card
->db
? card
->db
: CERTIFICATES_DEFAULT_DB
,
450 card
->cert1
, card
->cert2
, card
->cert3
);
451 options
= vcard_emul_options(emul_args
);
452 if (options
== NULL
) {
453 printf("%s: warning: not using certificates due to"
454 " initialization error\n", __func__
);
456 return wrap_vcard_emul_init(options
);
459 typedef struct EnumTable
{
464 static const EnumTable backend_enum_table
[] = {
465 {BACKEND_NSS_EMULATED_NAME
, BACKEND_NSS_EMULATED
},
466 {BACKEND_CERTIFICATES_NAME
, BACKEND_CERTIFICATES
},
470 static uint32_t parse_enumeration(char *str
,
471 const EnumTable
*table
, uint32_t not_found_value
)
473 uint32_t ret
= not_found_value
;
478 while (table
->name
!= NULL
) {
479 if (strcmp(table
->name
, str
) == 0) {
488 static void emulated_realize(CCIDCardState
*base
, Error
**errp
)
490 EmulatedState
*card
= EMULATED_CCID_CARD(base
);
492 const EnumTable
*ptable
;
494 QSIMPLEQ_INIT(&card
->event_list
);
495 QSIMPLEQ_INIT(&card
->guest_apdu_list
);
496 qemu_mutex_init(&card
->event_list_mutex
);
497 qemu_mutex_init(&card
->vreader_mutex
);
498 qemu_mutex_init(&card
->handle_apdu_mutex
);
499 qemu_cond_init(&card
->handle_apdu_cond
);
501 card
->quit_apdu_thread
= 0;
502 if (init_event_notifier(card
, errp
) < 0) {
507 if (card
->backend_str
) {
508 card
->backend
= parse_enumeration(card
->backend_str
,
509 backend_enum_table
, 0);
512 if (card
->backend
== 0) {
513 error_setg(errp
, "backend must be one of:");
514 for (ptable
= backend_enum_table
; ptable
->name
!= NULL
; ++ptable
) {
515 error_append_hint(errp
, "%s\n", ptable
->name
);
520 /* TODO: a passthru backened that works on local machine. third card type?*/
521 if (card
->backend
== BACKEND_CERTIFICATES
) {
522 if (card
->cert1
!= NULL
&& card
->cert2
!= NULL
&& card
->cert3
!= NULL
) {
523 ret
= emulated_initialize_vcard_from_certificates(card
);
525 error_setg(errp
, "%s: you must provide all three certs for"
526 " certificates backend", TYPE_EMULATED_CCID
);
530 if (card
->backend
!= BACKEND_NSS_EMULATED
) {
531 error_setg(errp
, "%s: bad backend specified. The options are:%s"
532 " (default), %s.", TYPE_EMULATED_CCID
,
533 BACKEND_NSS_EMULATED_NAME
, BACKEND_CERTIFICATES_NAME
);
536 if (card
->cert1
!= NULL
|| card
->cert2
!= NULL
|| card
->cert3
!= NULL
) {
537 error_setg(errp
, "%s: unexpected cert parameters to nss emulated "
538 "backend", TYPE_EMULATED_CCID
);
541 /* default to mirroring the local hardware readers */
542 ret
= wrap_vcard_emul_init(NULL
);
544 if (ret
!= VCARD_EMUL_OK
) {
545 error_setg(errp
, "%s: failed to initialize vcard", TYPE_EMULATED_CCID
);
548 qemu_thread_create(&card
->event_thread_id
, "ccid/event", event_thread
,
549 card
, QEMU_THREAD_JOINABLE
);
550 qemu_thread_create(&card
->apdu_thread_id
, "ccid/apdu", handle_apdu_thread
,
551 card
, QEMU_THREAD_JOINABLE
);
556 clean_event_notifier(card
);
558 qemu_cond_destroy(&card
->handle_apdu_cond
);
559 qemu_mutex_destroy(&card
->handle_apdu_mutex
);
560 qemu_mutex_destroy(&card
->vreader_mutex
);
561 qemu_mutex_destroy(&card
->event_list_mutex
);
564 static void emulated_unrealize(CCIDCardState
*base
, Error
**errp
)
566 EmulatedState
*card
= EMULATED_CCID_CARD(base
);
567 VEvent
*vevent
= vevent_new(VEVENT_LAST
, NULL
, NULL
);
569 vevent_queue_vevent(vevent
); /* stop vevent thread */
570 qemu_thread_join(&card
->event_thread_id
);
572 card
->quit_apdu_thread
= 1; /* stop handle_apdu thread */
573 qemu_cond_signal(&card
->handle_apdu_cond
);
574 qemu_thread_join(&card
->apdu_thread_id
);
576 clean_event_notifier(card
);
577 /* threads exited, can destroy all condvars/mutexes */
578 qemu_cond_destroy(&card
->handle_apdu_cond
);
579 qemu_mutex_destroy(&card
->handle_apdu_mutex
);
580 qemu_mutex_destroy(&card
->vreader_mutex
);
581 qemu_mutex_destroy(&card
->event_list_mutex
);
584 static Property emulated_card_properties
[] = {
585 DEFINE_PROP_STRING("backend", EmulatedState
, backend_str
),
586 DEFINE_PROP_STRING("cert1", EmulatedState
, cert1
),
587 DEFINE_PROP_STRING("cert2", EmulatedState
, cert2
),
588 DEFINE_PROP_STRING("cert3", EmulatedState
, cert3
),
589 DEFINE_PROP_STRING("db", EmulatedState
, db
),
590 DEFINE_PROP_UINT8("debug", EmulatedState
, debug
, 0),
591 DEFINE_PROP_END_OF_LIST(),
594 static void emulated_class_initfn(ObjectClass
*klass
, void *data
)
596 DeviceClass
*dc
= DEVICE_CLASS(klass
);
597 CCIDCardClass
*cc
= CCID_CARD_CLASS(klass
);
599 cc
->realize
= emulated_realize
;
600 cc
->unrealize
= emulated_unrealize
;
601 cc
->get_atr
= emulated_get_atr
;
602 cc
->apdu_from_guest
= emulated_apdu_from_guest
;
603 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
604 dc
->desc
= "emulated smartcard";
605 dc
->props
= emulated_card_properties
;
608 static const TypeInfo emulated_card_info
= {
609 .name
= TYPE_EMULATED_CCID
,
610 .parent
= TYPE_CCID_CARD
,
611 .instance_size
= sizeof(EmulatedState
),
612 .class_init
= emulated_class_initfn
,
615 static void ccid_card_emulated_register_types(void)
617 type_register_static(&emulated_card_info
);
620 type_init(ccid_card_emulated_register_types
)