summaryrefslogtreecommitdiff
path: root/hw/usb/u2f-emulated.c
blob: 9151feb63d449027b47ee6c55708fdc2a7fa9f7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
 * U2F USB Emulated device.
 *
 * Copyright (c) 2020 César Belley <cesar.belley@lse.epita.fr>
 * Written by César Belley <cesar.belley@lse.epita.fr>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include "qemu/osdep.h"
#include "qemu/module.h"
#include "qemu/thread.h"
#include "qemu/main-loop.h"
#include "qapi/error.h"
#include "hw/usb.h"
#include "hw/qdev-properties.h"

#include <u2f-emu/u2f-emu.h>

#include "u2f.h"

/* Counter which sync with a file */
struct synced_counter {
    /* Emulated device counter */
    struct u2f_emu_vdev_counter vdev_counter;

    /* Private attributes */
    uint32_t value;
    FILE *fp;
};

static void counter_increment(struct u2f_emu_vdev_counter *vdev_counter)
{
    struct synced_counter *counter = (struct synced_counter *)vdev_counter;
    ++counter->value;

    /* Write back */
    if (fseek(counter->fp, 0, SEEK_SET) == -1) {
        return;
    }
    fprintf(counter->fp, "%u\n", counter->value);
}

static uint32_t counter_read(struct u2f_emu_vdev_counter *vdev_counter)
{
    struct synced_counter *counter = (struct synced_counter *)vdev_counter;
    return counter->value;
}

typedef struct U2FEmulatedState U2FEmulatedState;

#define PENDING_OUT_NUM 32

struct U2FEmulatedState {
    U2FKeyState base;

    /* U2F virtual emulated device */
    u2f_emu_vdev *vdev;
    QemuMutex vdev_mutex;

    /* Properties */
    char *dir;
    char *cert;
    char *privkey;
    char *entropy;
    char *counter;
    struct synced_counter synced_counter;

    /* Pending packets received from the guest */
    uint8_t pending_out[PENDING_OUT_NUM][U2FHID_PACKET_SIZE];
    uint8_t pending_out_start;
    uint8_t pending_out_end;
    uint8_t pending_out_num;
    QemuMutex pending_out_mutex;

    /* Emulation thread and sync */
    QemuCond key_cond;
    QemuMutex key_mutex;
    QemuThread key_thread;
    bool stop_thread;
    EventNotifier notifier;
};

#define TYPE_U2F_EMULATED "u2f-emulated"
#define EMULATED_U2F_KEY(obj) \
    OBJECT_CHECK(U2FEmulatedState, (obj), TYPE_U2F_EMULATED)

static void u2f_emulated_reset(U2FEmulatedState *key)
{
    key->pending_out_start = 0;
    key->pending_out_end = 0;
    key->pending_out_num = 0;
}

static void u2f_pending_out_add(U2FEmulatedState *key,
                                const uint8_t packet[U2FHID_PACKET_SIZE])
{
    int index;

    if (key->pending_out_num >= PENDING_OUT_NUM) {
        return;
    }

    index = key->pending_out_end;
    key->pending_out_end = (index + 1) % PENDING_OUT_NUM;
    ++key->pending_out_num;

    memcpy(&key->pending_out[index], packet, U2FHID_PACKET_SIZE);
}

static uint8_t *u2f_pending_out_get(U2FEmulatedState *key)
{
    int index;

    if (key->pending_out_num == 0) {
        return NULL;
    }

    index  = key->pending_out_start;
    key->pending_out_start = (index + 1) % PENDING_OUT_NUM;
    --key->pending_out_num;

    return key->pending_out[index];
}

static void u2f_emulated_recv_from_guest(U2FKeyState *base,
                                    const uint8_t packet[U2FHID_PACKET_SIZE])
{
    U2FEmulatedState *key = EMULATED_U2F_KEY(base);

    qemu_mutex_lock(&key->pending_out_mutex);
    u2f_pending_out_add(key, packet);
    qemu_mutex_unlock(&key->pending_out_mutex);

    qemu_mutex_lock(&key->key_mutex);
    qemu_cond_signal(&key->key_cond);
    qemu_mutex_unlock(&key->key_mutex);
}

static void *u2f_emulated_thread(void* arg)
{
    U2FEmulatedState *key = arg;
    uint8_t packet[U2FHID_PACKET_SIZE];
    uint8_t *packet_out = NULL;


    while (true) {
        /* Wait signal */
        qemu_mutex_lock(&key->key_mutex);
        qemu_cond_wait(&key->key_cond, &key->key_mutex);
        qemu_mutex_unlock(&key->key_mutex);

        /* Exit thread check */
        if (key->stop_thread) {
            key->stop_thread = false;
            break;
        }

        qemu_mutex_lock(&key->pending_out_mutex);
        packet_out = u2f_pending_out_get(key);
        if (packet_out == NULL) {
            qemu_mutex_unlock(&key->pending_out_mutex);
            continue;
        }
        memcpy(packet, packet_out, U2FHID_PACKET_SIZE);
        qemu_mutex_unlock(&key->pending_out_mutex);

        qemu_mutex_lock(&key->vdev_mutex);
        u2f_emu_vdev_send(key->vdev, U2F_EMU_USB, packet,
                          U2FHID_PACKET_SIZE);

        /* Notify response */
        if (u2f_emu_vdev_has_response(key->vdev, U2F_EMU_USB)) {
            event_notifier_set(&key->notifier);
        }
        qemu_mutex_unlock(&key->vdev_mutex);
    }
    return NULL;
}

static ssize_t u2f_emulated_read(const char *path, char *buffer,
                                 size_t buffer_len)
{
    int fd;
    ssize_t ret;

    fd = qemu_open_old(path, O_RDONLY);
    if (fd < 0) {
        return -1;
    }

    ret = read(fd, buffer, buffer_len);
    close(fd);

    return ret;
}

static bool u2f_emulated_setup_counter(const char *path,
                                       struct synced_counter *counter)
{
    int fd, ret;
    FILE *fp;

    fd = qemu_open_old(path, O_RDWR);
    if (fd < 0) {
        return false;
    }
    fp = fdopen(fd, "r+");
    if (fp == NULL) {
        close(fd);
        return false;
    }
    ret = fscanf(fp, "%u", &counter->value);
    if (ret == EOF) {
        fclose(fp);
        return false;
    }
    counter->fp = fp;
    counter->vdev_counter.counter_increment = counter_increment;
    counter->vdev_counter.counter_read = counter_read;

    return true;
}

static u2f_emu_rc u2f_emulated_setup_vdev_manualy(U2FEmulatedState *key)
{
    ssize_t ret;
    char cert_pem[4096], privkey_pem[2048];
    struct u2f_emu_vdev_setup setup_info;

    /* Certificate */
    ret = u2f_emulated_read(key->cert, cert_pem, sizeof(cert_pem));
    if (ret < 0) {
        return -1;
    }

    /* Private key */
    ret = u2f_emulated_read(key->privkey, privkey_pem, sizeof(privkey_pem));
    if (ret < 0) {
        return -1;
    }

    /* Entropy */
    ret = u2f_emulated_read(key->entropy, (char *)&setup_info.entropy,
                            sizeof(setup_info.entropy));
    if (ret < 0) {
        return -1;
    }

    /* Counter */
    if (!u2f_emulated_setup_counter(key->counter, &key->synced_counter)) {
        return -1;
    }

    /* Setup */
    setup_info.certificate = cert_pem;
    setup_info.private_key = privkey_pem;
    setup_info.counter = (struct u2f_emu_vdev_counter *)&key->synced_counter;

    return u2f_emu_vdev_new(&key->vdev, &setup_info);
}

static void u2f_emulated_event_handler(EventNotifier *notifier)
{
    U2FEmulatedState *key = container_of(notifier, U2FEmulatedState, notifier);
    size_t packet_size;
    uint8_t *packet_in = NULL;

    event_notifier_test_and_clear(&key->notifier);
    qemu_mutex_lock(&key->vdev_mutex);
    while (u2f_emu_vdev_has_response(key->vdev, U2F_EMU_USB)) {
        packet_size = u2f_emu_vdev_get_response(key->vdev, U2F_EMU_USB,
                                                &packet_in);
        if (packet_size == U2FHID_PACKET_SIZE) {
            u2f_send_to_guest(&key->base, packet_in);
        }
        u2f_emu_vdev_free_response(packet_in);
    }
    qemu_mutex_unlock(&key->vdev_mutex);
}

static void u2f_emulated_realize(U2FKeyState *base, Error **errp)
{
    U2FEmulatedState *key = EMULATED_U2F_KEY(base);
    u2f_emu_rc rc;

    if (key->cert != NULL || key->privkey != NULL || key->entropy != NULL
        || key->counter != NULL) {
        if (key->cert != NULL && key->privkey != NULL
            && key->entropy != NULL && key->counter != NULL) {
            rc = u2f_emulated_setup_vdev_manualy(key);
        } else {
            error_setg(errp, "%s: cert, priv, entropy and counter "
                       "parameters must be provided to manualy configure "
                       "the emulated device", TYPE_U2F_EMULATED);
            return;
        }
    } else if (key->dir != NULL) {
        rc = u2f_emu_vdev_new_from_dir(&key->vdev, key->dir);
    } else {
        rc = u2f_emu_vdev_new_ephemeral(&key->vdev);
    }

    if (rc != U2F_EMU_OK) {
        error_setg(errp, "%s: Failed to setup the key", TYPE_U2F_EMULATED);
        return;
    }

    if (event_notifier_init(&key->notifier, false) < 0) {
        error_setg(errp, "%s: Failed to initialize notifier",
                   TYPE_U2F_EMULATED);
        return;
    }
    /* Notifier */
    event_notifier_set_handler(&key->notifier, u2f_emulated_event_handler);

    /* Synchronization */
    qemu_cond_init(&key->key_cond);
    qemu_mutex_init(&key->vdev_mutex);
    qemu_mutex_init(&key->pending_out_mutex);
    qemu_mutex_init(&key->key_mutex);
    u2f_emulated_reset(key);

    /* Thread */
    key->stop_thread = false;
    qemu_thread_create(&key->key_thread, "u2f-key", u2f_emulated_thread,
                       key, QEMU_THREAD_JOINABLE);
}

static void u2f_emulated_unrealize(U2FKeyState *base)
{
    U2FEmulatedState *key = EMULATED_U2F_KEY(base);

    /* Thread */
    key->stop_thread = true;
    qemu_cond_signal(&key->key_cond);
    qemu_thread_join(&key->key_thread);

    /* Notifier */
    event_notifier_set_handler(&key->notifier, NULL);
    event_notifier_cleanup(&key->notifier);

    /* Synchronization */
    qemu_cond_destroy(&key->key_cond);
    qemu_mutex_destroy(&key->vdev_mutex);
    qemu_mutex_destroy(&key->key_mutex);
    qemu_mutex_destroy(&key->pending_out_mutex);

    /* Vdev */
    u2f_emu_vdev_free(key->vdev);
    if (key->synced_counter.fp != NULL) {
        fclose(key->synced_counter.fp);
    }
}

static Property u2f_emulated_properties[] = {
    DEFINE_PROP_STRING("dir", U2FEmulatedState, dir),
    DEFINE_PROP_STRING("cert", U2FEmulatedState, cert),
    DEFINE_PROP_STRING("privkey", U2FEmulatedState, privkey),
    DEFINE_PROP_STRING("entropy", U2FEmulatedState, entropy),
    DEFINE_PROP_STRING("counter", U2FEmulatedState, counter),
    DEFINE_PROP_END_OF_LIST(),
};

static void u2f_emulated_class_init(ObjectClass *klass, void *data)
{
    DeviceClass *dc = DEVICE_CLASS(klass);
    U2FKeyClass *kc = U2F_KEY_CLASS(klass);

    kc->realize = u2f_emulated_realize;
    kc->unrealize = u2f_emulated_unrealize;
    kc->recv_from_guest = u2f_emulated_recv_from_guest;
    dc->desc = "QEMU U2F emulated key";
    device_class_set_props(dc, u2f_emulated_properties);
}

static const TypeInfo u2f_key_emulated_info = {
    .name = TYPE_U2F_EMULATED,
    .parent = TYPE_U2F_KEY,
    .instance_size = sizeof(U2FEmulatedState),
    .class_init = u2f_emulated_class_init
};

static void u2f_key_emulated_register_types(void)
{
    type_register_static(&u2f_key_emulated_info);
}

type_init(u2f_key_emulated_register_types)