xref: /freebsd/contrib/libfido2/src/hid_freebsd.c (revision 2ccfa855b2fc331819953e3de1b1c15ce5b95a7e)
10afa8e06SEd Maste /*
21843dfb0SEd Maste  * Copyright (c) 2020-2022 Yubico AB. All rights reserved.
30afa8e06SEd Maste  * Use of this source code is governed by a BSD-style
40afa8e06SEd Maste  * license that can be found in the LICENSE file.
5*2ccfa855SEd Maste  * SPDX-License-Identifier: BSD-2-Clause
60afa8e06SEd Maste  */
70afa8e06SEd Maste 
81843dfb0SEd Maste #include <sys/param.h>
90afa8e06SEd Maste 
100afa8e06SEd Maste #include <dev/usb/usb_ioctl.h>
110afa8e06SEd Maste #include <dev/usb/usbhid.h>
121843dfb0SEd Maste #if __FreeBSD_version >= 1300500
131843dfb0SEd Maste #include <dev/hid/hidraw.h>
141843dfb0SEd Maste #define USE_HIDRAW /* see usbhid(4) and hidraw(4) on FreeBSD 13+ */
151843dfb0SEd Maste #endif
160afa8e06SEd Maste 
170afa8e06SEd Maste #include <errno.h>
180afa8e06SEd Maste #include <unistd.h>
190afa8e06SEd Maste 
200afa8e06SEd Maste #include "fido.h"
210afa8e06SEd Maste 
22f540a430SEd Maste #if defined(__MidnightBSD__)
23f540a430SEd Maste #define UHID_VENDOR    "MidnightBSD"
24f540a430SEd Maste #else
25f540a430SEd Maste #define UHID_VENDOR    "FreeBSD"
26f540a430SEd Maste #endif
27f540a430SEd Maste 
280afa8e06SEd Maste #define MAX_UHID	64
290afa8e06SEd Maste 
300afa8e06SEd Maste struct hid_freebsd {
310afa8e06SEd Maste 	int             fd;
320afa8e06SEd Maste 	size_t          report_in_len;
330afa8e06SEd Maste 	size_t          report_out_len;
340afa8e06SEd Maste 	sigset_t        sigmask;
350afa8e06SEd Maste 	const sigset_t *sigmaskp;
360afa8e06SEd Maste };
370afa8e06SEd Maste 
380afa8e06SEd Maste static bool
is_fido(int fd)390afa8e06SEd Maste is_fido(int fd)
400afa8e06SEd Maste {
410afa8e06SEd Maste 	char				buf[64];
420afa8e06SEd Maste 	struct usb_gen_descriptor	ugd;
430afa8e06SEd Maste 	uint32_t			usage_page = 0;
440afa8e06SEd Maste 
450afa8e06SEd Maste 	memset(&buf, 0, sizeof(buf));
460afa8e06SEd Maste 	memset(&ugd, 0, sizeof(ugd));
470afa8e06SEd Maste 
480afa8e06SEd Maste 	ugd.ugd_report_type = UHID_FEATURE_REPORT;
490afa8e06SEd Maste 	ugd.ugd_data = buf;
500afa8e06SEd Maste 	ugd.ugd_maxlen = sizeof(buf);
510afa8e06SEd Maste 
520afa8e06SEd Maste 	if (ioctl(fd, IOCTL_REQ(USB_GET_REPORT_DESC), &ugd) == -1) {
530afa8e06SEd Maste 		fido_log_error(errno, "%s: ioctl", __func__);
540afa8e06SEd Maste 		return (false);
550afa8e06SEd Maste 	}
560afa8e06SEd Maste 	if (ugd.ugd_actlen > sizeof(buf) || fido_hid_get_usage(ugd.ugd_data,
570afa8e06SEd Maste 	    ugd.ugd_actlen, &usage_page) < 0) {
580afa8e06SEd Maste 		fido_log_debug("%s: fido_hid_get_usage", __func__);
590afa8e06SEd Maste 		return (false);
600afa8e06SEd Maste 	}
610afa8e06SEd Maste 
620afa8e06SEd Maste 	return (usage_page == 0xf1d0);
630afa8e06SEd Maste }
640afa8e06SEd Maste 
651843dfb0SEd Maste #ifdef USE_HIDRAW
660afa8e06SEd Maste static int
copy_info_hidraw(fido_dev_info_t * di,const char * path)671843dfb0SEd Maste copy_info_hidraw(fido_dev_info_t *di, const char *path)
681843dfb0SEd Maste {
691843dfb0SEd Maste 	int			fd = -1;
701843dfb0SEd Maste 	int			ok = -1;
711843dfb0SEd Maste 	struct usb_device_info	udi;
721843dfb0SEd Maste 	struct hidraw_devinfo	devinfo;
731843dfb0SEd Maste 	char			rawname[129];
741843dfb0SEd Maste 
751843dfb0SEd Maste 	memset(di, 0, sizeof(*di));
761843dfb0SEd Maste 	memset(&udi, 0, sizeof(udi));
771843dfb0SEd Maste 	memset(&devinfo, 0, sizeof(devinfo));
781843dfb0SEd Maste 	memset(rawname, 0, sizeof(rawname));
791843dfb0SEd Maste 
801843dfb0SEd Maste 	if ((fd = fido_hid_unix_open(path)) == -1 || is_fido(fd) == 0)
811843dfb0SEd Maste 		goto fail;
821843dfb0SEd Maste 
831843dfb0SEd Maste 	if (ioctl(fd, IOCTL_REQ(USB_GET_DEVICEINFO), &udi) == -1) {
841843dfb0SEd Maste 		if (ioctl(fd, IOCTL_REQ(HIDIOCGRAWINFO), &devinfo) == -1 ||
851843dfb0SEd Maste 		    ioctl(fd, IOCTL_REQ(HIDIOCGRAWNAME(128)), rawname) == -1 ||
861843dfb0SEd Maste 		    (di->path = strdup(path)) == NULL ||
871843dfb0SEd Maste 		    (di->manufacturer = strdup(UHID_VENDOR)) == NULL ||
881843dfb0SEd Maste 		    (di->product = strdup(rawname)) == NULL)
891843dfb0SEd Maste 			goto fail;
901843dfb0SEd Maste 		di->vendor_id = devinfo.vendor;
911843dfb0SEd Maste 		di->product_id = devinfo.product;
921843dfb0SEd Maste 	} else {
931843dfb0SEd Maste 		if ((di->path = strdup(path)) == NULL ||
941843dfb0SEd Maste 		    (di->manufacturer = strdup(udi.udi_vendor)) == NULL ||
951843dfb0SEd Maste 		    (di->product = strdup(udi.udi_product)) == NULL)
961843dfb0SEd Maste 			goto fail;
971843dfb0SEd Maste 		di->vendor_id = (int16_t)udi.udi_vendorNo;
981843dfb0SEd Maste 		di->product_id = (int16_t)udi.udi_productNo;
991843dfb0SEd Maste 	}
1001843dfb0SEd Maste 
1011843dfb0SEd Maste 	ok = 0;
1021843dfb0SEd Maste fail:
1031843dfb0SEd Maste 	if (fd != -1 && close(fd) == -1)
1041843dfb0SEd Maste 		fido_log_error(errno, "%s: close %s", __func__, path);
1051843dfb0SEd Maste 
1061843dfb0SEd Maste 	if (ok < 0) {
1071843dfb0SEd Maste 		free(di->path);
1081843dfb0SEd Maste 		free(di->manufacturer);
1091843dfb0SEd Maste 		free(di->product);
1101843dfb0SEd Maste 		explicit_bzero(di, sizeof(*di));
1111843dfb0SEd Maste 	}
1121843dfb0SEd Maste 
1131843dfb0SEd Maste 	return (ok);
1141843dfb0SEd Maste }
1151843dfb0SEd Maste #endif /* USE_HIDRAW */
1161843dfb0SEd Maste 
1171843dfb0SEd Maste static int
copy_info_uhid(fido_dev_info_t * di,const char * path)1181843dfb0SEd Maste copy_info_uhid(fido_dev_info_t *di, const char *path)
1190afa8e06SEd Maste {
1200afa8e06SEd Maste 	int			fd = -1;
1210afa8e06SEd Maste 	int			ok = -1;
1220afa8e06SEd Maste 	struct usb_device_info	udi;
1230afa8e06SEd Maste 
1240afa8e06SEd Maste 	memset(di, 0, sizeof(*di));
1250afa8e06SEd Maste 	memset(&udi, 0, sizeof(udi));
1260afa8e06SEd Maste 
1270afa8e06SEd Maste 	if ((fd = fido_hid_unix_open(path)) == -1 || is_fido(fd) == 0)
1280afa8e06SEd Maste 		goto fail;
1290afa8e06SEd Maste 
1300afa8e06SEd Maste 	if (ioctl(fd, IOCTL_REQ(USB_GET_DEVICEINFO), &udi) == -1) {
1310afa8e06SEd Maste 		fido_log_error(errno, "%s: ioctl", __func__);
132f540a430SEd Maste 		strlcpy(udi.udi_vendor, UHID_VENDOR, sizeof(udi.udi_vendor));
1330afa8e06SEd Maste 		strlcpy(udi.udi_product, "uhid(4)", sizeof(udi.udi_product));
1340afa8e06SEd Maste 		udi.udi_vendorNo = 0x0b5d; /* stolen from PCI_VENDOR_OPENBSD */
1350afa8e06SEd Maste 	}
1360afa8e06SEd Maste 
1370afa8e06SEd Maste 	if ((di->path = strdup(path)) == NULL ||
1380afa8e06SEd Maste 	    (di->manufacturer = strdup(udi.udi_vendor)) == NULL ||
1390afa8e06SEd Maste 	    (di->product = strdup(udi.udi_product)) == NULL)
1400afa8e06SEd Maste 		goto fail;
1410afa8e06SEd Maste 	di->vendor_id = (int16_t)udi.udi_vendorNo;
1420afa8e06SEd Maste 	di->product_id = (int16_t)udi.udi_productNo;
1430afa8e06SEd Maste 
1440afa8e06SEd Maste 	ok = 0;
1450afa8e06SEd Maste fail:
1461843dfb0SEd Maste 	if (fd != -1 && close(fd) == -1)
1471843dfb0SEd Maste 		fido_log_error(errno, "%s: close %s", __func__, path);
1480afa8e06SEd Maste 
1490afa8e06SEd Maste 	if (ok < 0) {
1500afa8e06SEd Maste 		free(di->path);
1510afa8e06SEd Maste 		free(di->manufacturer);
1520afa8e06SEd Maste 		free(di->product);
1530afa8e06SEd Maste 		explicit_bzero(di, sizeof(*di));
1540afa8e06SEd Maste 	}
1550afa8e06SEd Maste 
1560afa8e06SEd Maste 	return (ok);
1570afa8e06SEd Maste }
1580afa8e06SEd Maste 
1590afa8e06SEd Maste int
fido_hid_manifest(fido_dev_info_t * devlist,size_t ilen,size_t * olen)1600afa8e06SEd Maste fido_hid_manifest(fido_dev_info_t *devlist, size_t ilen, size_t *olen)
1610afa8e06SEd Maste {
1620afa8e06SEd Maste 	char	path[64];
1630afa8e06SEd Maste 	size_t	i;
1640afa8e06SEd Maste 
1650afa8e06SEd Maste 	if (ilen == 0)
1660afa8e06SEd Maste 		return (FIDO_OK); /* nothing to do */
1670afa8e06SEd Maste 
1680afa8e06SEd Maste 	if (devlist == NULL || olen == NULL)
1690afa8e06SEd Maste 		return (FIDO_ERR_INVALID_ARGUMENT);
1700afa8e06SEd Maste 
1711843dfb0SEd Maste 	*olen = 0;
1721843dfb0SEd Maste 
1731843dfb0SEd Maste #ifdef USE_HIDRAW
1741843dfb0SEd Maste 	for (i = 0; i < MAX_UHID && *olen < ilen; i++) {
1751843dfb0SEd Maste 		snprintf(path, sizeof(path), "/dev/hidraw%zu", i);
1761843dfb0SEd Maste 		if (copy_info_hidraw(&devlist[*olen], path) == 0) {
1771843dfb0SEd Maste 			devlist[*olen].io = (fido_dev_io_t) {
1781843dfb0SEd Maste 				fido_hid_open,
1791843dfb0SEd Maste 				fido_hid_close,
1801843dfb0SEd Maste 				fido_hid_read,
1811843dfb0SEd Maste 				fido_hid_write,
1821843dfb0SEd Maste 			};
1831843dfb0SEd Maste 			++(*olen);
1841843dfb0SEd Maste 		}
1851843dfb0SEd Maste 	}
1861843dfb0SEd Maste 	/* hidraw(4) is preferred over uhid(4) */
1871843dfb0SEd Maste 	if (*olen != 0)
1881843dfb0SEd Maste 		return (FIDO_OK);
1891843dfb0SEd Maste #endif /* USE_HIDRAW */
1901843dfb0SEd Maste 
1911843dfb0SEd Maste 	for (i = 0; i < MAX_UHID && *olen < ilen; i++) {
1920afa8e06SEd Maste 		snprintf(path, sizeof(path), "/dev/uhid%zu", i);
1931843dfb0SEd Maste 		if (copy_info_uhid(&devlist[*olen], path) == 0) {
1940afa8e06SEd Maste 			devlist[*olen].io = (fido_dev_io_t) {
1950afa8e06SEd Maste 				fido_hid_open,
1960afa8e06SEd Maste 				fido_hid_close,
1970afa8e06SEd Maste 				fido_hid_read,
1980afa8e06SEd Maste 				fido_hid_write,
1990afa8e06SEd Maste 			};
2000afa8e06SEd Maste 			++(*olen);
2010afa8e06SEd Maste 		}
2020afa8e06SEd Maste 	}
2030afa8e06SEd Maste 
2040afa8e06SEd Maste 	return (FIDO_OK);
2050afa8e06SEd Maste }
2060afa8e06SEd Maste 
2070afa8e06SEd Maste void *
fido_hid_open(const char * path)2080afa8e06SEd Maste fido_hid_open(const char *path)
2090afa8e06SEd Maste {
2100afa8e06SEd Maste 	char				 buf[64];
2110afa8e06SEd Maste 	struct hid_freebsd		*ctx;
2120afa8e06SEd Maste 	struct usb_gen_descriptor	 ugd;
2130afa8e06SEd Maste 	int				 r;
2140afa8e06SEd Maste 
2150afa8e06SEd Maste 	memset(&buf, 0, sizeof(buf));
2160afa8e06SEd Maste 	memset(&ugd, 0, sizeof(ugd));
2170afa8e06SEd Maste 
2180afa8e06SEd Maste 	if ((ctx = calloc(1, sizeof(*ctx))) == NULL)
2190afa8e06SEd Maste 		return (NULL);
2200afa8e06SEd Maste 
2210afa8e06SEd Maste 	if ((ctx->fd = fido_hid_unix_open(path)) == -1) {
2220afa8e06SEd Maste 		free(ctx);
2230afa8e06SEd Maste 		return (NULL);
2240afa8e06SEd Maste 	}
2250afa8e06SEd Maste 
2260afa8e06SEd Maste 	ugd.ugd_report_type = UHID_FEATURE_REPORT;
2270afa8e06SEd Maste 	ugd.ugd_data = buf;
2280afa8e06SEd Maste 	ugd.ugd_maxlen = sizeof(buf);
2290afa8e06SEd Maste 
2301843dfb0SEd Maste 	/*
2311843dfb0SEd Maste 	 * N.B. if ctx->fd is an hidraw(4) device, the ioctl() below puts it in
2321843dfb0SEd Maste 	 * uhid(4) compat mode, which we need to keep fido_hid_write() as-is.
2331843dfb0SEd Maste 	 */
2340afa8e06SEd Maste 	if ((r = ioctl(ctx->fd, IOCTL_REQ(USB_GET_REPORT_DESC), &ugd) == -1) ||
2350afa8e06SEd Maste 	    ugd.ugd_actlen > sizeof(buf) ||
2360afa8e06SEd Maste 	    fido_hid_get_report_len(ugd.ugd_data, ugd.ugd_actlen,
2370afa8e06SEd Maste 	    &ctx->report_in_len, &ctx->report_out_len) < 0) {
2380afa8e06SEd Maste 		if (r == -1)
2390afa8e06SEd Maste 			fido_log_error(errno, "%s: ioctl", __func__);
2400afa8e06SEd Maste 		fido_log_debug("%s: using default report sizes", __func__);
2410afa8e06SEd Maste 		ctx->report_in_len = CTAP_MAX_REPORT_LEN;
2420afa8e06SEd Maste 		ctx->report_out_len = CTAP_MAX_REPORT_LEN;
2430afa8e06SEd Maste 	}
2440afa8e06SEd Maste 
2450afa8e06SEd Maste 	return (ctx);
2460afa8e06SEd Maste }
2470afa8e06SEd Maste 
2480afa8e06SEd Maste void
fido_hid_close(void * handle)2490afa8e06SEd Maste fido_hid_close(void *handle)
2500afa8e06SEd Maste {
2510afa8e06SEd Maste 	struct hid_freebsd *ctx = handle;
2520afa8e06SEd Maste 
2530afa8e06SEd Maste 	if (close(ctx->fd) == -1)
2540afa8e06SEd Maste 		fido_log_error(errno, "%s: close", __func__);
2550afa8e06SEd Maste 
2560afa8e06SEd Maste 	free(ctx);
2570afa8e06SEd Maste }
2580afa8e06SEd Maste 
2590afa8e06SEd Maste int
fido_hid_set_sigmask(void * handle,const fido_sigset_t * sigmask)2600afa8e06SEd Maste fido_hid_set_sigmask(void *handle, const fido_sigset_t *sigmask)
2610afa8e06SEd Maste {
2620afa8e06SEd Maste 	struct hid_freebsd *ctx = handle;
2630afa8e06SEd Maste 
2640afa8e06SEd Maste 	ctx->sigmask = *sigmask;
2650afa8e06SEd Maste 	ctx->sigmaskp = &ctx->sigmask;
2660afa8e06SEd Maste 
2670afa8e06SEd Maste 	return (FIDO_OK);
2680afa8e06SEd Maste }
2690afa8e06SEd Maste 
2700afa8e06SEd Maste int
fido_hid_read(void * handle,unsigned char * buf,size_t len,int ms)2710afa8e06SEd Maste fido_hid_read(void *handle, unsigned char *buf, size_t len, int ms)
2720afa8e06SEd Maste {
2730afa8e06SEd Maste 	struct hid_freebsd	*ctx = handle;
2740afa8e06SEd Maste 	ssize_t			 r;
2750afa8e06SEd Maste 
2760afa8e06SEd Maste 	if (len != ctx->report_in_len) {
2770afa8e06SEd Maste 		fido_log_debug("%s: len %zu", __func__, len);
2780afa8e06SEd Maste 		return (-1);
2790afa8e06SEd Maste 	}
2800afa8e06SEd Maste 
2810afa8e06SEd Maste 	if (fido_hid_unix_wait(ctx->fd, ms, ctx->sigmaskp) < 0) {
2820afa8e06SEd Maste 		fido_log_debug("%s: fd not ready", __func__);
2830afa8e06SEd Maste 		return (-1);
2840afa8e06SEd Maste 	}
2850afa8e06SEd Maste 
2860afa8e06SEd Maste 	if ((r = read(ctx->fd, buf, len)) == -1) {
2870afa8e06SEd Maste 		fido_log_error(errno, "%s: read", __func__);
2880afa8e06SEd Maste 		return (-1);
2890afa8e06SEd Maste 	}
2900afa8e06SEd Maste 
2910afa8e06SEd Maste 	if (r < 0 || (size_t)r != len) {
2920afa8e06SEd Maste 		fido_log_debug("%s: %zd != %zu", __func__, r, len);
2930afa8e06SEd Maste 		return (-1);
2940afa8e06SEd Maste 	}
2950afa8e06SEd Maste 
2960afa8e06SEd Maste 	return ((int)r);
2970afa8e06SEd Maste }
2980afa8e06SEd Maste 
2990afa8e06SEd Maste int
fido_hid_write(void * handle,const unsigned char * buf,size_t len)3000afa8e06SEd Maste fido_hid_write(void *handle, const unsigned char *buf, size_t len)
3010afa8e06SEd Maste {
3020afa8e06SEd Maste 	struct hid_freebsd	*ctx = handle;
3030afa8e06SEd Maste 	ssize_t			 r;
3040afa8e06SEd Maste 
3050afa8e06SEd Maste 	if (len != ctx->report_out_len + 1) {
3060afa8e06SEd Maste 		fido_log_debug("%s: len %zu", __func__, len);
3070afa8e06SEd Maste 		return (-1);
3080afa8e06SEd Maste 	}
3090afa8e06SEd Maste 
3100afa8e06SEd Maste 	if ((r = write(ctx->fd, buf + 1, len - 1)) == -1) {
3110afa8e06SEd Maste 		fido_log_error(errno, "%s: write", __func__);
3120afa8e06SEd Maste 		return (-1);
3130afa8e06SEd Maste 	}
3140afa8e06SEd Maste 
3150afa8e06SEd Maste 	if (r < 0 || (size_t)r != len - 1) {
3160afa8e06SEd Maste 		fido_log_debug("%s: %zd != %zu", __func__, r, len - 1);
3170afa8e06SEd Maste 		return (-1);
3180afa8e06SEd Maste 	}
3190afa8e06SEd Maste 
3200afa8e06SEd Maste 	return ((int)len);
3210afa8e06SEd Maste }
3220afa8e06SEd Maste 
3230afa8e06SEd Maste size_t
fido_hid_report_in_len(void * handle)3240afa8e06SEd Maste fido_hid_report_in_len(void *handle)
3250afa8e06SEd Maste {
3260afa8e06SEd Maste 	struct hid_freebsd *ctx = handle;
3270afa8e06SEd Maste 
3280afa8e06SEd Maste 	return (ctx->report_in_len);
3290afa8e06SEd Maste }
3300afa8e06SEd Maste 
3310afa8e06SEd Maste size_t
fido_hid_report_out_len(void * handle)3320afa8e06SEd Maste fido_hid_report_out_len(void *handle)
3330afa8e06SEd Maste {
3340afa8e06SEd Maste 	struct hid_freebsd *ctx = handle;
3350afa8e06SEd Maste 
3360afa8e06SEd Maste 	return (ctx->report_out_len);
3370afa8e06SEd Maste }
338