summaryrefslogtreecommitdiff
path: root/hw/usb/u2f.c
blob: 56001249a449a480eed7cf9c21108c1aea432cd7 (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
/*
 * U2F USB 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 "qapi/error.h"
#include "hw/usb.h"
#include "hw/usb/hid.h"
#include "migration/vmstate.h"
#include "desc.h"

#include "u2f.h"

/* U2F key Vendor / Product */
#define U2F_KEY_VENDOR_NUM     0x46f4 /* CRC16() of "QEMU" */
#define U2F_KEY_PRODUCT_NUM    0x0005

enum {
    STR_MANUFACTURER = 1,
    STR_PRODUCT,
    STR_SERIALNUMBER,
    STR_CONFIG,
    STR_INTERFACE
};

static const USBDescStrings desc_strings = {
    [STR_MANUFACTURER]     = "QEMU",
    [STR_PRODUCT]          = "U2F USB key",
    [STR_SERIALNUMBER]     = "0",
    [STR_CONFIG]           = "U2F key config",
    [STR_INTERFACE]        = "U2F key interface"
};

static const USBDescIface desc_iface_u2f_key = {
    .bInterfaceNumber              = 0,
    .bNumEndpoints                 = 2,
    .bInterfaceClass               = USB_CLASS_HID,
    .bInterfaceSubClass            = 0x0,
    .bInterfaceProtocol            = 0x0,
    .ndesc                         = 1,
    .descs = (USBDescOther[]) {
        {
            /* HID descriptor */
            .data = (uint8_t[]) {
                0x09,          /*  u8  bLength */
                USB_DT_HID,    /*  u8  bDescriptorType */
                0x10, 0x01,    /*  u16 HID_class */
                0x00,          /*  u8  country_code */
                0x01,          /*  u8  num_descriptors */
                USB_DT_REPORT, /*  u8  type: Report */
                0x22, 0,       /*  u16 len */
            },
        },
    },
    .eps = (USBDescEndpoint[]) {
        {
            .bEndpointAddress      = USB_DIR_IN | 0x01,
            .bmAttributes          = USB_ENDPOINT_XFER_INT,
            .wMaxPacketSize        = U2FHID_PACKET_SIZE,
            .bInterval             = 0x05,
        }, {
            .bEndpointAddress      = USB_DIR_OUT | 0x01,
            .bmAttributes          = USB_ENDPOINT_XFER_INT,
            .wMaxPacketSize        = U2FHID_PACKET_SIZE,
            .bInterval             = 0x05,
        },
    },

};

static const USBDescDevice desc_device_u2f_key = {
    .bcdUSB                        = 0x0100,
    .bMaxPacketSize0               = U2FHID_PACKET_SIZE,
    .bNumConfigurations            = 1,
    .confs = (USBDescConfig[]) {
        {
            .bNumInterfaces        = 1,
            .bConfigurationValue   = 1,
            .iConfiguration        = STR_CONFIG,
            .bmAttributes          = USB_CFG_ATT_ONE,
            .bMaxPower             = 15,
            .nif = 1,
            .ifs = &desc_iface_u2f_key,
        },
    },
};

static const USBDesc desc_u2f_key = {
    .id = {
        .idVendor          = U2F_KEY_VENDOR_NUM,
        .idProduct         = U2F_KEY_PRODUCT_NUM,
        .bcdDevice         = 0,
        .iManufacturer     = STR_MANUFACTURER,
        .iProduct          = STR_PRODUCT,
        .iSerialNumber     = STR_SERIALNUMBER,
    },
    .full = &desc_device_u2f_key,
    .str  = desc_strings,
};

static const uint8_t u2f_key_hid_report_desc[] = {
    0x06, 0xd0, 0xf1, /* Usage Page (FIDO) */
    0x09, 0x01,       /* Usage (FIDO) */
    0xa1, 0x01,       /* Collection (HID Application) */
    0x09, 0x20,       /*    Usage (FIDO data in) */
    0x15, 0x00,       /*        Logical Minimum (0) */
    0x26, 0xFF, 0x00, /*        Logical Maximum (0xff) */
    0x75, 0x08,       /*        Report Size (8) */
    0x95, 0x40,       /*        Report Count (0x40) */
    0x81, 0x02,       /*        Input (Data, Variable, Absolute) */
    0x09, 0x21,       /*    Usage (FIDO data out) */
    0x15, 0x00,       /*        Logical Minimum (0) */
    0x26, 0xFF, 0x00, /*        Logical Maximum  (0xFF) */
    0x75, 0x08,       /*        Report Size (8) */
    0x95, 0x40,       /*        Report Count (0x40) */
    0x91, 0x02,       /*        Output (Data, Variable, Absolute) */
    0xC0              /* End Collection */
};

static void u2f_key_reset(U2FKeyState *key)
{
    key->pending_in_start = 0;
    key->pending_in_end = 0;
    key->pending_in_num = 0;
}

static void u2f_key_handle_reset(USBDevice *dev)
{
    U2FKeyState *key = U2F_KEY(dev);

    u2f_key_reset(key);
}

static void u2f_key_handle_control(USBDevice *dev, USBPacket *p,
               int request, int value, int index, int length, uint8_t *data)
{
    U2FKeyState *key = U2F_KEY(dev);
    int ret;

    ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
    if (ret >= 0) {
        return;
    }

    switch (request) {
    case InterfaceRequest | USB_REQ_GET_DESCRIPTOR:
        switch (value >> 8) {
        case 0x22:
            memcpy(data, u2f_key_hid_report_desc,
                   sizeof(u2f_key_hid_report_desc));
            p->actual_length = sizeof(u2f_key_hid_report_desc);
            break;
        default:
            goto fail;
        }
        break;
    case HID_GET_IDLE:
        data[0] = key->idle;
        p->actual_length = 1;
        break;
    case HID_SET_IDLE:
        key->idle = (uint8_t)(value >> 8);
        break;
    default:
    fail:
        p->status = USB_RET_STALL;
        break;
    }

}

static void u2f_key_recv_from_guest(U2FKeyState *key, USBPacket *p)
{
    U2FKeyClass *kc = U2F_KEY_GET_CLASS(key);
    uint8_t packet[U2FHID_PACKET_SIZE];

    if (kc->recv_from_guest == NULL || p->iov.size != U2FHID_PACKET_SIZE) {
        return;
    }

    usb_packet_copy(p, packet, p->iov.size);
    kc->recv_from_guest(key, packet);
}

static void u2f_pending_in_add(U2FKeyState *key,
                               const uint8_t packet[U2FHID_PACKET_SIZE])
{
    uint8_t index;

    if (key->pending_in_num >= U2FHID_PENDING_IN_NUM) {
        return;
    }

    index = key->pending_in_end;
    key->pending_in_end = (index + 1) % U2FHID_PENDING_IN_NUM;
    ++key->pending_in_num;

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

static uint8_t *u2f_pending_in_get(U2FKeyState *key)
{
    uint8_t index;

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

    index = key->pending_in_start;
    key->pending_in_start = (index + 1) % U2FHID_PENDING_IN_NUM;
    --key->pending_in_num;

    return key->pending_in[index];
}

static void u2f_key_handle_data(USBDevice *dev, USBPacket *p)
{
    U2FKeyState *key = U2F_KEY(dev);
    uint8_t *packet_in;

    /* Endpoint number check */
    if (p->ep->nr != 1) {
        p->status = USB_RET_STALL;
        return;
    }

    switch (p->pid) {
    case USB_TOKEN_OUT:
        u2f_key_recv_from_guest(key, p);
        break;
    case USB_TOKEN_IN:
        packet_in = u2f_pending_in_get(key);
        if (packet_in == NULL) {
            p->status = USB_RET_NAK;
            return;
        }
        usb_packet_copy(p, packet_in, U2FHID_PACKET_SIZE);
        break;
    default:
        p->status = USB_RET_STALL;
        break;
    }
}

void u2f_send_to_guest(U2FKeyState *key,
                       const uint8_t packet[U2FHID_PACKET_SIZE])
{
    u2f_pending_in_add(key, packet);
    usb_wakeup(key->ep, 0);
}

static void u2f_key_unrealize(USBDevice *dev)
{
    U2FKeyState *key = U2F_KEY(dev);
    U2FKeyClass *kc = U2F_KEY_GET_CLASS(key);

    if (kc->unrealize != NULL) {
        kc->unrealize(key);
    }
}

static void u2f_key_realize(USBDevice *dev, Error **errp)
{
    U2FKeyState *key = U2F_KEY(dev);
    U2FKeyClass *kc = U2F_KEY_GET_CLASS(key);
    Error *local_err = NULL;

    usb_desc_create_serial(dev);
    usb_desc_init(dev);
    u2f_key_reset(key);

    if (kc->realize != NULL) {
        kc->realize(key, &local_err);
        if (local_err != NULL) {
            error_propagate(errp, local_err);
            return;
        }
    }
    key->ep = usb_ep_get(dev, USB_TOKEN_IN, 1);
}

const VMStateDescription vmstate_u2f_key = {
    .name = "u2f-key",
    .version_id = 1,
    .minimum_version_id = 1,
    .fields = (VMStateField[]) {
        VMSTATE_USB_DEVICE(dev, U2FKeyState),
        VMSTATE_UINT8(idle, U2FKeyState),
        VMSTATE_UINT8_2DARRAY(pending_in, U2FKeyState,
            U2FHID_PENDING_IN_NUM, U2FHID_PACKET_SIZE),
        VMSTATE_UINT8(pending_in_start, U2FKeyState),
        VMSTATE_UINT8(pending_in_end, U2FKeyState),
        VMSTATE_UINT8(pending_in_num, U2FKeyState),
        VMSTATE_END_OF_LIST()
    }
};

static void u2f_key_class_init(ObjectClass *klass, void *data)
{
    DeviceClass *dc = DEVICE_CLASS(klass);
    USBDeviceClass *uc = USB_DEVICE_CLASS(klass);

    uc->product_desc   = "QEMU U2F USB key";
    uc->usb_desc       = &desc_u2f_key;
    uc->handle_reset   = u2f_key_handle_reset;
    uc->handle_control = u2f_key_handle_control;
    uc->handle_data    = u2f_key_handle_data;
    uc->handle_attach  = usb_desc_attach;
    uc->realize        = u2f_key_realize;
    uc->unrealize      = u2f_key_unrealize;
    dc->desc           = "QEMU U2F key";
    dc->vmsd           = &vmstate_u2f_key;
}

static const TypeInfo u2f_key_info = {
    .name          = TYPE_U2F_KEY,
    .parent        = TYPE_USB_DEVICE,
    .instance_size = sizeof(U2FKeyState),
    .abstract      = true,
    .class_size    = sizeof(U2FKeyClass),
    .class_init    = u2f_key_class_init,
};

static void u2f_key_register_types(void)
{
    type_register_static(&u2f_key_info);
}

type_init(u2f_key_register_types)