Statistics
| Branch: | Revision:

root / hw / ccid-card-emulated.c @ 927d4878

History | View | Annotate | Download (18.5 kB)

1 585738a6 Alon Levy
/*
2 585738a6 Alon Levy
 * CCID Card Device. Emulated card.
3 585738a6 Alon Levy
 *
4 585738a6 Alon Levy
 * Copyright (c) 2011 Red Hat.
5 585738a6 Alon Levy
 * Written by Alon Levy.
6 585738a6 Alon Levy
 *
7 8e31bf38 Matthew Fernandez
 * This code is licensed under the GNU LGPL, version 2 or later.
8 585738a6 Alon Levy
 */
9 585738a6 Alon Levy
10 585738a6 Alon Levy
/*
11 585738a6 Alon Levy
 * It can be used to provide access to the local hardware in a non exclusive
12 585738a6 Alon Levy
 * way, or it can use certificates. It requires the usb-ccid bus.
13 585738a6 Alon Levy
 *
14 585738a6 Alon Levy
 * Usage 1: standard, mirror hardware reader+card:
15 585738a6 Alon Levy
 * qemu .. -usb -device usb-ccid -device ccid-card-emulated
16 585738a6 Alon Levy
 *
17 585738a6 Alon Levy
 * Usage 2: use certificates, no hardware required
18 585738a6 Alon Levy
 * one time: create the certificates:
19 585738a6 Alon Levy
 *  for i in 1 2 3; do
20 585738a6 Alon Levy
 *      certutil -d /etc/pki/nssdb -x -t "CT,CT,CT" -S -s "CN=user$i" -n user$i
21 585738a6 Alon Levy
 *  done
22 585738a6 Alon Levy
 * qemu .. -usb -device usb-ccid \
23 585738a6 Alon Levy
 *  -device ccid-card-emulated,cert1=user1,cert2=user2,cert3=user3
24 585738a6 Alon Levy
 *
25 585738a6 Alon Levy
 * If you use a non default db for the certificates you can specify it using
26 585738a6 Alon Levy
 * the db parameter.
27 585738a6 Alon Levy
 */
28 585738a6 Alon Levy
29 585738a6 Alon Levy
#include <eventt.h>
30 585738a6 Alon Levy
#include <vevent.h>
31 585738a6 Alon Levy
#include <vreader.h>
32 585738a6 Alon Levy
#include <vcard_emul.h>
33 585738a6 Alon Levy
34 1de7afc9 Paolo Bonzini
#include "qemu/thread.h"
35 927d4878 Paolo Bonzini
#include "char/char.h"
36 83c9089e Paolo Bonzini
#include "monitor/monitor.h"
37 585738a6 Alon Levy
#include "hw/ccid.h"
38 585738a6 Alon Levy
39 585738a6 Alon Levy
#define DPRINTF(card, lvl, fmt, ...) \
40 585738a6 Alon Levy
do {\
41 585738a6 Alon Levy
    if (lvl <= card->debug) {\
42 585738a6 Alon Levy
        printf("ccid-card-emul: %s: " fmt , __func__, ## __VA_ARGS__);\
43 585738a6 Alon Levy
    } \
44 585738a6 Alon Levy
} while (0)
45 585738a6 Alon Levy
46 585738a6 Alon Levy
#define EMULATED_DEV_NAME "ccid-card-emulated"
47 585738a6 Alon Levy
48 585738a6 Alon Levy
#define BACKEND_NSS_EMULATED_NAME "nss-emulated"
49 585738a6 Alon Levy
#define BACKEND_CERTIFICATES_NAME "certificates"
50 585738a6 Alon Levy
51 585738a6 Alon Levy
enum {
52 585738a6 Alon Levy
    BACKEND_NSS_EMULATED = 1,
53 585738a6 Alon Levy
    BACKEND_CERTIFICATES
54 585738a6 Alon Levy
};
55 585738a6 Alon Levy
56 585738a6 Alon Levy
#define DEFAULT_BACKEND BACKEND_NSS_EMULATED
57 585738a6 Alon Levy
58 585738a6 Alon Levy
typedef struct EmulatedState EmulatedState;
59 585738a6 Alon Levy
60 585738a6 Alon Levy
enum {
61 585738a6 Alon Levy
    EMUL_READER_INSERT = 0,
62 585738a6 Alon Levy
    EMUL_READER_REMOVE,
63 585738a6 Alon Levy
    EMUL_CARD_INSERT,
64 585738a6 Alon Levy
    EMUL_CARD_REMOVE,
65 585738a6 Alon Levy
    EMUL_GUEST_APDU,
66 585738a6 Alon Levy
    EMUL_RESPONSE_APDU,
67 585738a6 Alon Levy
    EMUL_ERROR,
68 585738a6 Alon Levy
};
69 585738a6 Alon Levy
70 585738a6 Alon Levy
static const char *emul_event_to_string(uint32_t emul_event)
71 585738a6 Alon Levy
{
72 585738a6 Alon Levy
    switch (emul_event) {
73 585738a6 Alon Levy
    case EMUL_READER_INSERT:
74 585738a6 Alon Levy
        return "EMUL_READER_INSERT";
75 585738a6 Alon Levy
    case EMUL_READER_REMOVE:
76 585738a6 Alon Levy
        return "EMUL_READER_REMOVE";
77 585738a6 Alon Levy
    case EMUL_CARD_INSERT:
78 585738a6 Alon Levy
        return "EMUL_CARD_INSERT";
79 585738a6 Alon Levy
    case EMUL_CARD_REMOVE:
80 585738a6 Alon Levy
        return "EMUL_CARD_REMOVE";
81 585738a6 Alon Levy
    case EMUL_GUEST_APDU:
82 585738a6 Alon Levy
        return "EMUL_GUEST_APDU";
83 585738a6 Alon Levy
    case EMUL_RESPONSE_APDU:
84 585738a6 Alon Levy
        return "EMUL_RESPONSE_APDU";
85 585738a6 Alon Levy
    case EMUL_ERROR:
86 585738a6 Alon Levy
        return "EMUL_ERROR";
87 585738a6 Alon Levy
    }
88 585738a6 Alon Levy
    return "UNKNOWN";
89 585738a6 Alon Levy
}
90 585738a6 Alon Levy
91 585738a6 Alon Levy
typedef struct EmulEvent {
92 585738a6 Alon Levy
    QSIMPLEQ_ENTRY(EmulEvent) entry;
93 585738a6 Alon Levy
    union {
94 585738a6 Alon Levy
        struct {
95 585738a6 Alon Levy
            uint32_t type;
96 585738a6 Alon Levy
        } gen;
97 585738a6 Alon Levy
        struct {
98 585738a6 Alon Levy
            uint32_t type;
99 585738a6 Alon Levy
            uint64_t code;
100 585738a6 Alon Levy
        } error;
101 585738a6 Alon Levy
        struct {
102 585738a6 Alon Levy
            uint32_t type;
103 585738a6 Alon Levy
            uint32_t len;
104 585738a6 Alon Levy
            uint8_t data[];
105 585738a6 Alon Levy
        } data;
106 585738a6 Alon Levy
    } p;
107 585738a6 Alon Levy
} EmulEvent;
108 585738a6 Alon Levy
109 585738a6 Alon Levy
#define MAX_ATR_SIZE 40
110 585738a6 Alon Levy
struct EmulatedState {
111 585738a6 Alon Levy
    CCIDCardState base;
112 585738a6 Alon Levy
    uint8_t  debug;
113 585738a6 Alon Levy
    char    *backend_str;
114 585738a6 Alon Levy
    uint32_t backend;
115 585738a6 Alon Levy
    char    *cert1;
116 585738a6 Alon Levy
    char    *cert2;
117 585738a6 Alon Levy
    char    *cert3;
118 585738a6 Alon Levy
    char    *db;
119 585738a6 Alon Levy
    uint8_t  atr[MAX_ATR_SIZE];
120 585738a6 Alon Levy
    uint8_t  atr_length;
121 585738a6 Alon Levy
    QSIMPLEQ_HEAD(event_list, EmulEvent) event_list;
122 585738a6 Alon Levy
    QemuMutex event_list_mutex;
123 da5361cc Paolo Bonzini
    QemuThread event_thread_id;
124 585738a6 Alon Levy
    VReader *reader;
125 585738a6 Alon Levy
    QSIMPLEQ_HEAD(guest_apdu_list, EmulEvent) guest_apdu_list;
126 585738a6 Alon Levy
    QemuMutex vreader_mutex; /* and guest_apdu_list mutex */
127 585738a6 Alon Levy
    QemuMutex handle_apdu_mutex;
128 585738a6 Alon Levy
    QemuCond handle_apdu_cond;
129 585738a6 Alon Levy
    int      pipe[2];
130 585738a6 Alon Levy
    int      quit_apdu_thread;
131 da5361cc Paolo Bonzini
    QemuThread apdu_thread_id;
132 585738a6 Alon Levy
};
133 585738a6 Alon Levy
134 585738a6 Alon Levy
static void emulated_apdu_from_guest(CCIDCardState *base,
135 585738a6 Alon Levy
    const uint8_t *apdu, uint32_t len)
136 585738a6 Alon Levy
{
137 585738a6 Alon Levy
    EmulatedState *card = DO_UPCAST(EmulatedState, base, base);
138 7267c094 Anthony Liguori
    EmulEvent *event = (EmulEvent *)g_malloc(sizeof(EmulEvent) + len);
139 585738a6 Alon Levy
140 585738a6 Alon Levy
    assert(event);
141 585738a6 Alon Levy
    event->p.data.type = EMUL_GUEST_APDU;
142 585738a6 Alon Levy
    event->p.data.len = len;
143 585738a6 Alon Levy
    memcpy(event->p.data.data, apdu, len);
144 585738a6 Alon Levy
    qemu_mutex_lock(&card->vreader_mutex);
145 585738a6 Alon Levy
    QSIMPLEQ_INSERT_TAIL(&card->guest_apdu_list, event, entry);
146 585738a6 Alon Levy
    qemu_mutex_unlock(&card->vreader_mutex);
147 585738a6 Alon Levy
    qemu_mutex_lock(&card->handle_apdu_mutex);
148 585738a6 Alon Levy
    qemu_cond_signal(&card->handle_apdu_cond);
149 585738a6 Alon Levy
    qemu_mutex_unlock(&card->handle_apdu_mutex);
150 585738a6 Alon Levy
}
151 585738a6 Alon Levy
152 585738a6 Alon Levy
static const uint8_t *emulated_get_atr(CCIDCardState *base, uint32_t *len)
153 585738a6 Alon Levy
{
154 585738a6 Alon Levy
    EmulatedState *card = DO_UPCAST(EmulatedState, base, base);
155 585738a6 Alon Levy
156 585738a6 Alon Levy
    *len = card->atr_length;
157 585738a6 Alon Levy
    return card->atr;
158 585738a6 Alon Levy
}
159 585738a6 Alon Levy
160 585738a6 Alon Levy
static void emulated_push_event(EmulatedState *card, EmulEvent *event)
161 585738a6 Alon Levy
{
162 585738a6 Alon Levy
    qemu_mutex_lock(&card->event_list_mutex);
163 585738a6 Alon Levy
    QSIMPLEQ_INSERT_TAIL(&(card->event_list), event, entry);
164 585738a6 Alon Levy
    qemu_mutex_unlock(&card->event_list_mutex);
165 585738a6 Alon Levy
    if (write(card->pipe[1], card, 1) != 1) {
166 585738a6 Alon Levy
        DPRINTF(card, 1, "write to pipe failed\n");
167 585738a6 Alon Levy
    }
168 585738a6 Alon Levy
}
169 585738a6 Alon Levy
170 585738a6 Alon Levy
static void emulated_push_type(EmulatedState *card, uint32_t type)
171 585738a6 Alon Levy
{
172 7267c094 Anthony Liguori
    EmulEvent *event = (EmulEvent *)g_malloc(sizeof(EmulEvent));
173 585738a6 Alon Levy
174 585738a6 Alon Levy
    assert(event);
175 585738a6 Alon Levy
    event->p.gen.type = type;
176 585738a6 Alon Levy
    emulated_push_event(card, event);
177 585738a6 Alon Levy
}
178 585738a6 Alon Levy
179 585738a6 Alon Levy
static void emulated_push_error(EmulatedState *card, uint64_t code)
180 585738a6 Alon Levy
{
181 7267c094 Anthony Liguori
    EmulEvent *event = (EmulEvent *)g_malloc(sizeof(EmulEvent));
182 585738a6 Alon Levy
183 585738a6 Alon Levy
    assert(event);
184 585738a6 Alon Levy
    event->p.error.type = EMUL_ERROR;
185 585738a6 Alon Levy
    event->p.error.code = code;
186 585738a6 Alon Levy
    emulated_push_event(card, event);
187 585738a6 Alon Levy
}
188 585738a6 Alon Levy
189 585738a6 Alon Levy
static void emulated_push_data_type(EmulatedState *card, uint32_t type,
190 585738a6 Alon Levy
    const uint8_t *data, uint32_t len)
191 585738a6 Alon Levy
{
192 7267c094 Anthony Liguori
    EmulEvent *event = (EmulEvent *)g_malloc(sizeof(EmulEvent) + len);
193 585738a6 Alon Levy
194 585738a6 Alon Levy
    assert(event);
195 585738a6 Alon Levy
    event->p.data.type = type;
196 585738a6 Alon Levy
    event->p.data.len = len;
197 585738a6 Alon Levy
    memcpy(event->p.data.data, data, len);
198 585738a6 Alon Levy
    emulated_push_event(card, event);
199 585738a6 Alon Levy
}
200 585738a6 Alon Levy
201 585738a6 Alon Levy
static void emulated_push_reader_insert(EmulatedState *card)
202 585738a6 Alon Levy
{
203 585738a6 Alon Levy
    emulated_push_type(card, EMUL_READER_INSERT);
204 585738a6 Alon Levy
}
205 585738a6 Alon Levy
206 585738a6 Alon Levy
static void emulated_push_reader_remove(EmulatedState *card)
207 585738a6 Alon Levy
{
208 585738a6 Alon Levy
    emulated_push_type(card, EMUL_READER_REMOVE);
209 585738a6 Alon Levy
}
210 585738a6 Alon Levy
211 585738a6 Alon Levy
static void emulated_push_card_insert(EmulatedState *card,
212 585738a6 Alon Levy
    const uint8_t *atr, uint32_t len)
213 585738a6 Alon Levy
{
214 585738a6 Alon Levy
    emulated_push_data_type(card, EMUL_CARD_INSERT, atr, len);
215 585738a6 Alon Levy
}
216 585738a6 Alon Levy
217 585738a6 Alon Levy
static void emulated_push_card_remove(EmulatedState *card)
218 585738a6 Alon Levy
{
219 585738a6 Alon Levy
    emulated_push_type(card, EMUL_CARD_REMOVE);
220 585738a6 Alon Levy
}
221 585738a6 Alon Levy
222 585738a6 Alon Levy
static void emulated_push_response_apdu(EmulatedState *card,
223 585738a6 Alon Levy
    const uint8_t *apdu, uint32_t len)
224 585738a6 Alon Levy
{
225 585738a6 Alon Levy
    emulated_push_data_type(card, EMUL_RESPONSE_APDU, apdu, len);
226 585738a6 Alon Levy
}
227 585738a6 Alon Levy
228 585738a6 Alon Levy
#define APDU_BUF_SIZE 270
229 585738a6 Alon Levy
static void *handle_apdu_thread(void* arg)
230 585738a6 Alon Levy
{
231 585738a6 Alon Levy
    EmulatedState *card = arg;
232 585738a6 Alon Levy
    uint8_t recv_data[APDU_BUF_SIZE];
233 585738a6 Alon Levy
    int recv_len;
234 585738a6 Alon Levy
    VReaderStatus reader_status;
235 585738a6 Alon Levy
    EmulEvent *event;
236 585738a6 Alon Levy
237 585738a6 Alon Levy
    while (1) {
238 585738a6 Alon Levy
        qemu_mutex_lock(&card->handle_apdu_mutex);
239 585738a6 Alon Levy
        qemu_cond_wait(&card->handle_apdu_cond, &card->handle_apdu_mutex);
240 585738a6 Alon Levy
        qemu_mutex_unlock(&card->handle_apdu_mutex);
241 585738a6 Alon Levy
        if (card->quit_apdu_thread) {
242 585738a6 Alon Levy
            card->quit_apdu_thread = 0; /* debugging */
243 585738a6 Alon Levy
            break;
244 585738a6 Alon Levy
        }
245 585738a6 Alon Levy
        qemu_mutex_lock(&card->vreader_mutex);
246 585738a6 Alon Levy
        while (!QSIMPLEQ_EMPTY(&card->guest_apdu_list)) {
247 585738a6 Alon Levy
            event = QSIMPLEQ_FIRST(&card->guest_apdu_list);
248 585738a6 Alon Levy
            assert((unsigned long)event > 1000);
249 585738a6 Alon Levy
            QSIMPLEQ_REMOVE_HEAD(&card->guest_apdu_list, entry);
250 585738a6 Alon Levy
            if (event->p.data.type != EMUL_GUEST_APDU) {
251 585738a6 Alon Levy
                DPRINTF(card, 1, "unexpected message in handle_apdu_thread\n");
252 7267c094 Anthony Liguori
                g_free(event);
253 585738a6 Alon Levy
                continue;
254 585738a6 Alon Levy
            }
255 585738a6 Alon Levy
            if (card->reader == NULL) {
256 585738a6 Alon Levy
                DPRINTF(card, 1, "reader is NULL\n");
257 7267c094 Anthony Liguori
                g_free(event);
258 585738a6 Alon Levy
                continue;
259 585738a6 Alon Levy
            }
260 585738a6 Alon Levy
            recv_len = sizeof(recv_data);
261 585738a6 Alon Levy
            reader_status = vreader_xfr_bytes(card->reader,
262 585738a6 Alon Levy
                    event->p.data.data, event->p.data.len,
263 585738a6 Alon Levy
                    recv_data, &recv_len);
264 585738a6 Alon Levy
            DPRINTF(card, 2, "got back apdu of length %d\n", recv_len);
265 585738a6 Alon Levy
            if (reader_status == VREADER_OK) {
266 585738a6 Alon Levy
                emulated_push_response_apdu(card, recv_data, recv_len);
267 585738a6 Alon Levy
            } else {
268 585738a6 Alon Levy
                emulated_push_error(card, reader_status);
269 585738a6 Alon Levy
            }
270 7267c094 Anthony Liguori
            g_free(event);
271 585738a6 Alon Levy
        }
272 585738a6 Alon Levy
        qemu_mutex_unlock(&card->vreader_mutex);
273 585738a6 Alon Levy
    }
274 585738a6 Alon Levy
    return NULL;
275 585738a6 Alon Levy
}
276 585738a6 Alon Levy
277 585738a6 Alon Levy
static void *event_thread(void *arg)
278 585738a6 Alon Levy
{
279 585738a6 Alon Levy
    int atr_len = MAX_ATR_SIZE;
280 585738a6 Alon Levy
    uint8_t atr[MAX_ATR_SIZE];
281 585738a6 Alon Levy
    VEvent *event = NULL;
282 585738a6 Alon Levy
    EmulatedState *card = arg;
283 585738a6 Alon Levy
284 585738a6 Alon Levy
    while (1) {
285 585738a6 Alon Levy
        const char *reader_name;
286 585738a6 Alon Levy
287 585738a6 Alon Levy
        event = vevent_wait_next_vevent();
288 585738a6 Alon Levy
        if (event == NULL || event->type == VEVENT_LAST) {
289 585738a6 Alon Levy
            break;
290 585738a6 Alon Levy
        }
291 585738a6 Alon Levy
        if (event->type != VEVENT_READER_INSERT) {
292 585738a6 Alon Levy
            if (card->reader == NULL && event->reader != NULL) {
293 585738a6 Alon Levy
                /* Happens after device_add followed by card remove or insert.
294 585738a6 Alon Levy
                 * XXX: create synthetic add_reader events if vcard_emul_init
295 585738a6 Alon Levy
                 * already called, which happens if device_del and device_add
296 585738a6 Alon Levy
                 * are called */
297 585738a6 Alon Levy
                card->reader = vreader_reference(event->reader);
298 585738a6 Alon Levy
            } else {
299 585738a6 Alon Levy
                if (event->reader != card->reader) {
300 585738a6 Alon Levy
                    fprintf(stderr,
301 585738a6 Alon Levy
                        "ERROR: wrong reader: quiting event_thread\n");
302 585738a6 Alon Levy
                    break;
303 585738a6 Alon Levy
                }
304 585738a6 Alon Levy
            }
305 585738a6 Alon Levy
        }
306 585738a6 Alon Levy
        switch (event->type) {
307 585738a6 Alon Levy
        case VEVENT_READER_INSERT:
308 585738a6 Alon Levy
            /* TODO: take a specific reader. i.e. track which reader
309 585738a6 Alon Levy
             * we are seeing here, check it is the one we want (the first,
310 585738a6 Alon Levy
             * or by a particular name), and ignore if we don't want it.
311 585738a6 Alon Levy
             */
312 585738a6 Alon Levy
            reader_name = vreader_get_name(event->reader);
313 585738a6 Alon Levy
            if (card->reader != NULL) {
314 585738a6 Alon Levy
                DPRINTF(card, 2, "READER INSERT - replacing %s with %s\n",
315 585738a6 Alon Levy
                    vreader_get_name(card->reader), reader_name);
316 585738a6 Alon Levy
                qemu_mutex_lock(&card->vreader_mutex);
317 585738a6 Alon Levy
                vreader_free(card->reader);
318 585738a6 Alon Levy
                qemu_mutex_unlock(&card->vreader_mutex);
319 585738a6 Alon Levy
                emulated_push_reader_remove(card);
320 585738a6 Alon Levy
            }
321 585738a6 Alon Levy
            qemu_mutex_lock(&card->vreader_mutex);
322 585738a6 Alon Levy
            DPRINTF(card, 2, "READER INSERT %s\n", reader_name);
323 585738a6 Alon Levy
            card->reader = vreader_reference(event->reader);
324 585738a6 Alon Levy
            qemu_mutex_unlock(&card->vreader_mutex);
325 585738a6 Alon Levy
            emulated_push_reader_insert(card);
326 585738a6 Alon Levy
            break;
327 585738a6 Alon Levy
        case VEVENT_READER_REMOVE:
328 585738a6 Alon Levy
            DPRINTF(card, 2, " READER REMOVE: %s\n",
329 585738a6 Alon Levy
                    vreader_get_name(event->reader));
330 585738a6 Alon Levy
            qemu_mutex_lock(&card->vreader_mutex);
331 585738a6 Alon Levy
            vreader_free(card->reader);
332 585738a6 Alon Levy
            card->reader = NULL;
333 585738a6 Alon Levy
            qemu_mutex_unlock(&card->vreader_mutex);
334 585738a6 Alon Levy
            emulated_push_reader_remove(card);
335 585738a6 Alon Levy
            break;
336 585738a6 Alon Levy
        case VEVENT_CARD_INSERT:
337 585738a6 Alon Levy
            /* get the ATR (intended as a response to a power on from the
338 585738a6 Alon Levy
             * reader */
339 585738a6 Alon Levy
            atr_len = MAX_ATR_SIZE;
340 585738a6 Alon Levy
            vreader_power_on(event->reader, atr, &atr_len);
341 585738a6 Alon Levy
            card->atr_length = (uint8_t)atr_len;
342 585738a6 Alon Levy
            DPRINTF(card, 2, " CARD INSERT\n");
343 585738a6 Alon Levy
            emulated_push_card_insert(card, atr, atr_len);
344 585738a6 Alon Levy
            break;
345 585738a6 Alon Levy
        case VEVENT_CARD_REMOVE:
346 585738a6 Alon Levy
            DPRINTF(card, 2, " CARD REMOVE\n");
347 585738a6 Alon Levy
            emulated_push_card_remove(card);
348 585738a6 Alon Levy
            break;
349 585738a6 Alon Levy
        case VEVENT_LAST: /* quit */
350 585738a6 Alon Levy
            vevent_delete(event);
351 585738a6 Alon Levy
            return NULL;
352 585738a6 Alon Levy
            break;
353 585738a6 Alon Levy
        default:
354 585738a6 Alon Levy
            break;
355 585738a6 Alon Levy
        }
356 585738a6 Alon Levy
        vevent_delete(event);
357 585738a6 Alon Levy
    }
358 585738a6 Alon Levy
    return NULL;
359 585738a6 Alon Levy
}
360 585738a6 Alon Levy
361 585738a6 Alon Levy
static void pipe_read(void *opaque)
362 585738a6 Alon Levy
{
363 585738a6 Alon Levy
    EmulatedState *card = opaque;
364 585738a6 Alon Levy
    EmulEvent *event, *next;
365 585738a6 Alon Levy
    char dummy;
366 585738a6 Alon Levy
    int len;
367 585738a6 Alon Levy
368 585738a6 Alon Levy
    do {
369 585738a6 Alon Levy
        len = read(card->pipe[0], &dummy, sizeof(dummy));
370 585738a6 Alon Levy
    } while (len == sizeof(dummy));
371 585738a6 Alon Levy
    qemu_mutex_lock(&card->event_list_mutex);
372 585738a6 Alon Levy
    QSIMPLEQ_FOREACH_SAFE(event, &card->event_list, entry, next) {
373 585738a6 Alon Levy
        DPRINTF(card, 2, "event %s\n", emul_event_to_string(event->p.gen.type));
374 585738a6 Alon Levy
        switch (event->p.gen.type) {
375 585738a6 Alon Levy
        case EMUL_RESPONSE_APDU:
376 585738a6 Alon Levy
            ccid_card_send_apdu_to_guest(&card->base, event->p.data.data,
377 585738a6 Alon Levy
                event->p.data.len);
378 585738a6 Alon Levy
            break;
379 585738a6 Alon Levy
        case EMUL_READER_INSERT:
380 585738a6 Alon Levy
            ccid_card_ccid_attach(&card->base);
381 585738a6 Alon Levy
            break;
382 585738a6 Alon Levy
        case EMUL_READER_REMOVE:
383 585738a6 Alon Levy
            ccid_card_ccid_detach(&card->base);
384 585738a6 Alon Levy
            break;
385 585738a6 Alon Levy
        case EMUL_CARD_INSERT:
386 585738a6 Alon Levy
            assert(event->p.data.len <= MAX_ATR_SIZE);
387 585738a6 Alon Levy
            card->atr_length = event->p.data.len;
388 585738a6 Alon Levy
            memcpy(card->atr, event->p.data.data, card->atr_length);
389 585738a6 Alon Levy
            ccid_card_card_inserted(&card->base);
390 585738a6 Alon Levy
            break;
391 585738a6 Alon Levy
        case EMUL_CARD_REMOVE:
392 585738a6 Alon Levy
            ccid_card_card_removed(&card->base);
393 585738a6 Alon Levy
            break;
394 585738a6 Alon Levy
        case EMUL_ERROR:
395 585738a6 Alon Levy
            ccid_card_card_error(&card->base, event->p.error.code);
396 585738a6 Alon Levy
            break;
397 585738a6 Alon Levy
        default:
398 585738a6 Alon Levy
            DPRINTF(card, 2, "unexpected event\n");
399 585738a6 Alon Levy
            break;
400 585738a6 Alon Levy
        }
401 7267c094 Anthony Liguori
        g_free(event);
402 585738a6 Alon Levy
    }
403 585738a6 Alon Levy
    QSIMPLEQ_INIT(&card->event_list);
404 585738a6 Alon Levy
    qemu_mutex_unlock(&card->event_list_mutex);
405 585738a6 Alon Levy
}
406 585738a6 Alon Levy
407 585738a6 Alon Levy
static int init_pipe_signaling(EmulatedState *card)
408 585738a6 Alon Levy
{
409 585738a6 Alon Levy
    if (pipe(card->pipe) < 0) {
410 585738a6 Alon Levy
        DPRINTF(card, 2, "pipe creation failed\n");
411 585738a6 Alon Levy
        return -1;
412 585738a6 Alon Levy
    }
413 585738a6 Alon Levy
    fcntl(card->pipe[0], F_SETFL, O_NONBLOCK);
414 585738a6 Alon Levy
    fcntl(card->pipe[1], F_SETFL, O_NONBLOCK);
415 585738a6 Alon Levy
    fcntl(card->pipe[0], F_SETOWN, getpid());
416 585738a6 Alon Levy
    qemu_set_fd_handler(card->pipe[0], pipe_read, NULL, card);
417 585738a6 Alon Levy
    return 0;
418 585738a6 Alon Levy
}
419 585738a6 Alon Levy
420 585738a6 Alon Levy
#define CERTIFICATES_DEFAULT_DB "/etc/pki/nssdb"
421 585738a6 Alon Levy
#define CERTIFICATES_ARGS_TEMPLATE\
422 585738a6 Alon Levy
    "db=\"%s\" use_hw=no soft=(,Virtual Reader,CAC,,%s,%s,%s)"
423 585738a6 Alon Levy
424 585738a6 Alon Levy
static int wrap_vcard_emul_init(VCardEmulOptions *options)
425 585738a6 Alon Levy
{
426 585738a6 Alon Levy
    static int called;
427 585738a6 Alon Levy
    static int options_was_null;
428 585738a6 Alon Levy
429 585738a6 Alon Levy
    if (called) {
430 585738a6 Alon Levy
        if ((options == NULL) != options_was_null) {
431 585738a6 Alon Levy
            printf("%s: warning: running emulated with certificates"
432 585738a6 Alon Levy
                   " and emulated side by side is not supported\n",
433 585738a6 Alon Levy
                   __func__);
434 585738a6 Alon Levy
            return VCARD_EMUL_FAIL;
435 585738a6 Alon Levy
        }
436 585738a6 Alon Levy
        vcard_emul_replay_insertion_events();
437 585738a6 Alon Levy
        return VCARD_EMUL_OK;
438 585738a6 Alon Levy
    }
439 585738a6 Alon Levy
    options_was_null = (options == NULL);
440 585738a6 Alon Levy
    called = 1;
441 585738a6 Alon Levy
    return vcard_emul_init(options);
442 585738a6 Alon Levy
}
443 585738a6 Alon Levy
444 585738a6 Alon Levy
static int emulated_initialize_vcard_from_certificates(EmulatedState *card)
445 585738a6 Alon Levy
{
446 585738a6 Alon Levy
    char emul_args[200];
447 585738a6 Alon Levy
    VCardEmulOptions *options = NULL;
448 585738a6 Alon Levy
449 585738a6 Alon Levy
    snprintf(emul_args, sizeof(emul_args) - 1, CERTIFICATES_ARGS_TEMPLATE,
450 585738a6 Alon Levy
        card->db ? card->db : CERTIFICATES_DEFAULT_DB,
451 585738a6 Alon Levy
        card->cert1, card->cert2, card->cert3);
452 585738a6 Alon Levy
    options = vcard_emul_options(emul_args);
453 585738a6 Alon Levy
    if (options == NULL) {
454 585738a6 Alon Levy
        printf("%s: warning: not using certificates due to"
455 585738a6 Alon Levy
               " initialization error\n", __func__);
456 585738a6 Alon Levy
    }
457 585738a6 Alon Levy
    return wrap_vcard_emul_init(options);
458 585738a6 Alon Levy
}
459 585738a6 Alon Levy
460 585738a6 Alon Levy
typedef struct EnumTable {
461 585738a6 Alon Levy
    const char *name;
462 585738a6 Alon Levy
    uint32_t value;
463 585738a6 Alon Levy
} EnumTable;
464 585738a6 Alon Levy
465 585738a6 Alon Levy
EnumTable backend_enum_table[] = {
466 585738a6 Alon Levy
    {BACKEND_NSS_EMULATED_NAME, BACKEND_NSS_EMULATED},
467 585738a6 Alon Levy
    {BACKEND_CERTIFICATES_NAME, BACKEND_CERTIFICATES},
468 585738a6 Alon Levy
    {NULL, 0},
469 585738a6 Alon Levy
};
470 585738a6 Alon Levy
471 585738a6 Alon Levy
static uint32_t parse_enumeration(char *str,
472 585738a6 Alon Levy
    EnumTable *table, uint32_t not_found_value)
473 585738a6 Alon Levy
{
474 585738a6 Alon Levy
    uint32_t ret = not_found_value;
475 585738a6 Alon Levy
476 585738a6 Alon Levy
    while (table->name != NULL) {
477 585738a6 Alon Levy
        if (strcmp(table->name, str) == 0) {
478 585738a6 Alon Levy
            ret = table->value;
479 585738a6 Alon Levy
            break;
480 585738a6 Alon Levy
        }
481 585738a6 Alon Levy
        table++;
482 585738a6 Alon Levy
    }
483 585738a6 Alon Levy
    return ret;
484 585738a6 Alon Levy
}
485 585738a6 Alon Levy
486 585738a6 Alon Levy
static int emulated_initfn(CCIDCardState *base)
487 585738a6 Alon Levy
{
488 585738a6 Alon Levy
    EmulatedState *card = DO_UPCAST(EmulatedState, base, base);
489 585738a6 Alon Levy
    VCardEmulError ret;
490 585738a6 Alon Levy
    EnumTable *ptable;
491 585738a6 Alon Levy
492 585738a6 Alon Levy
    QSIMPLEQ_INIT(&card->event_list);
493 585738a6 Alon Levy
    QSIMPLEQ_INIT(&card->guest_apdu_list);
494 585738a6 Alon Levy
    qemu_mutex_init(&card->event_list_mutex);
495 585738a6 Alon Levy
    qemu_mutex_init(&card->vreader_mutex);
496 585738a6 Alon Levy
    qemu_mutex_init(&card->handle_apdu_mutex);
497 585738a6 Alon Levy
    qemu_cond_init(&card->handle_apdu_cond);
498 585738a6 Alon Levy
    card->reader = NULL;
499 585738a6 Alon Levy
    card->quit_apdu_thread = 0;
500 585738a6 Alon Levy
    if (init_pipe_signaling(card) < 0) {
501 585738a6 Alon Levy
        return -1;
502 585738a6 Alon Levy
    }
503 585738a6 Alon Levy
    card->backend = parse_enumeration(card->backend_str, backend_enum_table, 0);
504 585738a6 Alon Levy
    if (card->backend == 0) {
505 585738a6 Alon Levy
        printf("unknown backend, must be one of:\n");
506 585738a6 Alon Levy
        for (ptable = backend_enum_table; ptable->name != NULL; ++ptable) {
507 585738a6 Alon Levy
            printf("%s\n", ptable->name);
508 585738a6 Alon Levy
        }
509 585738a6 Alon Levy
        return -1;
510 585738a6 Alon Levy
    }
511 585738a6 Alon Levy
512 585738a6 Alon Levy
    /* TODO: a passthru backened that works on local machine. third card type?*/
513 585738a6 Alon Levy
    if (card->backend == BACKEND_CERTIFICATES) {
514 585738a6 Alon Levy
        if (card->cert1 != NULL && card->cert2 != NULL && card->cert3 != NULL) {
515 585738a6 Alon Levy
            ret = emulated_initialize_vcard_from_certificates(card);
516 585738a6 Alon Levy
        } else {
517 585738a6 Alon Levy
            printf("%s: you must provide all three certs for"
518 585738a6 Alon Levy
                   " certificates backend\n", EMULATED_DEV_NAME);
519 585738a6 Alon Levy
            return -1;
520 585738a6 Alon Levy
        }
521 585738a6 Alon Levy
    } else {
522 585738a6 Alon Levy
        if (card->backend != BACKEND_NSS_EMULATED) {
523 585738a6 Alon Levy
            printf("%s: bad backend specified. The options are:\n%s (default),"
524 585738a6 Alon Levy
                " %s.\n", EMULATED_DEV_NAME, BACKEND_NSS_EMULATED_NAME,
525 585738a6 Alon Levy
                BACKEND_CERTIFICATES_NAME);
526 585738a6 Alon Levy
            return -1;
527 585738a6 Alon Levy
        }
528 585738a6 Alon Levy
        if (card->cert1 != NULL || card->cert2 != NULL || card->cert3 != NULL) {
529 585738a6 Alon Levy
            printf("%s: unexpected cert parameters to nss emulated backend\n",
530 585738a6 Alon Levy
                   EMULATED_DEV_NAME);
531 585738a6 Alon Levy
            return -1;
532 585738a6 Alon Levy
        }
533 585738a6 Alon Levy
        /* default to mirroring the local hardware readers */
534 585738a6 Alon Levy
        ret = wrap_vcard_emul_init(NULL);
535 585738a6 Alon Levy
    }
536 585738a6 Alon Levy
    if (ret != VCARD_EMUL_OK) {
537 585738a6 Alon Levy
        printf("%s: failed to initialize vcard\n", EMULATED_DEV_NAME);
538 585738a6 Alon Levy
        return -1;
539 585738a6 Alon Levy
    }
540 da5361cc Paolo Bonzini
    qemu_thread_create(&card->event_thread_id, event_thread, card,
541 da5361cc Paolo Bonzini
                       QEMU_THREAD_JOINABLE);
542 da5361cc Paolo Bonzini
    qemu_thread_create(&card->apdu_thread_id, handle_apdu_thread, card,
543 da5361cc Paolo Bonzini
                       QEMU_THREAD_JOINABLE);
544 585738a6 Alon Levy
    return 0;
545 585738a6 Alon Levy
}
546 585738a6 Alon Levy
547 585738a6 Alon Levy
static int emulated_exitfn(CCIDCardState *base)
548 585738a6 Alon Levy
{
549 585738a6 Alon Levy
    EmulatedState *card = DO_UPCAST(EmulatedState, base, base);
550 585738a6 Alon Levy
    VEvent *vevent = vevent_new(VEVENT_LAST, NULL, NULL);
551 585738a6 Alon Levy
552 585738a6 Alon Levy
    vevent_queue_vevent(vevent); /* stop vevent thread */
553 da5361cc Paolo Bonzini
    qemu_thread_join(&card->event_thread_id);
554 da5361cc Paolo Bonzini
555 585738a6 Alon Levy
    card->quit_apdu_thread = 1; /* stop handle_apdu thread */
556 585738a6 Alon Levy
    qemu_cond_signal(&card->handle_apdu_cond);
557 da5361cc Paolo Bonzini
    qemu_thread_join(&card->apdu_thread_id);
558 da5361cc Paolo Bonzini
559 da5361cc Paolo Bonzini
    /* threads exited, can destroy all condvars/mutexes */
560 585738a6 Alon Levy
    qemu_cond_destroy(&card->handle_apdu_cond);
561 585738a6 Alon Levy
    qemu_mutex_destroy(&card->handle_apdu_mutex);
562 585738a6 Alon Levy
    qemu_mutex_destroy(&card->vreader_mutex);
563 585738a6 Alon Levy
    qemu_mutex_destroy(&card->event_list_mutex);
564 585738a6 Alon Levy
    return 0;
565 585738a6 Alon Levy
}
566 585738a6 Alon Levy
567 39bffca2 Anthony Liguori
static Property emulated_card_properties[] = {
568 39bffca2 Anthony Liguori
    DEFINE_PROP_STRING("backend", EmulatedState, backend_str),
569 39bffca2 Anthony Liguori
    DEFINE_PROP_STRING("cert1", EmulatedState, cert1),
570 39bffca2 Anthony Liguori
    DEFINE_PROP_STRING("cert2", EmulatedState, cert2),
571 39bffca2 Anthony Liguori
    DEFINE_PROP_STRING("cert3", EmulatedState, cert3),
572 39bffca2 Anthony Liguori
    DEFINE_PROP_STRING("db", EmulatedState, db),
573 39bffca2 Anthony Liguori
    DEFINE_PROP_UINT8("debug", EmulatedState, debug, 0),
574 39bffca2 Anthony Liguori
    DEFINE_PROP_END_OF_LIST(),
575 39bffca2 Anthony Liguori
};
576 39bffca2 Anthony Liguori
577 ba7c0520 Anthony Liguori
static void emulated_class_initfn(ObjectClass *klass, void *data)
578 ba7c0520 Anthony Liguori
{
579 39bffca2 Anthony Liguori
    DeviceClass *dc = DEVICE_CLASS(klass);
580 ba7c0520 Anthony Liguori
    CCIDCardClass *cc = CCID_CARD_CLASS(klass);
581 ba7c0520 Anthony Liguori
582 ba7c0520 Anthony Liguori
    cc->initfn = emulated_initfn;
583 ba7c0520 Anthony Liguori
    cc->exitfn = emulated_exitfn;
584 ba7c0520 Anthony Liguori
    cc->get_atr = emulated_get_atr;
585 ba7c0520 Anthony Liguori
    cc->apdu_from_guest = emulated_apdu_from_guest;
586 39bffca2 Anthony Liguori
    dc->desc = "emulated smartcard";
587 39bffca2 Anthony Liguori
    dc->props = emulated_card_properties;
588 ba7c0520 Anthony Liguori
}
589 ba7c0520 Anthony Liguori
590 39bffca2 Anthony Liguori
static TypeInfo emulated_card_info = {
591 39bffca2 Anthony Liguori
    .name          = EMULATED_DEV_NAME,
592 39bffca2 Anthony Liguori
    .parent        = TYPE_CCID_CARD,
593 39bffca2 Anthony Liguori
    .instance_size = sizeof(EmulatedState),
594 39bffca2 Anthony Liguori
    .class_init    = emulated_class_initfn,
595 585738a6 Alon Levy
};
596 585738a6 Alon Levy
597 83f7d43a Andreas Färber
static void ccid_card_emulated_register_types(void)
598 585738a6 Alon Levy
{
599 39bffca2 Anthony Liguori
    type_register_static(&emulated_card_info);
600 585738a6 Alon Levy
}
601 585738a6 Alon Levy
602 83f7d43a Andreas Färber
type_init(ccid_card_emulated_register_types)