Path: blob/main/contrib/libfido2/src/hid_freebsd.c
39478 views
/*1* Copyright (c) 2020-2022 Yubico AB. All rights reserved.2* Use of this source code is governed by a BSD-style3* license that can be found in the LICENSE file.4* SPDX-License-Identifier: BSD-2-Clause5*/67#include <sys/param.h>89#include <dev/usb/usb_ioctl.h>10#include <dev/usb/usbhid.h>11#if __FreeBSD_version >= 130050012#include <dev/hid/hidraw.h>13#define USE_HIDRAW /* see usbhid(4) and hidraw(4) on FreeBSD 13+ */14#endif1516#include <errno.h>17#include <unistd.h>1819#include "fido.h"2021#if defined(__MidnightBSD__)22#define UHID_VENDOR "MidnightBSD"23#else24#define UHID_VENDOR "FreeBSD"25#endif2627#define MAX_UHID 642829struct hid_freebsd {30int fd;31size_t report_in_len;32size_t report_out_len;33sigset_t sigmask;34const sigset_t *sigmaskp;35};3637static bool38is_fido(int fd)39{40char buf[64];41struct usb_gen_descriptor ugd;42uint32_t usage_page = 0;4344memset(&buf, 0, sizeof(buf));45memset(&ugd, 0, sizeof(ugd));4647ugd.ugd_report_type = UHID_FEATURE_REPORT;48ugd.ugd_data = buf;49ugd.ugd_maxlen = sizeof(buf);5051if (ioctl(fd, IOCTL_REQ(USB_GET_REPORT_DESC), &ugd) == -1) {52fido_log_error(errno, "%s: ioctl", __func__);53return (false);54}55if (ugd.ugd_actlen > sizeof(buf) || fido_hid_get_usage(ugd.ugd_data,56ugd.ugd_actlen, &usage_page) < 0) {57fido_log_debug("%s: fido_hid_get_usage", __func__);58return (false);59}6061return (usage_page == 0xf1d0);62}6364#ifdef USE_HIDRAW65static int66copy_info_hidraw(fido_dev_info_t *di, const char *path)67{68int fd = -1;69int ok = -1;70struct usb_device_info udi;71struct hidraw_devinfo devinfo;72char rawname[129];7374memset(di, 0, sizeof(*di));75memset(&udi, 0, sizeof(udi));76memset(&devinfo, 0, sizeof(devinfo));77memset(rawname, 0, sizeof(rawname));7879if ((fd = fido_hid_unix_open(path)) == -1 || is_fido(fd) == 0)80goto fail;8182if (ioctl(fd, IOCTL_REQ(USB_GET_DEVICEINFO), &udi) == -1) {83if (ioctl(fd, IOCTL_REQ(HIDIOCGRAWINFO), &devinfo) == -1 ||84ioctl(fd, IOCTL_REQ(HIDIOCGRAWNAME(128)), rawname) == -1 ||85(di->path = strdup(path)) == NULL ||86(di->manufacturer = strdup(UHID_VENDOR)) == NULL ||87(di->product = strdup(rawname)) == NULL)88goto fail;89di->vendor_id = devinfo.vendor;90di->product_id = devinfo.product;91} else {92if ((di->path = strdup(path)) == NULL ||93(di->manufacturer = strdup(udi.udi_vendor)) == NULL ||94(di->product = strdup(udi.udi_product)) == NULL)95goto fail;96di->vendor_id = (int16_t)udi.udi_vendorNo;97di->product_id = (int16_t)udi.udi_productNo;98}99100ok = 0;101fail:102if (fd != -1 && close(fd) == -1)103fido_log_error(errno, "%s: close %s", __func__, path);104105if (ok < 0) {106free(di->path);107free(di->manufacturer);108free(di->product);109explicit_bzero(di, sizeof(*di));110}111112return (ok);113}114#endif /* USE_HIDRAW */115116static int117copy_info_uhid(fido_dev_info_t *di, const char *path)118{119int fd = -1;120int ok = -1;121struct usb_device_info udi;122123memset(di, 0, sizeof(*di));124memset(&udi, 0, sizeof(udi));125126if ((fd = fido_hid_unix_open(path)) == -1 || is_fido(fd) == 0)127goto fail;128129if (ioctl(fd, IOCTL_REQ(USB_GET_DEVICEINFO), &udi) == -1) {130fido_log_error(errno, "%s: ioctl", __func__);131strlcpy(udi.udi_vendor, UHID_VENDOR, sizeof(udi.udi_vendor));132strlcpy(udi.udi_product, "uhid(4)", sizeof(udi.udi_product));133udi.udi_vendorNo = 0x0b5d; /* stolen from PCI_VENDOR_OPENBSD */134}135136if ((di->path = strdup(path)) == NULL ||137(di->manufacturer = strdup(udi.udi_vendor)) == NULL ||138(di->product = strdup(udi.udi_product)) == NULL)139goto fail;140di->vendor_id = (int16_t)udi.udi_vendorNo;141di->product_id = (int16_t)udi.udi_productNo;142143ok = 0;144fail:145if (fd != -1 && close(fd) == -1)146fido_log_error(errno, "%s: close %s", __func__, path);147148if (ok < 0) {149free(di->path);150free(di->manufacturer);151free(di->product);152explicit_bzero(di, sizeof(*di));153}154155return (ok);156}157158int159fido_hid_manifest(fido_dev_info_t *devlist, size_t ilen, size_t *olen)160{161char path[64];162size_t i;163164if (ilen == 0)165return (FIDO_OK); /* nothing to do */166167if (devlist == NULL || olen == NULL)168return (FIDO_ERR_INVALID_ARGUMENT);169170*olen = 0;171172#ifdef USE_HIDRAW173for (i = 0; i < MAX_UHID && *olen < ilen; i++) {174snprintf(path, sizeof(path), "/dev/hidraw%zu", i);175if (copy_info_hidraw(&devlist[*olen], path) == 0) {176devlist[*olen].io = (fido_dev_io_t) {177fido_hid_open,178fido_hid_close,179fido_hid_read,180fido_hid_write,181};182++(*olen);183}184}185/* hidraw(4) is preferred over uhid(4) */186if (*olen != 0)187return (FIDO_OK);188#endif /* USE_HIDRAW */189190for (i = 0; i < MAX_UHID && *olen < ilen; i++) {191snprintf(path, sizeof(path), "/dev/uhid%zu", i);192if (copy_info_uhid(&devlist[*olen], path) == 0) {193devlist[*olen].io = (fido_dev_io_t) {194fido_hid_open,195fido_hid_close,196fido_hid_read,197fido_hid_write,198};199++(*olen);200}201}202203return (FIDO_OK);204}205206void *207fido_hid_open(const char *path)208{209char buf[64];210struct hid_freebsd *ctx;211struct usb_gen_descriptor ugd;212int r;213214memset(&buf, 0, sizeof(buf));215memset(&ugd, 0, sizeof(ugd));216217if ((ctx = calloc(1, sizeof(*ctx))) == NULL)218return (NULL);219220if ((ctx->fd = fido_hid_unix_open(path)) == -1) {221free(ctx);222return (NULL);223}224225ugd.ugd_report_type = UHID_FEATURE_REPORT;226ugd.ugd_data = buf;227ugd.ugd_maxlen = sizeof(buf);228229/*230* N.B. if ctx->fd is an hidraw(4) device, the ioctl() below puts it in231* uhid(4) compat mode, which we need to keep fido_hid_write() as-is.232*/233if ((r = ioctl(ctx->fd, IOCTL_REQ(USB_GET_REPORT_DESC), &ugd) == -1) ||234ugd.ugd_actlen > sizeof(buf) ||235fido_hid_get_report_len(ugd.ugd_data, ugd.ugd_actlen,236&ctx->report_in_len, &ctx->report_out_len) < 0) {237if (r == -1)238fido_log_error(errno, "%s: ioctl", __func__);239fido_log_debug("%s: using default report sizes", __func__);240ctx->report_in_len = CTAP_MAX_REPORT_LEN;241ctx->report_out_len = CTAP_MAX_REPORT_LEN;242}243244return (ctx);245}246247void248fido_hid_close(void *handle)249{250struct hid_freebsd *ctx = handle;251252if (close(ctx->fd) == -1)253fido_log_error(errno, "%s: close", __func__);254255free(ctx);256}257258int259fido_hid_set_sigmask(void *handle, const fido_sigset_t *sigmask)260{261struct hid_freebsd *ctx = handle;262263ctx->sigmask = *sigmask;264ctx->sigmaskp = &ctx->sigmask;265266return (FIDO_OK);267}268269int270fido_hid_read(void *handle, unsigned char *buf, size_t len, int ms)271{272struct hid_freebsd *ctx = handle;273ssize_t r;274275if (len != ctx->report_in_len) {276fido_log_debug("%s: len %zu", __func__, len);277return (-1);278}279280if (fido_hid_unix_wait(ctx->fd, ms, ctx->sigmaskp) < 0) {281fido_log_debug("%s: fd not ready", __func__);282return (-1);283}284285if ((r = read(ctx->fd, buf, len)) == -1) {286fido_log_error(errno, "%s: read", __func__);287return (-1);288}289290if (r < 0 || (size_t)r != len) {291fido_log_debug("%s: %zd != %zu", __func__, r, len);292return (-1);293}294295return ((int)r);296}297298int299fido_hid_write(void *handle, const unsigned char *buf, size_t len)300{301struct hid_freebsd *ctx = handle;302ssize_t r;303304if (len != ctx->report_out_len + 1) {305fido_log_debug("%s: len %zu", __func__, len);306return (-1);307}308309if ((r = write(ctx->fd, buf + 1, len - 1)) == -1) {310fido_log_error(errno, "%s: write", __func__);311return (-1);312}313314if (r < 0 || (size_t)r != len - 1) {315fido_log_debug("%s: %zd != %zu", __func__, r, len - 1);316return (-1);317}318319return ((int)len);320}321322size_t323fido_hid_report_in_len(void *handle)324{325struct hid_freebsd *ctx = handle;326327return (ctx->report_in_len);328}329330size_t331fido_hid_report_out_len(void *handle)332{333struct hid_freebsd *ctx = handle;334335return (ctx->report_out_len);336}337338339