From nobody Sun May 19 01:43:01 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 ARC-Seal: i=1; a=rsa-sha256; t=1598269864; cv=none; d=zohomail.com; s=zohoarc; b=F+TpLjpvOmoWMdTo0fab9kPDv6LkM/92+yMJ/BoE4IBwRK0hsl1j2yiTE3bHNtYeOGDg8CPO5LWJN1FFEE7bQEmZKSTn1bf+IFMcknJi3xhNyehLvkd6uXu5IC3+g6Iabvnl+hhLX3mLe7Hd5xPhQHL2Osa9tfcIq5fPjDnd0uY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1598269864; 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=3GmZvLtvJwEB5eeX19PP6jMZw8+iCLu1EzinqfRoKgM=; b=c/Kwq9djBGSOoErnLeeDyZ4346+OGjyqcpU4p/BrXUDybXVV7NdzzoflqXXSMl6teJ5t8hu0CjVBFCk7PW9yrfP0WjEj/9g+1TiC2X/LWHOeN1/jzHHexTiwsg7K1JeOZ70dJGZ8Hv4qegP4AMKvIqmCaGvoJNyGpsBeHHCMK1g= ARC-Authentication-Results: i=1; mx.zohomail.com; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1598269864471770.6884844361562; Mon, 24 Aug 2020 04:51:04 -0700 (PDT) Received: from localhost ([::1]:38014 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kAB0B-0005ck-2l for importer@patchew.org; Mon, 24 Aug 2020 07:51:03 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51340) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kAAyR-0002UY-JK for qemu-devel@nongnu.org; Mon, 24 Aug 2020 07:49:15 -0400 Received: from gate-2.cri.epita.net ([163.5.55.20]:54788 helo=mail-2.srv.cri.epita.fr) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kAAyP-0003cJ-Tw for qemu-devel@nongnu.org; Mon, 24 Aug 2020 07:49:15 -0400 Received: from MattGorko-Laptop.home (lfbn-idf1-1-1395-83.w90-79.abo.wanadoo.fr [90.79.87.83]) (Authenticated sender: cesar.belley) by mail-2.srv.cri.epita.fr (Postfix) with ESMTPSA id B5D363FB5A; Mon, 24 Aug 2020 13:49:12 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=lse.epita.fr; s=cri; t=1598269752; bh=WDA0oI2Z69R2zXIfx2G23h1o+q5997g9l9qIdMw94RQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mpnVpbl+sRfYFZwdKLVT0THHPmpbPTKBgdNxF4BuVm+S+2xj6j7xKWvCcXlkywx86 hj9c4F2a39E3mmbV065lkUiM14tn+E+AErJepNbQOlOn83lqR6DfNM46fPvEu+QKc0 DU2/VoiG726/M2wY2ioN2ByJ0EFLAIkaNtdhyEI4= From: =?UTF-8?q?C=C3=A9sar=20Belley?= To: qemu-devel@nongnu.org Subject: [PATCH v2 01/13] hw/usb: Regroup USB HID protocol values Date: Mon, 24 Aug 2020 13:48:15 +0200 Message-Id: <20200824114827.81623-2-cesar.belley@lse.epita.fr> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200824114827.81623-1-cesar.belley@lse.epita.fr> References: <20200824114827.81623-1-cesar.belley@lse.epita.fr> MIME-Version: 1.0 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=163.5.55.20; envelope-from=srs0=45nv=cc=lse.epita.fr=cesar.belley@cri.epita.fr; helo=mail-2.srv.cri.epita.fr X-detected-operating-system: by eggs.gnu.org: First seen = 2020/08/24 07:49:04 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] X-Spam_score_int: -16 X-Spam_score: -1.7 X-Spam_bar: - X-Spam_report: (-1.7 / 5.0 requ) BAYES_00=-1.9, DKIM_INVALID=0.1, DKIM_SIGNED=0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no 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?C=C3=A9sar=20Belley?= , kraxel@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Group some HID values that are used pretty much everywhere when dealing with HID devices. Signed-off-by: C=C3=A9sar Belley --- hw/usb/dev-hid.c | 26 +++++++------------------- hw/usb/dev-wacom.c | 12 +++--------- include/hw/usb/hid.h | 17 +++++++++++++++++ 3 files changed, 27 insertions(+), 28 deletions(-) create mode 100644 include/hw/usb/hid.h diff --git a/hw/usb/dev-hid.c b/hw/usb/dev-hid.c index 89f63b698b..c73f7b2fe2 100644 --- a/hw/usb/dev-hid.c +++ b/hw/usb/dev-hid.c @@ -32,21 +32,9 @@ #include "qemu/module.h" #include "qemu/timer.h" #include "hw/input/hid.h" +#include "hw/usb/hid.h" #include "hw/qdev-properties.h" =20 -/* HID interface requests */ -#define GET_REPORT 0xa101 -#define GET_IDLE 0xa102 -#define GET_PROTOCOL 0xa103 -#define SET_REPORT 0x2109 -#define SET_IDLE 0x210a -#define SET_PROTOCOL 0x210b - -/* HID descriptor types */ -#define USB_DT_HID 0x21 -#define USB_DT_REPORT 0x22 -#define USB_DT_PHY 0x23 - typedef struct USBHIDState { USBDevice dev; USBEndpoint *intr; @@ -618,38 +606,38 @@ static void usb_hid_handle_control(USBDevice *dev, US= BPacket *p, goto fail; } break; - case GET_REPORT: + case HID_GET_REPORT: if (hs->kind =3D=3D HID_MOUSE || hs->kind =3D=3D HID_TABLET) { p->actual_length =3D hid_pointer_poll(hs, data, length); } else if (hs->kind =3D=3D HID_KEYBOARD) { p->actual_length =3D hid_keyboard_poll(hs, data, length); } break; - case SET_REPORT: + case HID_SET_REPORT: if (hs->kind =3D=3D HID_KEYBOARD) { p->actual_length =3D hid_keyboard_write(hs, data, length); } else { goto fail; } break; - case GET_PROTOCOL: + case HID_GET_PROTOCOL: if (hs->kind !=3D HID_KEYBOARD && hs->kind !=3D HID_MOUSE) { goto fail; } data[0] =3D hs->protocol; p->actual_length =3D 1; break; - case SET_PROTOCOL: + case HID_SET_PROTOCOL: if (hs->kind !=3D HID_KEYBOARD && hs->kind !=3D HID_MOUSE) { goto fail; } hs->protocol =3D value; break; - case GET_IDLE: + case HID_GET_IDLE: data[0] =3D hs->idle; p->actual_length =3D 1; break; - case SET_IDLE: + case HID_SET_IDLE: hs->idle =3D (uint8_t) (value >> 8); hid_set_next_idle(hs); if (hs->kind =3D=3D HID_MOUSE || hs->kind =3D=3D HID_TABLET) { diff --git a/hw/usb/dev-wacom.c b/hw/usb/dev-wacom.c index 8aba44b8bc..76fc5a5dab 100644 --- a/hw/usb/dev-wacom.c +++ b/hw/usb/dev-wacom.c @@ -29,6 +29,7 @@ #include "qemu/osdep.h" #include "ui/console.h" #include "hw/usb.h" +#include "hw/usb/hid.h" #include "migration/vmstate.h" #include "qemu/module.h" #include "desc.h" @@ -37,13 +38,6 @@ #define WACOM_GET_REPORT 0x2101 #define WACOM_SET_REPORT 0x2109 =20 -/* HID interface requests */ -#define HID_GET_REPORT 0xa101 -#define HID_GET_IDLE 0xa102 -#define HID_GET_PROTOCOL 0xa103 -#define HID_SET_IDLE 0x210a -#define HID_SET_PROTOCOL 0x210b - typedef struct USBWacomState { USBDevice dev; USBEndpoint *intr; @@ -86,11 +80,11 @@ static const USBDescIface desc_iface_wacom =3D { /* HID descriptor */ .data =3D (uint8_t[]) { 0x09, /* u8 bLength */ - 0x21, /* u8 bDescriptorType */ + USB_DT_HID, /* u8 bDescriptorType */ 0x01, 0x10, /* u16 HID_class */ 0x00, /* u8 country_code */ 0x01, /* u8 num_descriptors */ - 0x22, /* u8 type: Report */ + USB_DT_REPORT, /* u8 type: Report */ 0x6e, 0, /* u16 len */ }, }, diff --git a/include/hw/usb/hid.h b/include/hw/usb/hid.h new file mode 100644 index 0000000000..1c142584ff --- /dev/null +++ b/include/hw/usb/hid.h @@ -0,0 +1,17 @@ +#ifndef HW_USB_HID_H +#define HW_USB_HID_H + +/* HID interface requests */ +#define HID_GET_REPORT 0xa101 +#define HID_GET_IDLE 0xa102 +#define HID_GET_PROTOCOL 0xa103 +#define HID_SET_REPORT 0x2109 +#define HID_SET_IDLE 0x210a +#define HID_SET_PROTOCOL 0x210b + +/* HID descriptor types */ +#define USB_DT_HID 0x21 +#define USB_DT_REPORT 0x22 +#define USB_DT_PHY 0x23 + +#endif --=20 2.28.0 From nobody Sun May 19 01:43:01 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 ARC-Seal: i=1; a=rsa-sha256; t=1598269824; cv=none; d=zohomail.com; s=zohoarc; b=MpaqN4uedEqYZa/4P4UmHeFAwkH/++WdJG4nBbCBb/UgNtRrArVju3Sqof928hS9uSrThTysI18WOnmo0Oj4s8iqAdFpesWZeI7rJXk1slbZk6VEqIIj3GxDkeqY/ntgXKxrnq5u1RAxd/XaLjWmHzNr0MNh6GTbuHWrUGlAExU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1598269824; 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=qJHnNidFMFADmsdaExyya8m1j73y6AGQzKJl69XEeVc=; b=HTL5aeW828Vt0ruzydFiOdT71fXZMRun+VOSp2eLMitigt/rT1L2TzFLjUC/9BeKXWBZglor/fHnO+3d0CGljTg5Hyi8OKhfuVAoANDDADW3UuSE+9UNZRaUo1e2rBEU3MWa9QVWm/OWrp0xoTcZ1D31/g5aXPdS/St/O8S2Goc= ARC-Authentication-Results: i=1; mx.zohomail.com; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1598269824296654.0441936772411; Mon, 24 Aug 2020 04:50:24 -0700 (PDT) Received: from localhost ([::1]:34176 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kAAzX-00046Z-3H for importer@patchew.org; Mon, 24 Aug 2020 07:50:23 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51378) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kAAyS-0002Uz-TG for qemu-devel@nongnu.org; Mon, 24 Aug 2020 07:49:16 -0400 Received: from gate-2.cri.epita.net ([163.5.55.20]:54792 helo=mail-2.srv.cri.epita.fr) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kAAyQ-0003cP-P6 for qemu-devel@nongnu.org; Mon, 24 Aug 2020 07:49:16 -0400 Received: from MattGorko-Laptop.home (lfbn-idf1-1-1395-83.w90-79.abo.wanadoo.fr [90.79.87.83]) (Authenticated sender: cesar.belley) by mail-2.srv.cri.epita.fr (Postfix) with ESMTPSA id 4AB133FB60; Mon, 24 Aug 2020 13:49:13 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=lse.epita.fr; s=cri; t=1598269753; bh=mXjkHDuBA2Pelp7FvLqsvotpGAVI+Oj+eqQ5xB79ZVs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qCRkOn/oVfrKaCwoaoVQKMGgBVr3LUZttmVQW239j2gC7RnwwI27Uv8pQ75lUh9jW c/8T7yqGbF/gIu1rNzSuVhOkcFGTnlivpvJhuQfAWGeA66SzKtnHlOtoK17UEisE6s tOOQ2+XJ2VjYvzlOPbsOsGB+ZQwrFxVTkzgWpW9M= From: =?UTF-8?q?C=C3=A9sar=20Belley?= To: qemu-devel@nongnu.org Subject: [PATCH v2 02/13] docs: Add USB U2F key device documentation Date: Mon, 24 Aug 2020 13:48:16 +0200 Message-Id: <20200824114827.81623-3-cesar.belley@lse.epita.fr> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200824114827.81623-1-cesar.belley@lse.epita.fr> References: <20200824114827.81623-1-cesar.belley@lse.epita.fr> MIME-Version: 1.0 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=163.5.55.20; envelope-from=srs0=45nv=cc=lse.epita.fr=cesar.belley@cri.epita.fr; helo=mail-2.srv.cri.epita.fr X-detected-operating-system: by eggs.gnu.org: First seen = 2020/08/24 07:49:04 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] X-Spam_score_int: -16 X-Spam_score: -1.7 X-Spam_bar: - X-Spam_report: (-1.7 / 5.0 requ) BAYES_00=-1.9, DKIM_INVALID=0.1, DKIM_SIGNED=0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no 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?C=C3=A9sar=20Belley?= , kraxel@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Add USB U2F key device documentation: - USB U2F key device - Building - Using u2f-emulated - Using u2f-passthru - Libu2f-emu Signed-off-by: C=C3=A9sar Belley --- docs/u2f.txt | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 docs/u2f.txt diff --git a/docs/u2f.txt b/docs/u2f.txt new file mode 100644 index 0000000000..f60052882e --- /dev/null +++ b/docs/u2f.txt @@ -0,0 +1,101 @@ +QEMU U2F Key Device Documentation. + +Contents +1. USB U2F key device +2. Building +3. Using u2f-emulated +4. Using u2f-passthru +5. Libu2f-emu + +1. USB U2F key device + +U2F is an open authentication standard that enables relying parties +exposed to the internet to offer a strong second factor option for end +user authentication. + +The standard brings many advantages to both parties, client and server, +allowing to reduce over-reliance on passwords, it increases authentication +security and simplifies passwords. + +The second factor is materialized by a device implementing the U2F +protocol. In case of a USB U2F security key, it is a USB HID device +that implements the U2F protocol. + +In Qemu, the USB U2F key device offers a dedicated support of U2F, allowing +guest USB FIDO/U2F security keys operating in two possible modes: +pass-through and emulated. + +The pass-through mode consists of passing all requests made from the guest +to the physical security key connected to the host machine and vice versa. +In addition, the dedicated pass-through allows to have a U2F security key +shared on several guests which is not possible with a simple host device +assignment pass-through. + +The emulated mode consists of completely emulating the behavior of an +U2F device through software part. Libu2f-emu is used for that. + + +2. Building + +To ensure the build of the u2f-emulated device variant which depends +on libu2f-emu: configuring and building: + + ./configure --enable-u2f && make + + +3. Using u2f-emulated + +To work, an emulated U2F device must have four elements: + * ec x509 certificate + * ec private key + * counter (four bytes value) + * 48 bytes of entropy (random bits) + +To use this type of device, this one has to be configured, and these +four elements must be passed one way or another. + +Assuming that you have a working libu2f-emu installed on the host. +There are three possible ways of configurations: + * ephemeral + * setup directory + * manual + +Ephemeral is the simplest way to configure, it lets the device generate +all the elements it needs for a single use of the lifetime of the device. + + qemu -usb -device u2f-emulated + +Setup directory allows to configure the device from a directory containing +four files: + * certificate.pem: ec x509 certificate + * private-key.pem: ec private key + * counter: counter value + * entropy: 48 bytes of entropy + + qemu -usb -device u2f-emulated,dir=3D$dir + +Manual allows to configure the device more finely by specifying each +of the elements necessary for the device: + * cert + * priv + * counter + * entropy + + qemu -usb -device u2f-emulated,cert=3D$DIR1/$FILE1,priv=3D$DIR2/$FILE2= ,counter=3D$DIR3/$FILE3,entropy=3D$DIR4/$FILE4 + + +4. Using u2f-passthru + +On the host specify the u2f-passthru device with a suitable hidraw: + + qemu -usb -device u2f-passthru,hidraw=3D/dev/hidraw0 + + +5. Libu2f-emu + +The u2f-emulated device uses libu2f-emu for the U2F key emulation. Libu2f-= emu +implements completely the U2F protocol device part for all specified +transport given by the FIDO Alliance. + +For more information about libu2f-emu see this page: +https://github.com/MattGorko/libu2f-emu. --=20 2.28.0 From nobody Sun May 19 01:43:01 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 ARC-Seal: i=1; a=rsa-sha256; t=1598269941; cv=none; d=zohomail.com; s=zohoarc; b=h+DOh9EmPYdBvHB1AS1mITT4sBZMTeR2/Ia79rEEtoJtXtOPsY/Hc09pXgSA/R89KzXMyCLEb3lDyEdSLX8qJV4F4614NAiH3t4Z8On6FfQPqDZATyrl1nvR/NIQn1nXAqmWg7O2N01/ppoGsspT6DZqbiXcY5EOT/j4t9X71QM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1598269941; 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=qizeQIHGz1rMbab6I8KGp3QoxBVANddrYoa3Vx7/ckk=; b=hq21i4dcqL91ZHbqlwMyu7hhb3VT0XYlh06PVYR6Ck1F0v5n0B/niYWI9/Ks0Ijr5106wOVa/wyKoGRYJqvrYs0XP0i9ORd7w0wfmKTXJKY2/Et7S4uNV0EZ3HjPfMwtQGT/o1ZJ5azKEcbZzjen69jWa6Skf1v/lXhUWxabyWI= ARC-Authentication-Results: i=1; mx.zohomail.com; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 15982699417591022.3979010204191; Mon, 24 Aug 2020 04:52:21 -0700 (PDT) Received: from localhost ([::1]:46360 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kAB1Q-0000e1-GI for importer@patchew.org; Mon, 24 Aug 2020 07:52:20 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51374) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kAAyS-0002Uw-RO for qemu-devel@nongnu.org; Mon, 24 Aug 2020 07:49:16 -0400 Received: from gate-2.cri.epita.net ([163.5.55.20]:54798 helo=mail-2.srv.cri.epita.fr) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kAAyQ-0003cX-Ur for qemu-devel@nongnu.org; Mon, 24 Aug 2020 07:49:16 -0400 Received: from MattGorko-Laptop.home (lfbn-idf1-1-1395-83.w90-79.abo.wanadoo.fr [90.79.87.83]) (Authenticated sender: cesar.belley) by mail-2.srv.cri.epita.fr (Postfix) with ESMTPSA id BC7103FAC7; Mon, 24 Aug 2020 13:49:13 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=lse.epita.fr; s=cri; t=1598269753; bh=cvFDhlHRNc+dPkxjMum8CmWTe5nwcNjnFM+clGU5O+A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=eLbQt3wBWd4JWa7a1huVhzJmTVPsv3A0g4CfP98WV56roIPGDpSVGmoHwkeekjLCN 2QQUyVh4+JDfplaDGzBwQefhpANhUMMKqY4kIZlCdiSYyuotuNx3m+YEG4xTX22Y8z cmKcSMk7fEr5qUBEtcrrh6MeDLnrIULuoXOBZZIM= From: =?UTF-8?q?C=C3=A9sar=20Belley?= To: qemu-devel@nongnu.org Subject: [PATCH v2 03/13] hw/usb: Add U2F key base class Date: Mon, 24 Aug 2020 13:48:17 +0200 Message-Id: <20200824114827.81623-4-cesar.belley@lse.epita.fr> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200824114827.81623-1-cesar.belley@lse.epita.fr> References: <20200824114827.81623-1-cesar.belley@lse.epita.fr> MIME-Version: 1.0 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=163.5.55.20; envelope-from=srs0=45nv=cc=lse.epita.fr=cesar.belley@cri.epita.fr; helo=mail-2.srv.cri.epita.fr X-detected-operating-system: by eggs.gnu.org: First seen = 2020/08/24 07:49:04 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] X-Spam_score_int: -16 X-Spam_score: -1.7 X-Spam_bar: - X-Spam_report: (-1.7 / 5.0 requ) BAYES_00=-1.9, DKIM_INVALID=0.1, DKIM_SIGNED=0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no 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?C=C3=A9sar=20Belley?= , kraxel@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" This patch adds the specification for the U2F key base class. Used to group the common characteristics, this device class will be inherited by its two variants, corresponding to the two modes: passthrough and emulated This prepares the U2F devices hierarchy which is as follow: USB device -> u2f-key -> {u2f-passthru, u2f-emulated}. Signed-off-by: C=C3=A9sar Belley --- hw/usb/u2f.h | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 hw/usb/u2f.h diff --git a/hw/usb/u2f.h b/hw/usb/u2f.h new file mode 100644 index 0000000000..db30f3586b --- /dev/null +++ b/hw/usb/u2f.h @@ -0,0 +1,92 @@ +/* + * 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. + */ + +#ifndef U2F_H +#define U2F_H + +#include "hw/qdev-core.h" + +#define U2FHID_PACKET_SIZE 64 +#define U2FHID_PENDING_IN_NUM 32 + +typedef struct U2FKeyState U2FKeyState; +typedef struct U2FKeyInfo U2FKeyInfo; + +#define TYPE_U2F_KEY "u2f-key" +#define U2F_KEY(obj) \ + OBJECT_CHECK(U2FKeyState, (obj), TYPE_U2F_KEY) +#define U2F_KEY_CLASS(klass) \ + OBJECT_CLASS_CHECK(U2FKeyClass, (klass), TYPE_U2F_KEY) +#define U2F_KEY_GET_CLASS(obj) \ + OBJECT_GET_CLASS(U2FKeyClass, (obj), TYPE_U2F_KEY) + +/* + * Callbacks to be used by the U2F key base device (i.e. hw/u2f.c) + * to interact with its variants (i.e. hw/u2f-*.c) + */ +typedef struct U2FKeyClass { + /*< private >*/ + USBDeviceClass parent_class; + + /*< public >*/ + void (*recv_from_guest)(U2FKeyState *key, + const uint8_t packet[U2FHID_PACKET_SIZE]); + void (*realize)(U2FKeyState *key, Error **errp); + void (*unrealize)(U2FKeyState *key); +} U2FKeyClass; + +/* + * State of the U2F key base device (i.e. hw/u2f.c) + */ +typedef struct U2FKeyState { + USBDevice dev; + USBEndpoint *ep; + uint8_t idle; + + /* Pending packets to be send to the guest */ + uint8_t pending_in[U2FHID_PENDING_IN_NUM][U2FHID_PACKET_SIZE]; + uint8_t pending_in_start; + uint8_t pending_in_end; + uint8_t pending_in_num; +} U2FKeyState; + +/* + * API to be used by the U2F key device variants (i.e. hw/u2f-*.c) + * to interact with the the U2F key base device (i.e. hw/u2f.c) + */ +void u2f_send_to_guest(U2FKeyState *key, + const uint8_t packet[U2FHID_PACKET_SIZE]); + +extern const VMStateDescription vmstate_u2f_key; + +#define VMSTATE_U2F_KEY(_field, _state) { \ + .name =3D (stringify(_field)), \ + .size =3D sizeof(U2FKeyState), \ + .vmsd =3D &vmstate_u2f_key, \ + .flags =3D VMS_STRUCT, \ + .offset =3D vmstate_offset_value(_state, _field, U2FKeyState), \ +} + +#endif /* U2F_H */ --=20 2.28.0 From nobody Sun May 19 01:43:01 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 ARC-Seal: i=1; a=rsa-sha256; t=1598269903; cv=none; d=zohomail.com; s=zohoarc; b=U2s22FzyWVRqyYFYkJR8ow3Vx/3m9QKbw5FuDaUH/LSyhM8K/PKVjhkYLV85l9tuFBKQit+c77F3s73a2E2oKi4uH8062DyRteJ9n35Wa+UeFMlSmxwMuGsQ7g4WXm7YCOw1eVLI8MELy17tac3zsMkp3g1RyVTwI5EHTh59hcI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1598269903; 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=DQJdfdLdFUuF+qgn9AWSl8yuJ2gvbmd8MQ78zNGxXdM=; b=YHzxPUSDtW+c4zaq3jTHbPxUdunYUsOWnBOkBqpDMjYrjFruKojpgzaMCIXNTwFA5zl74U5NPas/LEKB9iD6Smv4OdV2/J7HZ4VIbZCBNaaNhtUCbHfuDB17Qh/eKXzN7zZZAvgbVbQAjzdJo22UP81v7hA3Z1g3uP3OI4RVItw= ARC-Authentication-Results: i=1; mx.zohomail.com; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1598269903486589.3782981260995; Mon, 24 Aug 2020 04:51:43 -0700 (PDT) Received: from localhost ([::1]:42220 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kAB0o-0007Pn-5D for importer@patchew.org; Mon, 24 Aug 2020 07:51:42 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51398) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kAAyT-0002WL-Mt for qemu-devel@nongnu.org; Mon, 24 Aug 2020 07:49:17 -0400 Received: from gate-2.cri.epita.net ([163.5.55.20]:54806 helo=mail-2.srv.cri.epita.fr) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kAAyR-0003cg-ID for qemu-devel@nongnu.org; Mon, 24 Aug 2020 07:49:17 -0400 Received: from MattGorko-Laptop.home (lfbn-idf1-1-1395-83.w90-79.abo.wanadoo.fr [90.79.87.83]) (Authenticated sender: cesar.belley) by mail-2.srv.cri.epita.fr (Postfix) with ESMTPSA id 576D0410DA; Mon, 24 Aug 2020 13:49:14 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=lse.epita.fr; s=cri; t=1598269754; bh=bZPaLgMb0FgA40ZLz8H5S+gZV0ArePaSLMEk5AtLEio=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HNsEA9NH4KfQYxysT8R8O299KV2ZNp3lNqIt1gSrqHV3AnrtgLWGuQHzMzPo1VhZS fECN9fOLjMQLwZwPg1CieZPQL/S2xHzxNQBnAbclpyONHPaI8gZrRy64DlEmZ84sYM ycnTt1I/wWkjm3Irk67k6sls/qQNPSiTQDUDAVkU= From: =?UTF-8?q?C=C3=A9sar=20Belley?= To: qemu-devel@nongnu.org Subject: [PATCH v2 04/13] hw/usb: Add U2F key base class implementation Date: Mon, 24 Aug 2020 13:48:18 +0200 Message-Id: <20200824114827.81623-5-cesar.belley@lse.epita.fr> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200824114827.81623-1-cesar.belley@lse.epita.fr> References: <20200824114827.81623-1-cesar.belley@lse.epita.fr> MIME-Version: 1.0 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=163.5.55.20; envelope-from=srs0=45nv=cc=lse.epita.fr=cesar.belley@cri.epita.fr; helo=mail-2.srv.cri.epita.fr X-detected-operating-system: by eggs.gnu.org: First seen = 2020/08/24 07:49:04 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] X-Spam_score_int: -16 X-Spam_score: -1.7 X-Spam_bar: - X-Spam_report: (-1.7 / 5.0 requ) BAYES_00=-1.9, DKIM_INVALID=0.1, DKIM_SIGNED=0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no 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?C=C3=A9sar=20Belley?= , kraxel@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" 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 --- 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 0000000000..bc09191f06 --- /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.28.0 From nobody Sun May 19 01:43:01 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 ARC-Seal: i=1; a=rsa-sha256; t=1598270029; cv=none; d=zohomail.com; s=zohoarc; b=C48oKCyUawyQIaV0OhWv5s/PEwOkr76mTDwcOVGmWP1FFmG/0hyZNT1Pen8Vai4jgsFHebEhSFXNHkXEuxzMr8qbfYtX4x2FAKn1Ff73OmemWfIQMDbMRbCS8B4ohEpTJfItVqAoGy2cKSEgPa6/l36sCWqv0tWFveIBQOpLmPA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1598270029; 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=5fUbmdETYC27I/icw7M3q4gwDaV8n6osDGxXaCcsr7k=; b=WIZVfEeyaxxsqBi4ztfwDFD2wxT6QQWNMUGYHZEWuuHe0zBAmLvI+7iBiOc8/1zJc2sGGPCCI3NuN2W+vh4ceBnF9lx84vzHB1w36JexDT7znnHqFWHpV4EEPNVCv3eGvvz9nj9jZcrELxjJDjXV2byZrj0rec9A13I/bxJsPqY= ARC-Authentication-Results: i=1; mx.zohomail.com; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1598270029839724.4793548724979; Mon, 24 Aug 2020 04:53:49 -0700 (PDT) Received: from localhost ([::1]:52976 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kAB2q-0003Hh-8C for importer@patchew.org; Mon, 24 Aug 2020 07:53:48 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51430) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kAAyU-0002Xp-Rv for qemu-devel@nongnu.org; Mon, 24 Aug 2020 07:49:18 -0400 Received: from gate-2.cri.epita.net ([163.5.55.20]:54812 helo=mail-2.srv.cri.epita.fr) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kAAyR-0003ck-U1 for qemu-devel@nongnu.org; Mon, 24 Aug 2020 07:49:18 -0400 Received: from MattGorko-Laptop.home (lfbn-idf1-1-1395-83.w90-79.abo.wanadoo.fr [90.79.87.83]) (Authenticated sender: cesar.belley) by mail-2.srv.cri.epita.fr (Postfix) with ESMTPSA id AEF05410E0; Mon, 24 Aug 2020 13:49:14 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=lse.epita.fr; s=cri; t=1598269754; bh=btRqdaUXVRbDKjkqGiCstodc5y/Je6n9QgR65B0DETM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dnhWop14o+n0eigCLLtbIvdXW9SzxDfCzFvuBZzddxA/lNb+4qpZiKldscw9Z2Iih wMKNtbBuKEziUdDBlEjZDWG+6auB3EYY2YV/LgvTjBAYOdu5NNY1pOeUCRMw5h7qkG Ut3qozgZf6PMiwWwvv94C79X+eyyE7ivvhhH5FqQ= From: =?UTF-8?q?C=C3=A9sar=20Belley?= To: qemu-devel@nongnu.org Subject: [PATCH v2 05/13] hw/usb: Add U2F key passthru mode Date: Mon, 24 Aug 2020 13:48:19 +0200 Message-Id: <20200824114827.81623-6-cesar.belley@lse.epita.fr> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200824114827.81623-1-cesar.belley@lse.epita.fr> References: <20200824114827.81623-1-cesar.belley@lse.epita.fr> MIME-Version: 1.0 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=163.5.55.20; envelope-from=srs0=45nv=cc=lse.epita.fr=cesar.belley@cri.epita.fr; helo=mail-2.srv.cri.epita.fr X-detected-operating-system: by eggs.gnu.org: First seen = 2020/08/24 07:49:04 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] X-Spam_score_int: -16 X-Spam_score: -1.7 X-Spam_bar: - X-Spam_report: (-1.7 / 5.0 requ) BAYES_00=-1.9, DKIM_INVALID=0.1, DKIM_SIGNED=0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no 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?C=C3=A9sar=20Belley?= , kraxel@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" This patch adds the U2F key pass-through mode. The pass-through mode consists of passing all requests made from the guest to the physical security key connected to the host machine and vice versa. In addition, the dedicated pass-through allows to have a U2F security key shared on several guests which is not possible with a simple host device assignment pass-through. The pass-through mode is associated with a device inheriting from u2f-key base. To work, it needs the path to a U2F hidraw, obtained from the Qemu command line, and passed by the user: qemu -usb -device u2f-passthru,hidraw=3D/dev/hidrawX Autoscan and U2F compatibility checking features are given at the end of the patch series. Signed-off-by: C=C3=A9sar Belley --- hw/usb/u2f-passthru.c | 423 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 423 insertions(+) create mode 100644 hw/usb/u2f-passthru.c diff --git a/hw/usb/u2f-passthru.c b/hw/usb/u2f-passthru.c new file mode 100644 index 0000000000..106b5abf9e --- /dev/null +++ b/hw/usb/u2f-passthru.c @@ -0,0 +1,423 @@ +/* + * U2F USB Passthru 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 "qemu/main-loop.h" +#include "qemu/error-report.h" +#include "qapi/error.h" +#include "hw/qdev-properties.h" +#include "hw/usb.h" +#include "migration/vmstate.h" + +#include "u2f.h" + +#define NONCE_SIZE 8 +#define BROADCAST_CID 0xFFFFFFFF +#define TRANSACTION_TIMEOUT 120000 + +struct transaction { + uint32_t cid; + uint16_t resp_bcnt; + uint16_t resp_size; + + /* Nonce for broadcast isolation */ + uint8_t nonce[NONCE_SIZE]; +}; + +typedef struct U2FPassthruState U2FPassthruState; + +#define CURRENT_TRANSACTIONS_NUM 4 + +struct U2FPassthruState { + U2FKeyState base; + + /* Host device */ + char *hidraw; + int hidraw_fd; + + /* Current Transactions */ + struct transaction current_transactions[CURRENT_TRANSACTIONS_NUM]; + uint8_t current_transactions_start; + uint8_t current_transactions_end; + uint8_t current_transactions_num; + + /* Transaction time checking */ + int64_t last_transaction_time; + QEMUTimer timer; +}; + +#define TYPE_U2F_PASSTHRU "u2f-passthru" +#define PASSTHRU_U2F_KEY(obj) \ + OBJECT_CHECK(U2FPassthruState, (obj), TYPE_U2F_PASSTHRU) + +/* Init packet sizes */ +#define PACKET_INIT_HEADER_SIZE 7 +#define PACKET_INIT_DATA_SIZE (U2FHID_PACKET_SIZE - PACKET_INIT_HEADER_SIZ= E) + +/* Cont packet sizes */ +#define PACKET_CONT_HEADER_SIZE 5 +#define PACKET_CONT_DATA_SIZE (U2FHID_PACKET_SIZE - PACKET_CONT_HEADER_SIZ= E) + +struct packet_init { + uint32_t cid; + uint8_t cmd; + uint8_t bcnth; + uint8_t bcntl; + uint8_t data[PACKET_INIT_DATA_SIZE]; +} QEMU_PACKED; + +static inline uint32_t packet_get_cid(const void *packet) +{ + return *((uint32_t *)packet); +} + +static inline bool packet_is_init(const void *packet) +{ + return ((uint8_t *)packet)[4] & (1 << 7); +} + +static inline uint16_t packet_init_get_bcnt( + const struct packet_init *packet_init) +{ + uint16_t bcnt =3D 0; + bcnt |=3D packet_init->bcnth << 8; + bcnt |=3D packet_init->bcntl; + + return bcnt; +} + +static void u2f_passthru_reset(U2FPassthruState *key) +{ + timer_del(&key->timer); + qemu_set_fd_handler(key->hidraw_fd, NULL, NULL, key); + key->last_transaction_time =3D 0; + key->current_transactions_start =3D 0; + key->current_transactions_end =3D 0; + key->current_transactions_num =3D 0; +} + +static void u2f_timeout_check(void *opaque) +{ + U2FPassthruState *key =3D opaque; + int64_t time =3D qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL); + + if (time > key->last_transaction_time + TRANSACTION_TIMEOUT) { + u2f_passthru_reset(key); + } else { + timer_mod(&key->timer, time + TRANSACTION_TIMEOUT / 4); + } +} + +static int u2f_transaction_get_index(U2FPassthruState *key, uint32_t cid) +{ + for (int i =3D 0; i < key->current_transactions_num; ++i) { + int index =3D (key->current_transactions_start + i) + % CURRENT_TRANSACTIONS_NUM; + if (cid =3D=3D key->current_transactions[index].cid) { + return index; + } + } + return -1; +} + +static struct transaction *u2f_transaction_get(U2FPassthruState *key, + uint32_t cid) +{ + int index =3D u2f_transaction_get_index(key, cid); + if (index < 0) { + return NULL; + } + return &key->current_transactions[index]; +} + +static struct transaction *u2f_transaction_get_from_nonce(U2FPassthruState= *key, + const uint8_t nonce[NONCE_SIZE]) +{ + for (int i =3D 0; i < key->current_transactions_num; ++i) { + int index =3D (key->current_transactions_start + i) + % CURRENT_TRANSACTIONS_NUM; + if (key->current_transactions[index].cid =3D=3D BROADCAST_CID + && memcmp(nonce, key->current_transactions[index].nonce, + NONCE_SIZE) =3D=3D 0) { + return &key->current_transactions[index]; + } + } + return NULL; +} + +static void u2f_transaction_close(U2FPassthruState *key, uint32_t cid) +{ + int index, next_index; + index =3D u2f_transaction_get_index(key, cid); + if (index < 0) { + return; + } + next_index =3D (index + 1) % CURRENT_TRANSACTIONS_NUM; + + /* Rearrange to ensure the oldest is at the start position */ + while (next_index !=3D key->current_transactions_end) { + memcpy(&key->current_transactions[index], + &key->current_transactions[next_index], + sizeof(struct transaction)); + + index =3D next_index; + next_index =3D (index + 1) % CURRENT_TRANSACTIONS_NUM; + } + + key->current_transactions_end =3D index; + --key->current_transactions_num; + + if (key->current_transactions_num =3D=3D 0) { + u2f_passthru_reset(key); + } +} + +static void u2f_transaction_add(U2FPassthruState *key, uint32_t cid, + const uint8_t nonce[NONCE_SIZE]) +{ + uint8_t index; + struct transaction *transaction; + + if (key->current_transactions_num >=3D CURRENT_TRANSACTIONS_NUM) { + /* Close the oldest transaction */ + index =3D key->current_transactions_start; + transaction =3D &key->current_transactions[index]; + u2f_transaction_close(key, transaction->cid); + } + + /* Index */ + index =3D key->current_transactions_end; + key->current_transactions_end =3D (index + 1) % CURRENT_TRANSACTIONS_N= UM; + ++key->current_transactions_num; + + /* Transaction */ + transaction =3D &key->current_transactions[index]; + transaction->cid =3D cid; + transaction->resp_bcnt =3D 0; + transaction->resp_size =3D 0; + + /* Nonce */ + if (nonce !=3D NULL) { + memcpy(transaction->nonce, nonce, NONCE_SIZE); + } +} + +static void u2f_passthru_read(void *opaque); + +static void u2f_transaction_start(U2FPassthruState *key, + const struct packet_init *packet_init) +{ + int64_t time; + + /* Transaction */ + if (packet_init->cid =3D=3D BROADCAST_CID) { + u2f_transaction_add(key, packet_init->cid, packet_init->data); + } else { + u2f_transaction_add(key, packet_init->cid, NULL); + } + + /* Time */ + time =3D qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL); + if (key->last_transaction_time =3D=3D 0) { + qemu_set_fd_handler(key->hidraw_fd, u2f_passthru_read, NULL, key); + timer_init_ms(&key->timer, QEMU_CLOCK_VIRTUAL, u2f_timeout_check, = key); + timer_mod(&key->timer, time + TRANSACTION_TIMEOUT / 4); + } + key->last_transaction_time =3D time; +} + +static void u2f_passthru_recv_from_host(U2FPassthruState *key, + const uint8_t packet[U2FHID_PACKET_SIZ= E]) +{ + struct transaction *transaction; + uint32_t cid; + + /* Retrieve transaction */ + cid =3D packet_get_cid(packet); + if (cid =3D=3D BROADCAST_CID) { + struct packet_init *packet_init; + if (!packet_is_init(packet)) { + return; + } + packet_init =3D (struct packet_init *)packet; + transaction =3D u2f_transaction_get_from_nonce(key, packet_init->d= ata); + } else { + transaction =3D u2f_transaction_get(key, cid); + } + + /* Ignore no started transaction */ + if (transaction =3D=3D NULL) { + return; + } + + if (packet_is_init(packet)) { + struct packet_init *packet_init =3D (struct packet_init *)packet; + transaction->resp_bcnt =3D packet_init_get_bcnt(packet_init); + transaction->resp_size =3D PACKET_INIT_DATA_SIZE; + + if (packet_init->cid =3D=3D BROADCAST_CID) { + /* Nonce checking for legitimate response */ + if (memcmp(transaction->nonce, packet_init->data, NONCE_SIZE) + !=3D 0) { + return; + } + } + } else { + transaction->resp_size +=3D PACKET_CONT_DATA_SIZE; + } + + /* Transaction end check */ + if (transaction->resp_size >=3D transaction->resp_bcnt) { + u2f_transaction_close(key, cid); + } + u2f_send_to_guest(&key->base, packet); +} + +static void u2f_passthru_read(void *opaque) +{ + U2FPassthruState *key =3D opaque; + U2FKeyState *base =3D &key->base; + uint8_t packet[2 * U2FHID_PACKET_SIZE]; + int ret; + + /* Full size base queue check */ + if (base->pending_in_num >=3D U2FHID_PENDING_IN_NUM) { + return; + } + + ret =3D read(key->hidraw_fd, packet, sizeof(packet)); + if (ret < 0) { + /* Detach */ + if (base->dev.attached) { + usb_device_detach(&base->dev); + u2f_passthru_reset(key); + } + return; + } + if (ret !=3D U2FHID_PACKET_SIZE) { + return; + } + u2f_passthru_recv_from_host(key, packet); +} + +static void u2f_passthru_recv_from_guest(U2FKeyState *base, + const uint8_t packet[U2FHID_PACKET_SIZ= E]) +{ + U2FPassthruState *key =3D PASSTHRU_U2F_KEY(base); + uint8_t host_packet[U2FHID_PACKET_SIZE + 1]; + ssize_t written; + + if (packet_is_init(packet)) { + u2f_transaction_start(key, (struct packet_init *)packet); + } + + host_packet[0] =3D 0; + memcpy(host_packet + 1, packet, U2FHID_PACKET_SIZE); + + written =3D write(key->hidraw_fd, host_packet, sizeof(host_packet)); + if (written !=3D sizeof(host_packet)) { + error_report("%s: Bad written size (req 0x%lx, val 0x%lx)", + TYPE_U2F_PASSTHRU, sizeof(host_packet), written); + } +} + +static void u2f_passthru_unrealize(U2FKeyState *base) +{ + U2FPassthruState *key =3D PASSTHRU_U2F_KEY(base); + + u2f_passthru_reset(key); + qemu_close(key->hidraw_fd); +} + +static void u2f_passthru_realize(U2FKeyState *base, Error **errp) +{ + U2FPassthruState *key =3D PASSTHRU_U2F_KEY(base); + int fd; + + if (key->hidraw =3D=3D NULL) { + error_setg(errp, "%s: Missing hidraw", TYPE_U2F_PASSTHRU); + return; + } + + fd =3D qemu_open(key->hidraw, O_RDWR); + if (fd < 0) { + error_setg(errp, "%s: Failed to open %s", TYPE_U2F_PASSTHRU, + key->hidraw); + return; + } + key->hidraw_fd =3D fd; + u2f_passthru_reset(key); +} + +static int u2f_passthru_post_load(void *opaque, int version_id) +{ + U2FPassthruState *key =3D opaque; + u2f_passthru_reset(key); + return 0; +} + +static const VMStateDescription u2f_passthru_vmstate =3D { + .name =3D "u2f-key-passthru", + .version_id =3D 1, + .minimum_version_id =3D 1, + .post_load =3D u2f_passthru_post_load, + .fields =3D (VMStateField[]) { + VMSTATE_U2F_KEY(base, U2FPassthruState), + VMSTATE_END_OF_LIST() + } +}; + +static Property u2f_passthru_properties[] =3D { + DEFINE_PROP_STRING("hidraw", U2FPassthruState, hidraw), + DEFINE_PROP_END_OF_LIST(), +}; + +static void u2f_passthru_class_init(ObjectClass *klass, void *data) +{ + DeviceClass *dc =3D DEVICE_CLASS(klass); + U2FKeyClass *kc =3D U2F_KEY_CLASS(klass); + + kc->realize =3D u2f_passthru_realize; + kc->unrealize =3D u2f_passthru_unrealize; + kc->recv_from_guest =3D u2f_passthru_recv_from_guest; + dc->desc =3D "QEMU U2F passthrough key"; + dc->vmsd =3D &u2f_passthru_vmstate; + device_class_set_props(dc, u2f_passthru_properties); +} + +static const TypeInfo u2f_key_passthru_info =3D { + .name =3D TYPE_U2F_PASSTHRU, + .parent =3D TYPE_U2F_KEY, + .instance_size =3D sizeof(U2FPassthruState), + .class_init =3D u2f_passthru_class_init +}; + +static void u2f_key_passthru_register_types(void) +{ + type_register_static(&u2f_key_passthru_info); +} + +type_init(u2f_key_passthru_register_types) --=20 2.28.0 From nobody Sun May 19 01:43:01 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 ARC-Seal: i=1; a=rsa-sha256; t=1598269831; cv=none; d=zohomail.com; s=zohoarc; b=i1tixkJy6zVpom9gx+KiaHPzxke52Gupu80gfyBVfBzaWFxdxWWMoRiWoSJmUcZyaxu6hItz5qPzsuFuJUfXI+U2KfGztvHCZjciLRrx+4vp1lIuON1Zhz25rDhyIXae/HeyBRSXk2EUg8TJ19vUYMaMWkKTTsH+KMEtKGs/TNM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1598269831; 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=dyhiGUXJud5LkfKwUROsoQY/zvNwRmbcYG0YANZ3BpM=; b=lAEG9EuvdAY+FC24tctdtQ2OD2SGlkRoOHFhGioA0BSuvvsW7mUu8lKYU7nM+leKi1MWAIl5N+Bv+fVtvwRphbp3t88yuRIjOUqByW0UDuk4RaKjRrSPAFt2gS2KPvJ4PpmFMuMW/5NpP3fTpD79dVxYtVjGQUxvq/Wfs829/JA= ARC-Authentication-Results: i=1; mx.zohomail.com; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1598269830742321.1975391317932; Mon, 24 Aug 2020 04:50:30 -0700 (PDT) Received: from localhost ([::1]:34474 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kAAzd-0004Dp-9y for importer@patchew.org; Mon, 24 Aug 2020 07:50:29 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51428) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kAAyU-0002Xo-O3 for qemu-devel@nongnu.org; Mon, 24 Aug 2020 07:49:18 -0400 Received: from gate-2.cri.epita.net ([163.5.55.20]:54818 helo=mail-2.srv.cri.epita.fr) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kAAyS-0003cr-AY for qemu-devel@nongnu.org; Mon, 24 Aug 2020 07:49:18 -0400 Received: from MattGorko-Laptop.home (lfbn-idf1-1-1395-83.w90-79.abo.wanadoo.fr [90.79.87.83]) (Authenticated sender: cesar.belley) by mail-2.srv.cri.epita.fr (Postfix) with ESMTPSA id 1EA5C412C7; Mon, 24 Aug 2020 13:49:15 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=lse.epita.fr; s=cri; t=1598269755; bh=YJhDGPHCamtSTbZXVCPZAZHaVE0kAI955YrNVx6bkSA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OgW1C9XEvY/Iltd0AOlY621h/ObKINqPRFt2OjPgxbcYkctqaS16EY+Y9xA01xEQj AfTI7mc7VLjl5Flp/VSwNDpdFsObGg5VB8R2cE0fSs/Fc8uFU4TlhHx5+AyJizXGjI aV0xsLmfnhGOWXorcSqjdkFKYKA/isSS1A2wE1vw= From: =?UTF-8?q?C=C3=A9sar=20Belley?= To: qemu-devel@nongnu.org Subject: [PATCH v2 06/13] hw/usb: Add U2F key emulated mode Date: Mon, 24 Aug 2020 13:48:20 +0200 Message-Id: <20200824114827.81623-7-cesar.belley@lse.epita.fr> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200824114827.81623-1-cesar.belley@lse.epita.fr> References: <20200824114827.81623-1-cesar.belley@lse.epita.fr> MIME-Version: 1.0 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=163.5.55.20; envelope-from=srs0=45nv=cc=lse.epita.fr=cesar.belley@cri.epita.fr; helo=mail-2.srv.cri.epita.fr X-detected-operating-system: by eggs.gnu.org: First seen = 2020/08/24 07:49:04 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] X-Spam_score_int: -16 X-Spam_score: -1.7 X-Spam_bar: - X-Spam_report: (-1.7 / 5.0 requ) BAYES_00=-1.9, DKIM_INVALID=0.1, DKIM_SIGNED=0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no 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?C=C3=A9sar=20Belley?= , kraxel@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" This patch adds the U2F key emulated mode. The emulated mode consists of completely emulating the behavior of a U2F device through software part. Libu2f-emu is used for that. The emulated mode is associated with a device inheriting from u2f-key base. To work, an emulated U2F device must have differents elements which can be given in different ways. This is detailed in docs/u2f.txt. The Ephemeral one is the simplest way to configure, it lets the device generate all the elements it needs for a single use of the lifetime of the device: qemu -usb -device u2f-emulated For more information about libu2f-emu see this page: https://github.com/MattGorko/libu2f-emu. Signed-off-by: C=C3=A9sar Belley --- hw/usb/u2f-emulated.c | 405 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 405 insertions(+) create mode 100644 hw/usb/u2f-emulated.c diff --git a/hw/usb/u2f-emulated.c b/hw/usb/u2f-emulated.c new file mode 100644 index 0000000000..9e1b829f3d --- /dev/null +++ b/hw/usb/u2f-emulated.c @@ -0,0 +1,405 @@ +/* + * U2F USB Emulated 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 "qemu/thread.h" +#include "qemu/main-loop.h" +#include "qapi/error.h" +#include "hw/usb.h" +#include "hw/qdev-properties.h" + +#include + +#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 =3D (struct synced_counter *)vdev_count= er; + ++counter->value; + + /* Write back */ + if (fseek(counter->fp, 0, SEEK_SET) =3D=3D -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 =3D (struct synced_counter *)vdev_count= er; + 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 =3D 0; + key->pending_out_end =3D 0; + key->pending_out_num =3D 0; +} + +static void u2f_pending_out_add(U2FEmulatedState *key, + const uint8_t packet[U2FHID_PACKET_SIZE]) +{ + int index; + + if (key->pending_out_num >=3D PENDING_OUT_NUM) { + return; + } + + index =3D key->pending_out_end; + key->pending_out_end =3D (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 =3D=3D 0) { + return NULL; + } + + index =3D key->pending_out_start; + key->pending_out_start =3D (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_SIZ= E]) +{ + U2FEmulatedState *key =3D 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 =3D arg; + uint8_t packet[U2FHID_PACKET_SIZE]; + uint8_t *packet_out =3D 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 =3D false; + break; + } + + qemu_mutex_lock(&key->pending_out_mutex); + packet_out =3D u2f_pending_out_get(key); + if (packet_out =3D=3D 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 =3D qemu_open(path, O_RDONLY); + if (fd < 0) { + return -1; + } + + ret =3D 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 =3D qemu_open(path, O_RDWR); + if (fd < 0) { + return false; + } + fp =3D fdopen(fd, "r+"); + if (fp =3D=3D NULL) { + close(fd); + return false; + } + ret =3D fscanf(fp, "%u", &counter->value); + if (ret =3D=3D EOF) { + fclose(fp); + return false; + } + counter->fp =3D fp; + counter->vdev_counter.counter_increment =3D counter_increment; + counter->vdev_counter.counter_read =3D 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 =3D u2f_emulated_read(key->cert, cert_pem, sizeof(cert_pem)); + if (ret < 0) { + return -1; + } + + /* Private key */ + ret =3D u2f_emulated_read(key->privkey, privkey_pem, sizeof(privkey_pe= m)); + if (ret < 0) { + return -1; + } + + /* Entropy */ + ret =3D 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 =3D cert_pem; + setup_info.private_key =3D privkey_pem; + setup_info.counter =3D (struct u2f_emu_vdev_counter *)&key->synced_cou= nter; + + return u2f_emu_vdev_new(&key->vdev, &setup_info); +} + +static void u2f_emulated_event_handler(EventNotifier *notifier) +{ + U2FEmulatedState *key =3D container_of(notifier, U2FEmulatedState, not= ifier); + size_t packet_size; + uint8_t *packet_in =3D 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 =3D u2f_emu_vdev_get_response(key->vdev, U2F_EMU_USB, + &packet_in); + if (packet_size =3D=3D 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 =3D EMULATED_U2F_KEY(base); + u2f_emu_rc rc; + + if (key->cert !=3D NULL || key->privkey !=3D NULL || key->entropy !=3D= NULL + || key->counter !=3D NULL) { + if (key->cert !=3D NULL && key->privkey !=3D NULL + && key->entropy !=3D NULL && key->counter !=3D NULL) { + rc =3D 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 !=3D NULL) { + rc =3D u2f_emu_vdev_new_from_dir(&key->vdev, key->dir); + } else { + rc =3D u2f_emu_vdev_new_ephemeral(&key->vdev); + } + + if (rc !=3D 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 =3D 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 =3D EMULATED_U2F_KEY(base); + + /* Thread */ + key->stop_thread =3D 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 !=3D NULL) { + fclose(key->synced_counter.fp); + } +} + +static Property u2f_emulated_properties[] =3D { + 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 =3D DEVICE_CLASS(klass); + U2FKeyClass *kc =3D U2F_KEY_CLASS(klass); + + kc->realize =3D u2f_emulated_realize; + kc->unrealize =3D u2f_emulated_unrealize; + kc->recv_from_guest =3D u2f_emulated_recv_from_guest; + dc->desc =3D "QEMU U2F emulated key"; + device_class_set_props(dc, u2f_emulated_properties); +} + +static const TypeInfo u2f_key_emulated_info =3D { + .name =3D TYPE_U2F_EMULATED, + .parent =3D TYPE_U2F_KEY, + .instance_size =3D sizeof(U2FEmulatedState), + .class_init =3D 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) --=20 2.28.0 From nobody Sun May 19 01:43:01 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 ARC-Seal: i=1; a=rsa-sha256; t=1598269907; cv=none; d=zohomail.com; s=zohoarc; b=VZMtuiri43FR3OjiFa8u68uuFJ8XD6Fz2p5x14RIuK70bw0aeUO5EDiwCtDlBI+7MDAC1AhRndp9VAHaKhZ81gQui0Zz61Spo+KFWCXrbkzA/tjQa2orGTyqu20KjH37uLGzfryBM2ciW8111vWeGdkW/uzLXMC85AAlLlx+yxA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1598269907; 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=fbIWBdG1sqCc3j/qI/u9HGII9ciSUTbgxn9pYFcXMOU=; b=I8q0gfQS0x3L/gMOl776K8BR1UJZrP+s9F/FFrcE0PpmnV7Bh8DIdc7SQFwDVPf7QQrc2UE8VfzL7pny+nV+Rp60jjgpLjamu/gZwCjXw0rafKdttOfiMRE1xrZrF+3kWQY7dAUhGiswpV3ZWrIXgLmeZpTk7wJ7tuiKHZaI0dM= ARC-Authentication-Results: i=1; mx.zohomail.com; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1598269907070881.4919949292304; Mon, 24 Aug 2020 04:51:47 -0700 (PDT) Received: from localhost ([::1]:42568 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kAB0r-0007YB-Kh for importer@patchew.org; Mon, 24 Aug 2020 07:51:45 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51438) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kAAyV-0002Yt-7H for qemu-devel@nongnu.org; Mon, 24 Aug 2020 07:49:19 -0400 Received: from gate-2.cri.epita.net ([163.5.55.20]:54822 helo=mail-2.srv.cri.epita.fr) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kAAyS-0003cy-LF for qemu-devel@nongnu.org; Mon, 24 Aug 2020 07:49:18 -0400 Received: from MattGorko-Laptop.home (lfbn-idf1-1-1395-83.w90-79.abo.wanadoo.fr [90.79.87.83]) (Authenticated sender: cesar.belley) by mail-2.srv.cri.epita.fr (Postfix) with ESMTPSA id 73685412C9; Mon, 24 Aug 2020 13:49:15 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=lse.epita.fr; s=cri; t=1598269755; bh=a+7ZXrsEm2NyNn+Qyc4H4Tfiz70E7/Z+HkEpxeMxp5s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Yaj+1N//azg7lXjhBL0DIrEmvGAnCGQL/99nKvAJNjv3qGzkte2P3Y88PjQDSkhPg HR9FYj0InPiHybQ+Z3t8cjHDRpv6SJTxER2NJVn+8/5xI4Nd8SREEK46+QZ/igG3EE dwOKBfYLnOJ9/hT0jBOVV10qZ23JQSOcp7mZhKSw= From: =?UTF-8?q?C=C3=A9sar=20Belley?= To: qemu-devel@nongnu.org Subject: [PATCH v2 07/13] hw/usb: Add U2F key build recipe Date: Mon, 24 Aug 2020 13:48:21 +0200 Message-Id: <20200824114827.81623-8-cesar.belley@lse.epita.fr> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200824114827.81623-1-cesar.belley@lse.epita.fr> References: <20200824114827.81623-1-cesar.belley@lse.epita.fr> MIME-Version: 1.0 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=163.5.55.20; envelope-from=srs0=45nv=cc=lse.epita.fr=cesar.belley@cri.epita.fr; helo=mail-2.srv.cri.epita.fr X-detected-operating-system: by eggs.gnu.org: First seen = 2020/08/24 07:49:04 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] X-Spam_score_int: -16 X-Spam_score: -1.7 X-Spam_bar: - X-Spam_report: (-1.7 / 5.0 requ) BAYES_00=-1.9, DKIM_INVALID=0.1, DKIM_SIGNED=0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no 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?C=C3=A9sar=20Belley?= , kraxel@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Signed-off-by: C=C3=A9sar Belley --- hw/usb/Kconfig | 5 +++++ hw/usb/meson.build | 5 +++++ meson.build | 6 ++++++ 3 files changed, 16 insertions(+) diff --git a/hw/usb/Kconfig b/hw/usb/Kconfig index 5e63dc75f8..3fc8fbe3c7 100644 --- a/hw/usb/Kconfig +++ b/hw/usb/Kconfig @@ -96,6 +96,11 @@ config USB_STORAGE_MTP default y depends on USB =20 +config USB_U2F + bool + default y + depends on USB + config IMX_USBPHY bool default y diff --git a/hw/usb/meson.build b/hw/usb/meson.build index 3c44a1b069..cac7639924 100644 --- a/hw/usb/meson.build +++ b/hw/usb/meson.build @@ -50,6 +50,11 @@ if config_host.has_key('CONFIG_SMARTCARD') hw_usb_modules +=3D {'smartcard': usbsmartcard_ss} endif =20 +# U2F +softmmu_ss.add(when: 'CONFIG_USB_U2F', if_true: files('u2f.c')) +softmmu_ss.add(when: ['CONFIG_LINUX', 'CONFIG_USB_U2F'], if_true: files('u= 2f-passthru.c')) +softmmu_ss.add(when: ['CONFIG_U2F', 'CONFIG_USB_U2F'], if_true: [u2f, file= s('u2f-emulated.c')]) + # usb redirect if config_host.has_key('CONFIG_USB_REDIR') usbredir_ss =3D ss.source_set() diff --git a/meson.build b/meson.build index df5bf728b5..1e6c9c598f 100644 --- a/meson.build +++ b/meson.build @@ -360,6 +360,11 @@ if 'CONFIG_SMARTCARD' in config_host cacard =3D declare_dependency(compile_args: config_host['SMARTCARD_CFLAG= S'].split(), link_args: config_host['SMARTCARD_LIBS'].spl= it()) endif +u2f =3D not_found +if 'CONFIG_U2F' in config_host + u2f =3D declare_dependency(compile_args: config_host['U2F_CFLAGS'].split= (), + link_args: config_host['U2F_LIBS'].split()) +endif usbredir =3D not_found if 'CONFIG_USB_REDIR' in config_host usbredir =3D declare_dependency(compile_args: config_host['USB_REDIR_CFL= AGS'].split(), @@ -1359,6 +1364,7 @@ summary_info +=3D {'spice support': config_host.h= as_key('CONFIG_SPICE')} summary_info +=3D {'rbd support': config_host.has_key('CONFIG_RBD')} summary_info +=3D {'xfsctl support': config_host.has_key('CONFIG_XFS')} summary_info +=3D {'smartcard support': config_host.has_key('CONFIG_SMARTC= ARD')} +summary_info +=3D {'U2F support': config_host.has_key('CONFIG_U2F')} summary_info +=3D {'libusb': config_host.has_key('CONFIG_USB_LI= BUSB')} summary_info +=3D {'usb net redir': config_host.has_key('CONFIG_USB_RE= DIR')} summary_info +=3D {'OpenGL support': config_host.has_key('CONFIG_OPENGL= ')} --=20 2.28.0 From nobody Sun May 19 01:43:01 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 ARC-Seal: i=1; a=rsa-sha256; t=1598269991; cv=none; d=zohomail.com; s=zohoarc; b=eJJaw/5fwiehWt8xfpqxDnZMquLTAnn42UfsedY8RXq+ry/Z8TevnvSOh7gcc4ndXepENOrtoa86uF3TEaZSVu7WSp7UD1oeiPWGv4oSKM93tIcyH48Vn9m/5vzsvx6I1n3JfPSNTpQSB3SvCWUUQs2Z5GpRD+NWpX50IvYcwYc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1598269991; 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=74EvUxbsR36NYjTiK1P559qtRevCvDeC87iS4zn1oVw=; b=IMLfsxNGm4WxMwpPEyKHyvv1T6XyQhrX4YwvRM7Y052vxjqWMGic3n3+uY6AH9O27lka1FwesUah8pHnHIM+9Rs3pUJBNsZuenwnV8GgpTGhMwYwZwT6rxv6RTXvU9tbjg7hVUHtMbwh2cnG0oYMPCaPNh85utJ85ifDXBD7dNk= ARC-Authentication-Results: i=1; mx.zohomail.com; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1598269991590495.6693736667378; Mon, 24 Aug 2020 04:53:11 -0700 (PDT) Received: from localhost ([::1]:50016 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kAB2E-00025i-Dr for importer@patchew.org; Mon, 24 Aug 2020 07:53:10 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51442) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kAAyV-0002a8-Nk for qemu-devel@nongnu.org; Mon, 24 Aug 2020 07:49:19 -0400 Received: from gate-2.cri.epita.net ([163.5.55.20]:54826 helo=mail-2.srv.cri.epita.fr) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kAAyS-0003d3-VL for qemu-devel@nongnu.org; Mon, 24 Aug 2020 07:49:19 -0400 Received: from MattGorko-Laptop.home (lfbn-idf1-1-1395-83.w90-79.abo.wanadoo.fr [90.79.87.83]) (Authenticated sender: cesar.belley) by mail-2.srv.cri.epita.fr (Postfix) with ESMTPSA id CB551412D2; Mon, 24 Aug 2020 13:49:15 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=lse.epita.fr; s=cri; t=1598269755; bh=YJtgc+Umbt+HNWGt7tySLOgQt78fbDR8UyHC6rJlKIk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=oTDC3NIVh6lhcVRM8AI0SaPnG2rVSlbJIdpYRmzefuWTmVRBHh2LDEBh5UeN6vw+6 gQhMJc527zljiGCHnEqcxlGFXGFIkFdMDR2uB5FYjYTdsfN85gHO7xqf5qD9swOT7m nMJK31cMq5g7+I641C7grEVM58WQC0DTOkI7VlUc= From: =?UTF-8?q?C=C3=A9sar=20Belley?= To: qemu-devel@nongnu.org Subject: [PATCH v2 08/13] configure: Add USB U2F key device Date: Mon, 24 Aug 2020 13:48:22 +0200 Message-Id: <20200824114827.81623-9-cesar.belley@lse.epita.fr> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200824114827.81623-1-cesar.belley@lse.epita.fr> References: <20200824114827.81623-1-cesar.belley@lse.epita.fr> MIME-Version: 1.0 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=163.5.55.20; envelope-from=srs0=45nv=cc=lse.epita.fr=cesar.belley@cri.epita.fr; helo=mail-2.srv.cri.epita.fr X-detected-operating-system: by eggs.gnu.org: First seen = 2020/08/24 07:49:04 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] X-Spam_score_int: -16 X-Spam_score: -1.7 X-Spam_bar: - X-Spam_report: (-1.7 / 5.0 requ) BAYES_00=-1.9, DKIM_INVALID=0.1, DKIM_SIGNED=0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no 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?C=C3=A9sar=20Belley?= , kraxel@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Signed-off-by: C=C3=A9sar Belley --- configure | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/configure b/configure index 67832e3bab..75d38e5411 100755 --- a/configure +++ b/configure @@ -495,6 +495,7 @@ trace_file=3D"trace" spice=3D"" rbd=3D"" smartcard=3D"" +u2f=3D"" libusb=3D"" usb_redir=3D"" opengl=3D"" @@ -1411,6 +1412,10 @@ for opt do ;; --enable-smartcard) smartcard=3D"yes" ;; + --disable-u2f) u2f=3D"no" + ;; + --enable-u2f) u2f=3D"yes" + ;; --disable-libusb) libusb=3D"no" ;; --enable-libusb) libusb=3D"yes" @@ -1940,6 +1945,7 @@ disabled with --disable-FEATURE, default is enabled i= f available: libiscsi iscsi support libnfs nfs support smartcard smartcard support (libcacard) + u2f U2F support (u2f-emu) libusb libusb (for usb passthrough) live-block-migration Block migration in the main migration stream usb-redir usb network redirection support @@ -5119,6 +5125,20 @@ if test "$smartcard" !=3D "no"; then fi fi =20 +# check for u2f support +if test "$u2f" !=3D "no"; then + if $pkg_config --atleast-version=3D0.0.0 u2f-emu; then + u2f_emu_cflags=3D$($pkg_config --cflags u2f-emu) + u2f_emu_libs=3D$($pkg_config --libs u2f-emu) + u2f=3D"yes" + else + if test "$u2f" =3D "yes"; then + feature_not_found "u2f" "Install u2f-emu" + fi + u2f=3D"no" + fi +fi + # check for libusb if test "$libusb" !=3D "no" ; then if $pkg_config --atleast-version=3D1.0.13 libusb-1.0; then @@ -7166,6 +7186,12 @@ if test "$smartcard" =3D "yes" ; then echo "SMARTCARD_LIBS=3D$libcacard_libs" >> $config_host_mak fi =20 +if test "$u2f" =3D "yes" ; then + echo "CONFIG_U2F=3Dy" >> $config_host_mak + echo "U2F_CFLAGS=3D$u2f_emu_cflags" >> $config_host_mak + echo "U2F_LIBS=3D$u2f_emu_libs" >> $config_host_mak +fi + if test "$libusb" =3D "yes" ; then echo "CONFIG_USB_LIBUSB=3Dy" >> $config_host_mak echo "LIBUSB_CFLAGS=3D$libusb_cflags" >> $config_host_mak --=20 2.28.0 From nobody Sun May 19 01:43:01 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 ARC-Seal: i=1; a=rsa-sha256; t=1598269985; cv=none; d=zohomail.com; s=zohoarc; b=e1LkPIeih+sJ1PxLZSS/pfat+vcmjwbpALHh3pP40Txl/Z8Ey+uuddvniVHbHWGd04W5BigMz822X5MLgSmkANGXT/M3RGbBHH3p9HPi2Kg83rKpR/ESj8N+Uq9+fgL8s4oRs9IUpQ8Y7hi6Ns5cRjgQC+kjoKFdFMw2ScZ2zYI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1598269985; 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=drNuxIgzsd+pB90KUH2svGeg2WIJeAB0Foune7N/ScM=; b=dv2YCKQ6TALgHUehe72miqdDOu3dczesVB5BMVXOK1DSpJLePvBvFhu+hMSIHGdV0ScvcORN1fUYiZ30NpUDLfaRu1lsPVwiklNw36ru5bYRl98JU2ABioGeSkjddkgb6QrY3vO56nrPhYN5g7taiAspLMmox/caVIf916z2yHg= ARC-Authentication-Results: i=1; mx.zohomail.com; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1598269985373329.51905878831053; Mon, 24 Aug 2020 04:53:05 -0700 (PDT) Received: from localhost ([::1]:49808 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kAB28-00020T-48 for importer@patchew.org; Mon, 24 Aug 2020 07:53:04 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51452) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kAAyW-0002cX-Pn for qemu-devel@nongnu.org; Mon, 24 Aug 2020 07:49:20 -0400 Received: from gate-2.cri.epita.net ([163.5.55.20]:54836 helo=mail-2.srv.cri.epita.fr) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kAAyV-0003da-B9 for qemu-devel@nongnu.org; Mon, 24 Aug 2020 07:49:20 -0400 Received: from MattGorko-Laptop.home (lfbn-idf1-1-1395-83.w90-79.abo.wanadoo.fr [90.79.87.83]) (Authenticated sender: cesar.belley) by mail-2.srv.cri.epita.fr (Postfix) with ESMTPSA id 18403410DA; Mon, 24 Aug 2020 13:49:18 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=lse.epita.fr; s=cri; t=1598269758; bh=nQhKuNn3MwH4gOHTPOjyBvj9K5iVBhe1uEtgY1+8Dds=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BMh2UdBDBRTCLH9JE6KNmq1jwO8SV/8t4zS41VqdiheLDoT2QwleB5IKfHVhNomiR JF/r4PeYTYISVolDRbOwuYzDdA1e9zeaYqkU6kYMdJqIkFKuSD+o7M/Mz19DJ1dlVP 6VlXTKGB18ydaxZhxW5DmV9vtid1YSjDkuZx9DT0= From: =?UTF-8?q?C=C3=A9sar=20Belley?= To: qemu-devel@nongnu.org Subject: [PATCH v2 09/13] docs/system: Add U2F key to the USB devices examples Date: Mon, 24 Aug 2020 13:48:23 +0200 Message-Id: <20200824114827.81623-10-cesar.belley@lse.epita.fr> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200824114827.81623-1-cesar.belley@lse.epita.fr> References: <20200824114827.81623-1-cesar.belley@lse.epita.fr> MIME-Version: 1.0 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=163.5.55.20; envelope-from=srs0=45nv=cc=lse.epita.fr=cesar.belley@cri.epita.fr; helo=mail-2.srv.cri.epita.fr X-detected-operating-system: by eggs.gnu.org: First seen = 2020/08/24 07:49:04 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] X-Spam_score_int: -16 X-Spam_score: -1.7 X-Spam_bar: - X-Spam_report: (-1.7 / 5.0 requ) BAYES_00=-1.9, DKIM_INVALID=0.1, DKIM_SIGNED=0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no 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?C=C3=A9sar=20Belley?= , kraxel@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Signed-off-by: C=C3=A9sar Belley --- docs/system/usb.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/system/usb.rst b/docs/system/usb.rst index ddfa828d74..9a2f1927c4 100644 --- a/docs/system/usb.rst +++ b/docs/system/usb.rst @@ -81,6 +81,9 @@ option or the ``device_add`` monitor command. Available d= evices are: ``usb-audio`` USB audio device =20 +``u2f-{emulated,passthru}`` + Universal Second Factor device + .. _host_005fusb_005fdevices: =20 Using host USB devices on a Linux host --=20 2.28.0 From nobody Sun May 19 01:43:01 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 ARC-Seal: i=1; a=rsa-sha256; t=1598269908; cv=none; d=zohomail.com; s=zohoarc; b=Z/Xr+8iSGFGRte1HUKTT2F/wC5BQ8AIbxUUUOUDQ/ay2mnnjoSEuyP2A4t0LqSDmxC53TZCTm5bl67WCkhzTzSYbR6l5vFYB/BG+8TnofLRdnpsoKPyKEl0lh3PdHu7aKe+/gKJfx+EIMXX/V/a73QZar4ggLgITCUfeqXzXLLI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1598269908; 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=uJCNxB6JCPdQPLv3l+tt3Qqwl+UVKQbhSHUEQjuw/8Q=; b=D4Pi6ect6Pi+ScX1QUM7Lu9LDKxyGCzJ+NHPxm5OyjPK52tuSfDg7TEXQuCZLg4F/VjDif1q1Dh65HbpUexTj4oZD+v5Qcgp/O30dOncDXtSErSTz25FNBLkOKc7cTk89F4BkhYlb2Mty8WslrSUtJLbeudTlyn7PNmwHppLf54= ARC-Authentication-Results: i=1; mx.zohomail.com; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1598269908877600.7239455052936; Mon, 24 Aug 2020 04:51:48 -0700 (PDT) Received: from localhost ([::1]:42886 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kAB0t-0007fz-LY for importer@patchew.org; Mon, 24 Aug 2020 07:51:47 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51492) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kAAyh-00031J-Pz for qemu-devel@nongnu.org; Mon, 24 Aug 2020 07:49:31 -0400 Received: from gate-2.cri.epita.net ([163.5.55.20]:54842 helo=mail-2.srv.cri.epita.fr) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kAAyf-0003dm-Ug for qemu-devel@nongnu.org; Mon, 24 Aug 2020 07:49:31 -0400 Received: from MattGorko-Laptop.home (lfbn-idf1-1-1395-83.w90-79.abo.wanadoo.fr [90.79.87.83]) (Authenticated sender: cesar.belley) by mail-2.srv.cri.epita.fr (Postfix) with ESMTPSA id A21863FB60; Mon, 24 Aug 2020 13:49:18 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=lse.epita.fr; s=cri; t=1598269758; bh=/33EairxGlE32NvdntSN+rcwZBT7otF2vHwM46qIMbY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fc1Mc566/ohzDsk+X6Wu5wtB5TPdKtMu8WY4BvslmRnAgxKLS+uonxRACpPvQOSy4 iNyCfNdg8GyUNuemaC8K30X4FzXBv0VByEKBRdCHkaFi5Rw3aOHj5v3LCFWP9O7Nbb e8tszNC207QIo7uCq/ckJJT8mq0ZOwer+VbLdPFg= From: =?UTF-8?q?C=C3=A9sar=20Belley?= To: qemu-devel@nongnu.org Subject: [PATCH v2 10/13] docs/qdev-device-use.txt: Add USB U2F key to the QDEV devices examples Date: Mon, 24 Aug 2020 13:48:24 +0200 Message-Id: <20200824114827.81623-11-cesar.belley@lse.epita.fr> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200824114827.81623-1-cesar.belley@lse.epita.fr> References: <20200824114827.81623-1-cesar.belley@lse.epita.fr> MIME-Version: 1.0 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=163.5.55.20; envelope-from=srs0=45nv=cc=lse.epita.fr=cesar.belley@cri.epita.fr; helo=mail-2.srv.cri.epita.fr X-detected-operating-system: by eggs.gnu.org: First seen = 2020/08/24 07:49:04 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] X-Spam_score_int: -16 X-Spam_score: -1.7 X-Spam_bar: - X-Spam_report: (-1.7 / 5.0 requ) BAYES_00=-1.9, DKIM_INVALID=0.1, DKIM_SIGNED=0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no 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?C=C3=A9sar=20Belley?= , kraxel@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Signed-off-by: C=C3=A9sar Belley --- docs/qdev-device-use.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/qdev-device-use.txt b/docs/qdev-device-use.txt index f8d0d2fe29..9889521e3c 100644 --- a/docs/qdev-device-use.txt +++ b/docs/qdev-device-use.txt @@ -325,6 +325,7 @@ The new way is -device DEVNAME,DEV-OPTS... Details dep= end on DRIVER: * mouse -device usb-mouse * tablet -device usb-tablet * wacom-tablet -device usb-wacom-tablet +* u2f -device u2f-{emulated,passthru} * braille See "Character Devices" =20 =3D=3D=3D Watchdog Devices =3D=3D=3D --=20 2.28.0 From nobody Sun May 19 01:43:01 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 ARC-Seal: i=1; a=rsa-sha256; t=1598270064; cv=none; d=zohomail.com; s=zohoarc; b=MX437MjxUW33Dfo2pQxGXrv81qBG/GQ02xolXNnSOJi0nwJ8+X4PWNBJSBfm7xO20NcfGRRx0bFORPvUQiPLDB+hdSzQLmX/om2DqMQsKr+bZXzI0ywQn5J8a8zsHZRZ9XMLx08lYiKHoQS4DN/VNs82wmLigBc3d1lqprR7i4U= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1598270064; 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=pXZDszG46ZleNGv6ug1RiQDt401fnMtuLsrNepA3Wcc=; b=RuRgbHrPPIFQRIm+FWwoTk43wL/UhyjBbxmeceMikBMewaKZqWv9facTu8F8vu58aRdg4kUFaEMxFeU+QXzb1XzRqr91so6PbCpD4AOI2HxtQy8L0m5U+SblPOs2YCFjjtZXVnZJQhZgHwpv63R8OCgxde71y/YIjLqfLg/63A8= ARC-Authentication-Results: i=1; mx.zohomail.com; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1598270064452363.45802443491436; Mon, 24 Aug 2020 04:54:24 -0700 (PDT) Received: from localhost ([::1]:55120 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kAB3P-00049Z-2v for importer@patchew.org; Mon, 24 Aug 2020 07:54:23 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51502) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kAAyi-00032H-8t for qemu-devel@nongnu.org; Mon, 24 Aug 2020 07:49:32 -0400 Received: from gate-2.cri.epita.net ([163.5.55.20]:54846 helo=mail-2.srv.cri.epita.fr) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kAAyg-0003dt-BR for qemu-devel@nongnu.org; Mon, 24 Aug 2020 07:49:31 -0400 Received: from MattGorko-Laptop.home (lfbn-idf1-1-1395-83.w90-79.abo.wanadoo.fr [90.79.87.83]) (Authenticated sender: cesar.belley) by mail-2.srv.cri.epita.fr (Postfix) with ESMTPSA id 1ABAA410E0; Mon, 24 Aug 2020 13:49:19 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=lse.epita.fr; s=cri; t=1598269759; bh=WhUfeKytyuCUL59PtsGUkPGPstr2XF/LWbWzGaMaN3Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YeItsgd22twBRUg/HkerPoW2IZYPNuc4HQ5DCvjNP9tEBBR04LNpjvm1J0ILns4hu J8BTCeSe6u/KF1LN2mMG02bZbcQjBnwEKwveBB11J/DRO0hLNg7XgJKTNu6YnaboKZ QqP5Oe/7EyM6X/wYcHsC+d2JtYrvKssTD7+PjCoo= From: =?UTF-8?q?C=C3=A9sar=20Belley?= To: qemu-devel@nongnu.org Subject: [PATCH v2 11/13] scripts: Add u2f-setup-gen script Date: Mon, 24 Aug 2020 13:48:25 +0200 Message-Id: <20200824114827.81623-12-cesar.belley@lse.epita.fr> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200824114827.81623-1-cesar.belley@lse.epita.fr> References: <20200824114827.81623-1-cesar.belley@lse.epita.fr> MIME-Version: 1.0 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=163.5.55.20; envelope-from=srs0=45nv=cc=lse.epita.fr=cesar.belley@cri.epita.fr; helo=mail-2.srv.cri.epita.fr X-detected-operating-system: by eggs.gnu.org: First seen = 2020/08/24 07:49:04 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] X-Spam_score_int: -16 X-Spam_score: -1.7 X-Spam_bar: - X-Spam_report: (-1.7 / 5.0 requ) BAYES_00=-1.9, DKIM_INVALID=0.1, DKIM_SIGNED=0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no 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?C=C3=A9sar=20Belley?= , kraxel@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" This patch adds the script used to generate setup directories, needed for the device u2f-emulated configuration in directory mode: python u2f-setup-gen.py $DIR qemu -usb -device u2f-emulated,dir=3D$DIR Signed-off-by: C=C3=A9sar Belley --- scripts/u2f-setup-gen.py | 170 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 170 insertions(+) create mode 100755 scripts/u2f-setup-gen.py diff --git a/scripts/u2f-setup-gen.py b/scripts/u2f-setup-gen.py new file mode 100755 index 0000000000..2122598fed --- /dev/null +++ b/scripts/u2f-setup-gen.py @@ -0,0 +1,170 @@ +#!/usr/bin/env python3 +# +# Libu2f-emu setup directory generator for USB U2F key emulation. +# +# Copyright (c) 2020 C=C3=A9sar Belley +# Written by C=C3=A9sar Belley +# +# This work is licensed under the terms of the GNU GPL, version 2 +# or, at your option, any later version. See the COPYING file in +# the top-level directory. + +import sys +import os +from random import randint +from typing import Tuple + +from cryptography.hazmat.backends import default_backend +from cryptography.hazmat.primitives.asymmetric import ec +from cryptography.hazmat.primitives.serialization import Encoding, \ + NoEncryption, PrivateFormat, PublicFormat +from OpenSSL import crypto + + +def write_setup_dir(dirpath: str, privkey_pem: bytes, cert_pem: bytes, + entropy: bytes, counter: int) -> None: + """ + Write the setup directory. + + Args: + dirpath: The directory path. + key_pem: The private key PEM. + cert_pem: The certificate PEM. + entropy: The 48 bytes of entropy. + counter: The counter value. + """ + # Directory + os.mkdir(dirpath) + + # Private key + with open(f'{dirpath}/private-key.pem', 'bw') as f: + f.write(privkey_pem) + + # Certificate + with open(f'{dirpath}/certificate.pem', 'bw') as f: + f.write(cert_pem) + + # Entropy + with open(f'{dirpath}/entropy', 'wb') as f: + f.write(entropy) + + # Counter + with open(f'{dirpath}/counter', 'w') as f: + f.write(f'{str(counter)}\n') + + +def generate_ec_key_pair() -> Tuple[str, str]: + """ + Generate an ec key pair. + + Returns: + The private and public key PEM. + """ + # Key generation + privkey =3D ec.generate_private_key(ec.SECP256R1, default_backend()) + pubkey =3D privkey.public_key() + + # PEM serialization + privkey_pem =3D privkey.private_bytes(encoding=3DEncoding.PEM, + format=3DPrivateFormat.Traditional= OpenSSL, + encryption_algorithm=3DNoEncryptio= n()) + pubkey_pem =3D pubkey.public_bytes(encoding=3DEncoding.PEM, + format=3DPublicFormat.SubjectPublicKe= yInfo) + return privkey_pem, pubkey_pem + + +def generate_certificate(privkey_pem: str, pubkey_pem: str) -> str: + """ + Generate a x509 certificate from a key pair. + + Args: + privkey_pem: The private key PEM. + pubkey_pem: The public key PEM. + + Returns: + The certificate PEM. + """ + # Convert key pair + privkey =3D crypto.load_privatekey(crypto.FILETYPE_PEM, privkey_pem) + pubkey =3D crypto.load_publickey(crypto.FILETYPE_PEM, pubkey_pem) + + # New x509v3 certificate + cert =3D crypto.X509() + cert.set_version(0x2) + + # Serial number + cert.set_serial_number(randint(1, 2 ** 64)) + + # Before / After + cert.gmtime_adj_notBefore(0) + cert.gmtime_adj_notAfter(4 * (365 * 24 * 60 * 60)) + + # Public key + cert.set_pubkey(pubkey) + + # Subject name and issueer + cert.get_subject().CN =3D "U2F emulated" + cert.set_issuer(cert.get_subject()) + + # Extensions + cert.add_extensions([ + crypto.X509Extension(b"subjectKeyIdentifier", + False, b"hash", subject=3Dcert), + ]) + cert.add_extensions([ + crypto.X509Extension(b"authorityKeyIdentifier", + False, b"keyid:always", issuer=3Dcert), + ]) + cert.add_extensions([ + crypto.X509Extension(b"basicConstraints", True, b"CA:TRUE") + ]) + + # Signature + cert.sign(privkey, 'sha256') + + return crypto.dump_certificate(crypto.FILETYPE_PEM, cert) + + +def generate_setup_dir(dirpath: str) -> None: + """ + Generates the setup directory. + + Args: + dirpath: The directory path. + """ + # Key pair + privkey_pem, pubkey_pem =3D generate_ec_key_pair() + + # Certificate + certificate_pem =3D generate_certificate(privkey_pem, pubkey_pem) + + # Entropy + entropy =3D os.urandom(48) + + # Counter + counter =3D 0 + + # Write + write_setup_dir(dirpath, privkey_pem, certificate_pem, entropy, counte= r) + + +def main() -> None: + """ + Main function + """ + # Dir path + if len(sys.argv) !=3D 2: + sys.stderr.write(f'Usage: {sys.argv[0]} \n') + exit(2) + dirpath =3D sys.argv[1] + + # Dir non existence + if os.path.exists(dirpath): + sys.stderr.write(f'Directory: {dirpath} already exists.\n') + exit(1) + + generate_setup_dir(dirpath) + + +if __name__ =3D=3D '__main__': + main() --=20 2.28.0 From nobody Sun May 19 01:43:01 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 ARC-Seal: i=1; a=rsa-sha256; t=1598270102; cv=none; d=zohomail.com; s=zohoarc; b=Np3nIkRHJvSRd6urr0h7UWf4uz84kWTjlOMWiGFLSkLagzDzsf46LzdWfIoSiYCaCA0I/ZwhZUxHfVvf0ddT3UaO8RrzY6vIyb44MrIj6SUO0mP0GQbMhbrmeu5TRK2b5ZZAC4F06W2dLKJZ5ZIrQ9Kd3dAc3B7wPzPjnJ5vm7g= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1598270102; 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=C8jS8fweY8XLnpb61Tdz+uBIZ2oy6RyZjyYkVbu4tZM=; b=GDx0dXYtVuYofOkFfJVNFHAMkLdGNkQU2c7EhsaZ7qhlnFHKSlps6JqAIA95bLbs+dq9BryyUeuvAjhhz3/x63rp3QZOtbxarMp4kE7Y9ZIUaNueo0U71ZEvYbDEAH30mTrFZeJHij09R4MKEIKLkNLulA2oa6g53Y1zR1imOwU= ARC-Authentication-Results: i=1; mx.zohomail.com; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1598270102450844.8642803102193; Mon, 24 Aug 2020 04:55:02 -0700 (PDT) Received: from localhost ([::1]:57270 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kAB41-00050r-8M for importer@patchew.org; Mon, 24 Aug 2020 07:55:01 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51520) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kAAyj-00034L-Jf for qemu-devel@nongnu.org; Mon, 24 Aug 2020 07:49:33 -0400 Received: from gate-2.cri.epita.net ([163.5.55.20]:54854 helo=mail-2.srv.cri.epita.fr) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kAAyg-0003e1-Lk for qemu-devel@nongnu.org; Mon, 24 Aug 2020 07:49:32 -0400 Received: from MattGorko-Laptop.home (lfbn-idf1-1-1395-83.w90-79.abo.wanadoo.fr [90.79.87.83]) (Authenticated sender: cesar.belley) by mail-2.srv.cri.epita.fr (Postfix) with ESMTPSA id 7185D412DD; Mon, 24 Aug 2020 13:49:19 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=lse.epita.fr; s=cri; t=1598269759; bh=oV+J1fqI2mcTVzHKlE6s1N2B0KJ3I3Q7UEclmD7xnak=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DHKOWM28qKcqN30RBm6HL+5B5YiUTMs+RJjyM3za1RjK+dseqTVtxJJgYhkf/2asl jbBsCvNGWDqsKgevaYx1tua3kIQ0fP50Z0M//inOtt6RMp1Mg/Pqdib4TPCGrhdPgn 9xWdXZtN2ZItZIb5AkC+mRcxBvDHXHBebPPMLv8w= From: =?UTF-8?q?C=C3=A9sar=20Belley?= To: qemu-devel@nongnu.org Subject: [PATCH v2 12/13] hw/usb: Add U2F device check to passthru mode Date: Mon, 24 Aug 2020 13:48:26 +0200 Message-Id: <20200824114827.81623-13-cesar.belley@lse.epita.fr> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200824114827.81623-1-cesar.belley@lse.epita.fr> References: <20200824114827.81623-1-cesar.belley@lse.epita.fr> MIME-Version: 1.0 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=163.5.55.20; envelope-from=srs0=45nv=cc=lse.epita.fr=cesar.belley@cri.epita.fr; helo=mail-2.srv.cri.epita.fr X-detected-operating-system: by eggs.gnu.org: First seen = 2020/08/24 07:49:04 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] X-Spam_score_int: -16 X-Spam_score: -1.7 X-Spam_bar: - X-Spam_report: (-1.7 / 5.0 requ) BAYES_00=-1.9, DKIM_INVALID=0.1, DKIM_SIGNED=0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no 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?C=C3=A9sar=20Belley?= , kraxel@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" This patchs adds a check to verify that the device passed through the hidraw property is a U2F device. The check is done by ensuring that the first values of the report descriptor (USAGE PAGE and USAGE) correspond to those of a U2F device. Signed-off-by: C=C3=A9sar Belley --- hw/usb/u2f-passthru.c | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/hw/usb/u2f-passthru.c b/hw/usb/u2f-passthru.c index 106b5abf9e..f8771966c7 100644 --- a/hw/usb/u2f-passthru.c +++ b/hw/usb/u2f-passthru.c @@ -34,6 +34,12 @@ =20 #include "u2f.h" =20 +#ifdef CONFIG_LIBUDEV +#include +#endif +#include +#include + #define NONCE_SIZE 8 #define BROADCAST_CID 0xFFFFFFFF #define TRANSACTION_TIMEOUT 120000 @@ -344,6 +350,34 @@ static void u2f_passthru_recv_from_guest(U2FKeyState *= base, } } =20 +static bool u2f_passthru_is_u2f_device(int fd) +{ + int ret, rdesc_size; + struct hidraw_report_descriptor rdesc; + const uint8_t u2f_hid_report_desc_header[] =3D { + 0x06, 0xd0, 0xf1, /* Usage Page (FIDO) */ + 0x09, 0x01, /* Usage (FIDO) */ + }; + + /* Get report descriptor size */ + ret =3D ioctl(fd, HIDIOCGRDESCSIZE, &rdesc_size); + if (ret < 0 || rdesc_size < sizeof(u2f_hid_report_desc_header)) { + return false; + } + + /* Get report descriptor */ + memset(&rdesc, 0x0, sizeof(rdesc)); + rdesc.size =3D rdesc_size; + ret =3D ioctl(fd, HIDIOCGRDESC, &rdesc); + if (ret < 0) { + return false; + } + + /* Header bytes cover specific U2F rdesc values */ + return memcmp(u2f_hid_report_desc_header, rdesc.value, + sizeof(u2f_hid_report_desc_header)) =3D=3D 0; +} + static void u2f_passthru_unrealize(U2FKeyState *base) { U2FPassthruState *key =3D PASSTHRU_U2F_KEY(base); @@ -368,6 +402,13 @@ static void u2f_passthru_realize(U2FKeyState *base, Er= ror **errp) key->hidraw); return; } + + if (!u2f_passthru_is_u2f_device(fd)) { + qemu_close(fd); + error_setg(errp, "%s: Passed hidraw does not represent " + "a U2F HID device", TYPE_U2F_PASSTHRU); + return; + } key->hidraw_fd =3D fd; u2f_passthru_reset(key); } --=20 2.28.0 From nobody Sun May 19 01:43:01 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 ARC-Seal: i=1; a=rsa-sha256; t=1598270152; cv=none; d=zohomail.com; s=zohoarc; b=N/AokN4HegwGjf9SkAnAPGxbbOcDMFrrd4SlVQa0zL4Qhq3k1mw350MAWbGrVh3b9noZSvyRZ5P+a6QkD2vsU9qijJ0ObwTHIfCxAlrqa3GaQaw0hMlX4kmvNt2eFkbvLIApBmW9ru3wKCxcibWqHWnV+88ci/BFfFUNACMklww= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1598270152; 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=1Whg2mc5iyxnrIkOgqe4IhGahSTXU0yG89+O6eLQVY8=; b=Rq6pgXr1r7AtZDhCOub7pAf5KSxK91BeSOubyjBmQIbtwKA3fEFTyvQqgQEQA43MbtylIwPw8KXz8s+4kx/UVvsdDOee+FaX4Q24rHP+JVBl8kDyCeXD+SiOkn68xM03EUnyFoL3BD4842JIVVL+D524ss4cM5ikHL5GZymX0e4= ARC-Authentication-Results: i=1; mx.zohomail.com; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1598270152156235.51387158943282; Mon, 24 Aug 2020 04:55:52 -0700 (PDT) Received: from localhost ([::1]:59536 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kAB4o-0005zM-PG for importer@patchew.org; Mon, 24 Aug 2020 07:55:50 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51522) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kAAyj-00034n-V2 for qemu-devel@nongnu.org; Mon, 24 Aug 2020 07:49:33 -0400 Received: from gate-2.cri.epita.net ([163.5.55.20]:54860 helo=mail-2.srv.cri.epita.fr) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kAAyh-0003eF-H4 for qemu-devel@nongnu.org; Mon, 24 Aug 2020 07:49:33 -0400 Received: from MattGorko-Laptop.home (lfbn-idf1-1-1395-83.w90-79.abo.wanadoo.fr [90.79.87.83]) (Authenticated sender: cesar.belley) by mail-2.srv.cri.epita.fr (Postfix) with ESMTPSA id 3FEA53FAC7; Mon, 24 Aug 2020 13:49:20 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=lse.epita.fr; s=cri; t=1598269760; bh=hKpGvvq0chDFquaLVg/SoDp2sQXaURAZyryEMSCPX1s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DoKDykgc0fzo8vagx/A3/0KCHvGmVGNK/HApubrJyHavgexBd6LYGfTQWRvOPc6VC GyE1wauhTPPj4QgGqML1191jV2bo+pf3MiDCyAuvnDyptGrYp0Jh3E2aFyK7YF9y6X HxzDWu3QvfNJvWwzqKz3gBNGlPRfhYvY1grCSpUU= From: =?UTF-8?q?C=C3=A9sar=20Belley?= To: qemu-devel@nongnu.org Subject: [PATCH v2 13/13] hw/usb: Add U2F device autoscan to passthru mode Date: Mon, 24 Aug 2020 13:48:27 +0200 Message-Id: <20200824114827.81623-14-cesar.belley@lse.epita.fr> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200824114827.81623-1-cesar.belley@lse.epita.fr> References: <20200824114827.81623-1-cesar.belley@lse.epita.fr> MIME-Version: 1.0 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=163.5.55.20; envelope-from=srs0=45nv=cc=lse.epita.fr=cesar.belley@cri.epita.fr; helo=mail-2.srv.cri.epita.fr X-detected-operating-system: by eggs.gnu.org: First seen = 2020/08/24 07:49:04 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] X-Spam_score_int: -16 X-Spam_score: -1.7 X-Spam_bar: - X-Spam_report: (-1.7 / 5.0 requ) BAYES_00=-1.9, DKIM_INVALID=0.1, DKIM_SIGNED=0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no 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?C=C3=A9sar=20Belley?= , kraxel@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Signed-off-by: C=C3=A9sar Belley --- docs/u2f.txt | 9 ++++ hw/usb/meson.build | 2 +- hw/usb/u2f-passthru.c | 113 +++++++++++++++++++++++++++++++++++++----- 3 files changed, 110 insertions(+), 14 deletions(-) diff --git a/docs/u2f.txt b/docs/u2f.txt index f60052882e..8f44994818 100644 --- a/docs/u2f.txt +++ b/docs/u2f.txt @@ -42,6 +42,10 @@ on libu2f-emu: configuring and building: =20 ./configure --enable-u2f && make =20 +The pass-through mode is built by default on Linux. To take advantage +of the autoscan option it provides, make sure you have a working libudev +installed on the host. + =20 3. Using u2f-emulated =20 @@ -90,6 +94,11 @@ On the host specify the u2f-passthru device with a suita= ble hidraw: =20 qemu -usb -device u2f-passthru,hidraw=3D/dev/hidraw0 =20 +Alternately, the u2f-passthru device can autoscan to take the first +U2F device it finds on the host (this requires a working libudev): + + qemu -usb -device u2f-passthru + =20 5. Libu2f-emu =20 diff --git a/hw/usb/meson.build b/hw/usb/meson.build index cac7639924..40c283ceb5 100644 --- a/hw/usb/meson.build +++ b/hw/usb/meson.build @@ -52,7 +52,7 @@ endif =20 # U2F softmmu_ss.add(when: 'CONFIG_USB_U2F', if_true: files('u2f.c')) -softmmu_ss.add(when: ['CONFIG_LINUX', 'CONFIG_USB_U2F'], if_true: files('u= 2f-passthru.c')) +softmmu_ss.add(when: ['CONFIG_LINUX', 'CONFIG_USB_U2F'], if_true: [libudev= , files('u2f-passthru.c')]) softmmu_ss.add(when: ['CONFIG_U2F', 'CONFIG_USB_U2F'], if_true: [u2f, file= s('u2f-emulated.c')]) =20 # usb redirect diff --git a/hw/usb/u2f-passthru.c b/hw/usb/u2f-passthru.c index f8771966c7..1311530ee5 100644 --- a/hw/usb/u2f-passthru.c +++ b/hw/usb/u2f-passthru.c @@ -378,6 +378,84 @@ static bool u2f_passthru_is_u2f_device(int fd) sizeof(u2f_hid_report_desc_header)) =3D=3D 0; } =20 +#ifdef CONFIG_LIBUDEV +static int u2f_passthru_open_from_device(struct udev_device *device) +{ + const char *devnode =3D udev_device_get_devnode(device); + + int fd =3D qemu_open(devnode, O_RDWR); + if (fd < 0) { + return -1; + } else if (!u2f_passthru_is_u2f_device(fd)) { + qemu_close(fd); + return -1; + } + return fd; +} + +static int u2f_passthru_open_from_enumerate(struct udev *udev, + struct udev_enumerate *enumera= te) +{ + struct udev_list_entry *devices, *entry; + int ret, fd; + + ret =3D udev_enumerate_scan_devices(enumerate); + if (ret < 0) { + return -1; + } + + devices =3D udev_enumerate_get_list_entry(enumerate); + udev_list_entry_foreach(entry, devices) { + struct udev_device *device; + const char *syspath =3D udev_list_entry_get_name(entry); + + if (syspath =3D=3D NULL) { + continue; + } + + device =3D udev_device_new_from_syspath(udev, syspath); + if (device =3D=3D NULL) { + continue; + } + + fd =3D u2f_passthru_open_from_device(device); + udev_device_unref(device); + if (fd >=3D 0) { + return fd; + } + } + return -1; +} + +static int u2f_passthru_open_from_scan(void) +{ + struct udev *udev; + struct udev_enumerate *enumerate; + int ret, fd =3D -1; + + udev =3D udev_new(); + if (udev =3D=3D NULL) { + return -1; + } + + enumerate =3D udev_enumerate_new(udev); + if (enumerate =3D=3D NULL) { + udev_unref(udev); + return -1; + } + + ret =3D udev_enumerate_add_match_subsystem(enumerate, "hidraw"); + if (ret >=3D 0) { + fd =3D u2f_passthru_open_from_enumerate(udev, enumerate); + } + + udev_enumerate_unref(enumerate); + udev_unref(udev); + + return fd; +} +#endif + static void u2f_passthru_unrealize(U2FKeyState *base) { U2FPassthruState *key =3D PASSTHRU_U2F_KEY(base); @@ -392,22 +470,31 @@ static void u2f_passthru_realize(U2FKeyState *base, E= rror **errp) int fd; =20 if (key->hidraw =3D=3D NULL) { +#ifdef CONFIG_LIBUDEV + fd =3D u2f_passthru_open_from_scan(); + if (fd < 0) { + error_setg(errp, "%s: Failed to find a U2F USB device", + TYPE_U2F_PASSTHRU); + return; + } +#else error_setg(errp, "%s: Missing hidraw", TYPE_U2F_PASSTHRU); return; - } - - fd =3D qemu_open(key->hidraw, O_RDWR); - if (fd < 0) { - error_setg(errp, "%s: Failed to open %s", TYPE_U2F_PASSTHRU, - key->hidraw); - return; - } +#endif + } else { + fd =3D qemu_open(key->hidraw, O_RDWR); + if (fd < 0) { + error_setg(errp, "%s: Failed to open %s", TYPE_U2F_PASSTHRU, + key->hidraw); + return; + } =20 - if (!u2f_passthru_is_u2f_device(fd)) { - qemu_close(fd); - error_setg(errp, "%s: Passed hidraw does not represent " - "a U2F HID device", TYPE_U2F_PASSTHRU); - return; + if (!u2f_passthru_is_u2f_device(fd)) { + qemu_close(fd); + error_setg(errp, "%s: Passed hidraw does not represent " + "a U2F HID device", TYPE_U2F_PASSTHRU); + return; + } } key->hidraw_fd =3D fd; u2f_passthru_reset(key); --=20 2.28.0