From nobody Sun Nov 16 06:00:26 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1598602376; cv=none; d=zohomail.com; s=zohoarc; b=YDUotyZNvkJKpK/SeO5Gx8QXzLYu2St59ZYwCABQWThwoaDAERj0zbQsVEMf8JpbsmOjgHeTGb6BcqQ9gzhhfjFftB0XNiYutJEUY9srWHdulCW8qKYCMwJQFqBl1EtEK/Icz7i2GSADy5cdK8S+Bm68ILg3QYVewOPQtHbPF5c= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1598602376; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=dTE+G237rTzPCXBR1JEpjCjPVXH6rj6XiPtZ2JTJp2c=; b=I1TV8jC71l7RLszc3Hh+MtjhOZPSn736u48bGuSZXTSedpcRmC9qM5BmQwJY9CS6UY2X6n6+WTIPD50LZvV2ulY0Uvadq4BSwQVMGKRknhG+wmY99cDdmb6NHSkFwnWpmeoxyb1VzeY4VIhEWA5IR3DGslt101iVczTisF59A9Q= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1598602376471334.8435229979565; Fri, 28 Aug 2020 01:12:56 -0700 (PDT) Received: from localhost ([::1]:46260 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kBZVG-0000D1-69 for importer@patchew.org; Fri, 28 Aug 2020 04:12:54 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57894) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kBZRZ-0000iy-0z for qemu-devel@nongnu.org; Fri, 28 Aug 2020 04:09:05 -0400 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:35514) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1kBZRS-0003RD-3G for qemu-devel@nongnu.org; Fri, 28 Aug 2020 04:09:04 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-324-yaBZnF5rOsqFfSCq2WaZIg-1; Fri, 28 Aug 2020 04:08:53 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 260C8425D7; Fri, 28 Aug 2020 08:08:52 +0000 (UTC) Received: from sirius.home.kraxel.org (ovpn-112-54.ams2.redhat.com [10.36.112.54]) by smtp.corp.redhat.com (Postfix) with ESMTP id 312257D66C; Fri, 28 Aug 2020 08:08:50 +0000 (UTC) Received: by sirius.home.kraxel.org (Postfix, from userid 1000) id 3098831E60; Fri, 28 Aug 2020 10:08:46 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1598602135; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=dTE+G237rTzPCXBR1JEpjCjPVXH6rj6XiPtZ2JTJp2c=; b=d4+YGV7aWxpPR4O+SQAKmdDJ64ngprWQxuT6GdNwH9TfQMhQ3c7/KGtd4urEw/7RIL8eRs aQdi1BkzysCcg56RBXBDKB7nxUAIqyUrdFlu4Ksj9SU7tH09IN6W3Jy1gLTfYmVHldF5mD avGDNWWKb6Q1f1zHtFLum93jLdD5oHc= X-MC-Unique: yaBZnF5rOsqFfSCq2WaZIg-1 From: Gerd Hoffmann To: qemu-devel@nongnu.org Subject: [PULL 08/18] hw/usb: Add U2F key base class implementation Date: Fri, 28 Aug 2020 10:08:35 +0200 Message-Id: <20200828080845.28287-9-kraxel@redhat.com> In-Reply-To: <20200828080845.28287-1-kraxel@redhat.com> References: <20200828080845.28287-1-kraxel@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=kraxel@redhat.com X-Mimecast-Spam-Score: 0.002 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=63.128.21.124; envelope-from=kraxel@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/08/28 04:08:55 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -30 X-Spam_score: -3.1 X-Spam_bar: --- X-Spam_report: (-3.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.959, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , =?UTF-8?q?C=C3=A9sar=20Belley?= , Gerd Hoffmann , Cleber Rosa , Paolo Bonzini , Eduardo Habkost Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) From: C=C3=A9sar Belley This patch adds the U2F key base class implementation. The U2F key base mainly takes care of the HID interfacing with guest. On the one hand, it retrieves the guest U2FHID packets and transmits them to the variant associated according to the mode: pass-through or emulated. On the other hand, it provides the public API used by its variants to send U2FHID packets to the guest. Signed-off-by: C=C3=A9sar Belley Message-id: 20200826114209.28821-5-cesar.belley@lse.epita.fr Signed-off-by: Gerd Hoffmann --- hw/usb/u2f.c | 352 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 352 insertions(+) create mode 100644 hw/usb/u2f.c diff --git a/hw/usb/u2f.c b/hw/usb/u2f.c new file mode 100644 index 000000000000..bc09191f063e --- /dev/null +++ b/hw/usb/u2f.c @@ -0,0 +1,352 @@ +/* + * U2F USB device. + * + * Copyright (c) 2020 C=C3=A9sar Belley + * Written by C=C3=A9sar Belley + * + * Permission is hereby granted, free of charge, to any person obtaining a= copy + * of this software and associated documentation files (the "Software"), t= o deal + * in the Software without restriction, including without limitation the r= ights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or se= ll + * 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 OT= HER + * 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 =3D 1, + STR_PRODUCT, + STR_SERIALNUMBER, + STR_CONFIG, + STR_INTERFACE +}; + +static const USBDescStrings desc_strings =3D { + [STR_MANUFACTURER] =3D "QEMU", + [STR_PRODUCT] =3D "U2F USB key", + [STR_SERIALNUMBER] =3D "0", + [STR_CONFIG] =3D "U2F key config", + [STR_INTERFACE] =3D "U2F key interface" +}; + +static const USBDescIface desc_iface_u2f_key =3D { + .bInterfaceNumber =3D 0, + .bNumEndpoints =3D 2, + .bInterfaceClass =3D USB_CLASS_HID, + .bInterfaceSubClass =3D 0x0, + .bInterfaceProtocol =3D 0x0, + .ndesc =3D 1, + .descs =3D (USBDescOther[]) { + { + /* HID descriptor */ + .data =3D (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 =3D (USBDescEndpoint[]) { + { + .bEndpointAddress =3D USB_DIR_IN | 0x01, + .bmAttributes =3D USB_ENDPOINT_XFER_INT, + .wMaxPacketSize =3D U2FHID_PACKET_SIZE, + .bInterval =3D 0x05, + }, { + .bEndpointAddress =3D USB_DIR_OUT | 0x01, + .bmAttributes =3D USB_ENDPOINT_XFER_INT, + .wMaxPacketSize =3D U2FHID_PACKET_SIZE, + .bInterval =3D 0x05, + }, + }, + +}; + +static const USBDescDevice desc_device_u2f_key =3D { + .bcdUSB =3D 0x0100, + .bMaxPacketSize0 =3D U2FHID_PACKET_SIZE, + .bNumConfigurations =3D 1, + .confs =3D (USBDescConfig[]) { + { + .bNumInterfaces =3D 1, + .bConfigurationValue =3D 1, + .iConfiguration =3D STR_CONFIG, + .bmAttributes =3D USB_CFG_ATT_ONE, + .bMaxPower =3D 15, + .nif =3D 1, + .ifs =3D &desc_iface_u2f_key, + }, + }, +}; + +static const USBDesc desc_u2f_key =3D { + .id =3D { + .idVendor =3D U2F_KEY_VENDOR_NUM, + .idProduct =3D U2F_KEY_PRODUCT_NUM, + .bcdDevice =3D 0, + .iManufacturer =3D STR_MANUFACTURER, + .iProduct =3D STR_PRODUCT, + .iSerialNumber =3D STR_SERIALNUMBER, + }, + .full =3D &desc_device_u2f_key, + .str =3D desc_strings, +}; + +static const uint8_t u2f_key_hid_report_desc[] =3D { + 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 =3D 0; + key->pending_in_end =3D 0; + key->pending_in_num =3D 0; +} + +static void u2f_key_handle_reset(USBDevice *dev) +{ + U2FKeyState *key =3D 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 *dat= a) +{ + U2FKeyState *key =3D U2F_KEY(dev); + int ret; + + ret =3D usb_desc_handle_control(dev, p, request, value, index, length,= data); + if (ret >=3D 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 =3D sizeof(u2f_key_hid_report_desc); + break; + default: + goto fail; + } + break; + case HID_GET_IDLE: + data[0] =3D key->idle; + p->actual_length =3D 1; + break; + case HID_SET_IDLE: + key->idle =3D (uint8_t)(value >> 8); + break; + default: + fail: + p->status =3D USB_RET_STALL; + break; + } + +} + +static void u2f_key_recv_from_guest(U2FKeyState *key, USBPacket *p) +{ + U2FKeyClass *kc =3D U2F_KEY_GET_CLASS(key); + uint8_t packet[U2FHID_PACKET_SIZE]; + + if (kc->recv_from_guest =3D=3D NULL || p->iov.size !=3D 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 >=3D U2FHID_PENDING_IN_NUM) { + return; + } + + index =3D key->pending_in_end; + key->pending_in_end =3D (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 =3D=3D 0) { + return NULL; + } + + index =3D key->pending_in_start; + key->pending_in_start =3D (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 =3D U2F_KEY(dev); + uint8_t *packet_in; + + /* Endpoint number check */ + if (p->ep->nr !=3D 1) { + p->status =3D 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 =3D u2f_pending_in_get(key); + if (packet_in =3D=3D NULL) { + p->status =3D USB_RET_NAK; + return; + } + usb_packet_copy(p, packet_in, U2FHID_PACKET_SIZE); + break; + default: + p->status =3D 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 =3D U2F_KEY(dev); + U2FKeyClass *kc =3D U2F_KEY_GET_CLASS(key); + + if (kc->unrealize !=3D NULL) { + kc->unrealize(key); + } +} + +static void u2f_key_realize(USBDevice *dev, Error **errp) +{ + U2FKeyState *key =3D U2F_KEY(dev); + U2FKeyClass *kc =3D U2F_KEY_GET_CLASS(key); + Error *local_err =3D NULL; + + usb_desc_create_serial(dev); + usb_desc_init(dev); + u2f_key_reset(key); + + if (kc->realize !=3D NULL) { + kc->realize(key, &local_err); + if (local_err !=3D NULL) { + error_propagate(errp, local_err); + return; + } + } + key->ep =3D usb_ep_get(dev, USB_TOKEN_IN, 1); +} + +const VMStateDescription vmstate_u2f_key =3D { + .name =3D "u2f-key", + .version_id =3D 1, + .minimum_version_id =3D 1, + .fields =3D (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 =3D DEVICE_CLASS(klass); + USBDeviceClass *uc =3D USB_DEVICE_CLASS(klass); + + uc->product_desc =3D "QEMU U2F USB key"; + uc->usb_desc =3D &desc_u2f_key; + uc->handle_reset =3D u2f_key_handle_reset; + uc->handle_control =3D u2f_key_handle_control; + uc->handle_data =3D u2f_key_handle_data; + uc->handle_attach =3D usb_desc_attach; + uc->realize =3D u2f_key_realize; + uc->unrealize =3D u2f_key_unrealize; + dc->desc =3D "QEMU U2F key"; + dc->vmsd =3D &vmstate_u2f_key; +} + +static const TypeInfo u2f_key_info =3D { + .name =3D TYPE_U2F_KEY, + .parent =3D TYPE_USB_DEVICE, + .instance_size =3D sizeof(U2FKeyState), + .abstract =3D true, + .class_size =3D sizeof(U2FKeyClass), + .class_init =3D u2f_key_class_init, +}; + +static void u2f_key_register_types(void) +{ + type_register_static(&u2f_key_info); + usb_legacy_register(TYPE_U2F_KEY, "u2f-key", NULL); +} + +type_init(u2f_key_register_types) --=20 2.27.0