xref: /freebsd/contrib/libfido2/src/hid_openbsd.c (revision 2ccfa855)
10afa8e06SEd Maste /*
20afa8e06SEd Maste  * Copyright (c) 2019 Google LLC. 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 
80afa8e06SEd Maste #include <sys/types.h>
90afa8e06SEd Maste 
100afa8e06SEd Maste #include <sys/ioctl.h>
110afa8e06SEd Maste #include <dev/usb/usb.h>
120afa8e06SEd Maste 
130afa8e06SEd Maste #include <errno.h>
140afa8e06SEd Maste #include <fcntl.h>
150afa8e06SEd Maste #include <poll.h>
160afa8e06SEd Maste #include <signal.h>
170afa8e06SEd Maste #include <unistd.h>
180afa8e06SEd Maste 
190afa8e06SEd Maste #include "fido.h"
200afa8e06SEd Maste 
210afa8e06SEd Maste #define MAX_UHID	64
220afa8e06SEd Maste 
230afa8e06SEd Maste struct hid_openbsd {
240afa8e06SEd Maste 	int fd;
250afa8e06SEd Maste 	size_t report_in_len;
260afa8e06SEd Maste 	size_t report_out_len;
27f540a430SEd Maste 	sigset_t sigmask;
28f540a430SEd Maste 	const sigset_t *sigmaskp;
290afa8e06SEd Maste };
300afa8e06SEd Maste 
31*2ccfa855SEd Maste static int
copy_info(fido_dev_info_t * di,const char * path)32*2ccfa855SEd Maste copy_info(fido_dev_info_t *di, const char *path)
33*2ccfa855SEd Maste {
34*2ccfa855SEd Maste 	int fd = -1, ok = -1;
35*2ccfa855SEd Maste 	struct usb_device_info udi;
36*2ccfa855SEd Maste 
37*2ccfa855SEd Maste 	memset(di, 0, sizeof(*di));
38*2ccfa855SEd Maste 	memset(&udi, 0, sizeof(udi));
39*2ccfa855SEd Maste 
40*2ccfa855SEd Maste 	if ((fd = fido_hid_unix_open(path)) == -1)
41*2ccfa855SEd Maste 		goto fail;
42*2ccfa855SEd Maste 	if (ioctl(fd, IOCTL_REQ(USB_GET_DEVICEINFO), &udi) == -1) {
43*2ccfa855SEd Maste 		fido_log_error(errno, "%s: ioctl %s", __func__, path);
44*2ccfa855SEd Maste 		goto fail;
45*2ccfa855SEd Maste 	}
46*2ccfa855SEd Maste 
47*2ccfa855SEd Maste 	fido_log_debug("%s: %s: bus = 0x%02x, addr = 0x%02x", __func__, path,
48*2ccfa855SEd Maste 	    udi.udi_bus, udi.udi_addr);
49*2ccfa855SEd Maste 	fido_log_debug("%s: %s: vendor = \"%s\", product = \"%s\"", __func__,
50*2ccfa855SEd Maste 	    path, udi.udi_vendor, udi.udi_product);
51*2ccfa855SEd Maste 	fido_log_debug("%s: %s: productNo = 0x%04x, vendorNo = 0x%04x, "
52*2ccfa855SEd Maste 	    "releaseNo = 0x%04x", __func__, path, udi.udi_productNo,
53*2ccfa855SEd Maste 	    udi.udi_vendorNo, udi.udi_releaseNo);
54*2ccfa855SEd Maste 
55*2ccfa855SEd Maste 	if ((di->path = strdup(path)) == NULL ||
56*2ccfa855SEd Maste 	    (di->manufacturer = strdup(udi.udi_vendor)) == NULL ||
57*2ccfa855SEd Maste 	    (di->product = strdup(udi.udi_product)) == NULL)
58*2ccfa855SEd Maste 		goto fail;
59*2ccfa855SEd Maste 
60*2ccfa855SEd Maste 	di->vendor_id = (int16_t)udi.udi_vendorNo;
61*2ccfa855SEd Maste 	di->product_id = (int16_t)udi.udi_productNo;
62*2ccfa855SEd Maste 
63*2ccfa855SEd Maste 	ok = 0;
64*2ccfa855SEd Maste fail:
65*2ccfa855SEd Maste 	if (fd != -1 && close(fd) == -1)
66*2ccfa855SEd Maste 		fido_log_error(errno, "%s: close %s", __func__, path);
67*2ccfa855SEd Maste 
68*2ccfa855SEd Maste 	if (ok < 0) {
69*2ccfa855SEd Maste 		free(di->path);
70*2ccfa855SEd Maste 		free(di->manufacturer);
71*2ccfa855SEd Maste 		free(di->product);
72*2ccfa855SEd Maste 		explicit_bzero(di, sizeof(*di));
73*2ccfa855SEd Maste 	}
74*2ccfa855SEd Maste 
75*2ccfa855SEd Maste 	return (ok);
76*2ccfa855SEd Maste }
77*2ccfa855SEd Maste 
780afa8e06SEd Maste int
fido_hid_manifest(fido_dev_info_t * devlist,size_t ilen,size_t * olen)790afa8e06SEd Maste fido_hid_manifest(fido_dev_info_t *devlist, size_t ilen, size_t *olen)
800afa8e06SEd Maste {
810afa8e06SEd Maste 	size_t i;
820afa8e06SEd Maste 	char path[64];
830afa8e06SEd Maste 
840afa8e06SEd Maste 	if (ilen == 0)
850afa8e06SEd Maste 		return (FIDO_OK); /* nothing to do */
860afa8e06SEd Maste 
870afa8e06SEd Maste 	if (devlist == NULL || olen == NULL)
880afa8e06SEd Maste 		return (FIDO_ERR_INVALID_ARGUMENT);
890afa8e06SEd Maste 
900afa8e06SEd Maste 	for (i = *olen = 0; i < MAX_UHID && *olen < ilen; i++) {
910afa8e06SEd Maste 		snprintf(path, sizeof(path), "/dev/fido/%zu", i);
92*2ccfa855SEd Maste 		if (copy_info(&devlist[*olen], path) == 0) {
93*2ccfa855SEd Maste 			devlist[*olen].io = (fido_dev_io_t) {
940afa8e06SEd Maste 				fido_hid_open,
950afa8e06SEd Maste 				fido_hid_close,
960afa8e06SEd Maste 				fido_hid_read,
970afa8e06SEd Maste 				fido_hid_write,
980afa8e06SEd Maste 			};
99*2ccfa855SEd Maste 			++(*olen);
1000afa8e06SEd Maste 		}
1010afa8e06SEd Maste 	}
1020afa8e06SEd Maste 
103*2ccfa855SEd Maste 	return (FIDO_OK);
1040afa8e06SEd Maste }
1050afa8e06SEd Maste 
1060afa8e06SEd Maste /*
1070afa8e06SEd Maste  * Workaround for OpenBSD <=6.6-current (as of 201910) bug that loses
1080afa8e06SEd Maste  * sync of DATA0/DATA1 sequence bit across uhid open/close.
1090afa8e06SEd Maste  * Send pings until we get a response - early pings with incorrect
1100afa8e06SEd Maste  * sequence bits will be ignored as duplicate packets by the device.
1110afa8e06SEd Maste  */
1120afa8e06SEd Maste static int
terrible_ping_kludge(struct hid_openbsd * ctx)1130afa8e06SEd Maste terrible_ping_kludge(struct hid_openbsd *ctx)
1140afa8e06SEd Maste {
1150afa8e06SEd Maste 	u_char data[256];
1160afa8e06SEd Maste 	int i, n;
1170afa8e06SEd Maste 	struct pollfd pfd;
1180afa8e06SEd Maste 
1190afa8e06SEd Maste 	if (sizeof(data) < ctx->report_out_len + 1)
1200afa8e06SEd Maste 		return -1;
1210afa8e06SEd Maste 	for (i = 0; i < 4; i++) {
1220afa8e06SEd Maste 		memset(data, 0, sizeof(data));
1230afa8e06SEd Maste 		/* broadcast channel ID */
1240afa8e06SEd Maste 		data[1] = 0xff;
1250afa8e06SEd Maste 		data[2] = 0xff;
1260afa8e06SEd Maste 		data[3] = 0xff;
1270afa8e06SEd Maste 		data[4] = 0xff;
1280afa8e06SEd Maste 		/* Ping command */
1290afa8e06SEd Maste 		data[5] = 0x81;
1300afa8e06SEd Maste 		/* One byte ping only, Vasili */
1310afa8e06SEd Maste 		data[6] = 0;
1320afa8e06SEd Maste 		data[7] = 1;
1330afa8e06SEd Maste 		fido_log_debug("%s: send ping %d", __func__, i);
1340afa8e06SEd Maste 		if (fido_hid_write(ctx, data, ctx->report_out_len + 1) == -1)
1350afa8e06SEd Maste 			return -1;
1360afa8e06SEd Maste 		fido_log_debug("%s: wait reply", __func__);
1370afa8e06SEd Maste 		memset(&pfd, 0, sizeof(pfd));
1380afa8e06SEd Maste 		pfd.fd = ctx->fd;
1390afa8e06SEd Maste 		pfd.events = POLLIN;
1400afa8e06SEd Maste 		if ((n = poll(&pfd, 1, 100)) == -1) {
1410afa8e06SEd Maste 			fido_log_error(errno, "%s: poll", __func__);
1420afa8e06SEd Maste 			return -1;
1430afa8e06SEd Maste 		} else if (n == 0) {
1440afa8e06SEd Maste 			fido_log_debug("%s: timed out", __func__);
1450afa8e06SEd Maste 			continue;
1460afa8e06SEd Maste 		}
1470afa8e06SEd Maste 		if (fido_hid_read(ctx, data, ctx->report_out_len, 250) == -1)
1480afa8e06SEd Maste 			return -1;
1490afa8e06SEd Maste 		/*
1500afa8e06SEd Maste 		 * Ping isn't always supported on the broadcast channel,
1510afa8e06SEd Maste 		 * so we might get an error, but we don't care - we're
1520afa8e06SEd Maste 		 * synched now.
1530afa8e06SEd Maste 		 */
1540afa8e06SEd Maste 		fido_log_xxd(data, ctx->report_out_len, "%s: got reply",
1550afa8e06SEd Maste 		    __func__);
1560afa8e06SEd Maste 		return 0;
1570afa8e06SEd Maste 	}
1580afa8e06SEd Maste 	fido_log_debug("%s: no response", __func__);
1590afa8e06SEd Maste 	return -1;
1600afa8e06SEd Maste }
1610afa8e06SEd Maste 
1620afa8e06SEd Maste void *
fido_hid_open(const char * path)1630afa8e06SEd Maste fido_hid_open(const char *path)
1640afa8e06SEd Maste {
1650afa8e06SEd Maste 	struct hid_openbsd *ret = NULL;
1660afa8e06SEd Maste 
1670afa8e06SEd Maste 	if ((ret = calloc(1, sizeof(*ret))) == NULL ||
1680afa8e06SEd Maste 	    (ret->fd = fido_hid_unix_open(path)) == -1) {
1690afa8e06SEd Maste 		free(ret);
1700afa8e06SEd Maste 		return (NULL);
1710afa8e06SEd Maste 	}
1720afa8e06SEd Maste 	ret->report_in_len = ret->report_out_len = CTAP_MAX_REPORT_LEN;
1730afa8e06SEd Maste 	fido_log_debug("%s: inlen = %zu outlen = %zu", __func__,
1740afa8e06SEd Maste 	    ret->report_in_len, ret->report_out_len);
1750afa8e06SEd Maste 
1760afa8e06SEd Maste 	/*
1770afa8e06SEd Maste 	 * OpenBSD (as of 201910) has a bug that causes it to lose
1780afa8e06SEd Maste 	 * track of the DATA0/DATA1 sequence toggle across uhid device
1790afa8e06SEd Maste 	 * open and close. This is a terrible hack to work around it.
1800afa8e06SEd Maste 	 */
1810afa8e06SEd Maste 	if (terrible_ping_kludge(ret) != 0) {
1820afa8e06SEd Maste 		fido_hid_close(ret);
1830afa8e06SEd Maste 		return NULL;
1840afa8e06SEd Maste 	}
1850afa8e06SEd Maste 
1860afa8e06SEd Maste 	return (ret);
1870afa8e06SEd Maste }
1880afa8e06SEd Maste 
1890afa8e06SEd Maste void
fido_hid_close(void * handle)1900afa8e06SEd Maste fido_hid_close(void *handle)
1910afa8e06SEd Maste {
1920afa8e06SEd Maste 	struct hid_openbsd *ctx = (struct hid_openbsd *)handle;
1930afa8e06SEd Maste 
1940afa8e06SEd Maste 	if (close(ctx->fd) == -1)
1950afa8e06SEd Maste 		fido_log_error(errno, "%s: close", __func__);
1960afa8e06SEd Maste 
1970afa8e06SEd Maste 	free(ctx);
1980afa8e06SEd Maste }
1990afa8e06SEd Maste 
2000afa8e06SEd Maste int
fido_hid_set_sigmask(void * handle,const fido_sigset_t * sigmask)2010afa8e06SEd Maste fido_hid_set_sigmask(void *handle, const fido_sigset_t *sigmask)
2020afa8e06SEd Maste {
203f540a430SEd Maste 	struct hid_openbsd *ctx = handle;
2040afa8e06SEd Maste 
205f540a430SEd Maste 	ctx->sigmask = *sigmask;
206f540a430SEd Maste 	ctx->sigmaskp = &ctx->sigmask;
207f540a430SEd Maste 
208f540a430SEd Maste 	return (FIDO_OK);
2090afa8e06SEd Maste }
2100afa8e06SEd Maste 
2110afa8e06SEd Maste int
fido_hid_read(void * handle,unsigned char * buf,size_t len,int ms)2120afa8e06SEd Maste fido_hid_read(void *handle, unsigned char *buf, size_t len, int ms)
2130afa8e06SEd Maste {
2140afa8e06SEd Maste 	struct hid_openbsd *ctx = (struct hid_openbsd *)handle;
2150afa8e06SEd Maste 	ssize_t r;
2160afa8e06SEd Maste 
2170afa8e06SEd Maste 	if (len != ctx->report_in_len) {
2180afa8e06SEd Maste 		fido_log_debug("%s: invalid len: got %zu, want %zu", __func__,
2190afa8e06SEd Maste 		    len, ctx->report_in_len);
2200afa8e06SEd Maste 		return (-1);
2210afa8e06SEd Maste 	}
2220afa8e06SEd Maste 
223f540a430SEd Maste 	if (fido_hid_unix_wait(ctx->fd, ms, ctx->sigmaskp) < 0) {
224f540a430SEd Maste 		fido_log_debug("%s: fd not ready", __func__);
225f540a430SEd Maste 		return (-1);
226f540a430SEd Maste 	}
227f540a430SEd Maste 
2280afa8e06SEd Maste 	if ((r = read(ctx->fd, buf, len)) == -1) {
2290afa8e06SEd Maste 		fido_log_error(errno, "%s: read", __func__);
2300afa8e06SEd Maste 		return (-1);
2310afa8e06SEd Maste 	}
2320afa8e06SEd Maste 
2330afa8e06SEd Maste 	if (r < 0 || (size_t)r != len) {
2340afa8e06SEd Maste 		fido_log_debug("%s: %zd != %zu", __func__, r, len);
2350afa8e06SEd Maste 		return (-1);
2360afa8e06SEd Maste 	}
2370afa8e06SEd Maste 
2380afa8e06SEd Maste 	return ((int)len);
2390afa8e06SEd Maste }
2400afa8e06SEd Maste 
2410afa8e06SEd Maste int
fido_hid_write(void * handle,const unsigned char * buf,size_t len)2420afa8e06SEd Maste fido_hid_write(void *handle, const unsigned char *buf, size_t len)
2430afa8e06SEd Maste {
2440afa8e06SEd Maste 	struct hid_openbsd *ctx = (struct hid_openbsd *)handle;
2450afa8e06SEd Maste 	ssize_t r;
2460afa8e06SEd Maste 
2470afa8e06SEd Maste 	if (len != ctx->report_out_len + 1) {
2480afa8e06SEd Maste 		fido_log_debug("%s: invalid len: got %zu, want %zu", __func__,
2490afa8e06SEd Maste 		    len, ctx->report_out_len);
2500afa8e06SEd Maste 		return (-1);
2510afa8e06SEd Maste 	}
2520afa8e06SEd Maste 
2530afa8e06SEd Maste 	if ((r = write(ctx->fd, buf + 1, len - 1)) == -1) {
2540afa8e06SEd Maste 		fido_log_error(errno, "%s: write", __func__);
2550afa8e06SEd Maste 		return (-1);
2560afa8e06SEd Maste 	}
2570afa8e06SEd Maste 
2580afa8e06SEd Maste 	if (r < 0 || (size_t)r != len - 1) {
2590afa8e06SEd Maste 		fido_log_debug("%s: %zd != %zu", __func__, r, len - 1);
2600afa8e06SEd Maste 		return (-1);
2610afa8e06SEd Maste 	}
2620afa8e06SEd Maste 
2630afa8e06SEd Maste 	return ((int)len);
2640afa8e06SEd Maste }
2650afa8e06SEd Maste 
2660afa8e06SEd Maste size_t
fido_hid_report_in_len(void * handle)2670afa8e06SEd Maste fido_hid_report_in_len(void *handle)
2680afa8e06SEd Maste {
2690afa8e06SEd Maste 	struct hid_openbsd *ctx = handle;
2700afa8e06SEd Maste 
2710afa8e06SEd Maste 	return (ctx->report_in_len);
2720afa8e06SEd Maste }
2730afa8e06SEd Maste 
2740afa8e06SEd Maste size_t
fido_hid_report_out_len(void * handle)2750afa8e06SEd Maste fido_hid_report_out_len(void *handle)
2760afa8e06SEd Maste {
2770afa8e06SEd Maste 	struct hid_openbsd *ctx = handle;
2780afa8e06SEd Maste 
2790afa8e06SEd Maste 	return (ctx->report_out_len);
2800afa8e06SEd Maste }
281