summaryrefslogtreecommitdiff
path: root/src/hid_openbsd.c
blob: 8b92bd61dc6b2a5c6642e4361b65dddc6030eace (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
 * Copyright (c) 2019 Google LLC. All rights reserved.
 * Use of this source code is governed by a BSD-style
 * license that can be found in the LICENSE file.
 */

#include <sys/types.h>

#include <sys/ioctl.h>
#include <dev/usb/usb.h>
#include <dev/usb/usbhid.h>

#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <usbhid.h>
#include <poll.h>

#include "fido.h"

#define MAX_UHID	64
#define MAX_REPORT_LEN	(sizeof(((struct usb_ctl_report *)(NULL))->ucr_data))

struct hid_openbsd {
	int fd;
	size_t report_in_len;
	size_t report_out_len;
};

int
fido_dev_info_manifest(fido_dev_info_t *devlist, size_t ilen, size_t *olen)
{
	size_t i;
	char path[64];
	int is_fido, fd;
	struct usb_device_info udi;
	report_desc_t rdesc = NULL;
	hid_data_t hdata = NULL;
	hid_item_t hitem;
	fido_dev_info_t *di;

	if (ilen == 0)
		return (FIDO_OK); /* nothing to do */

	if (devlist == NULL || olen == NULL)
		return (FIDO_ERR_INVALID_ARGUMENT);

	for (i = *olen = 0; i < MAX_UHID && *olen < ilen; i++) {
		snprintf(path, sizeof(path), "/dev/uhid%zu", i);
		if ((fd = open(path, O_RDWR)) == -1) {
			if (errno != ENOENT && errno != ENXIO) {
				fido_log_debug("%s: open %s: %s", __func__,
				    path, strerror(errno));
			}
			continue;
		}
		memset(&udi, 0, sizeof(udi));
		if (ioctl(fd, USB_GET_DEVICEINFO, &udi) != 0) {
			fido_log_debug("%s: get device info %s: %s", __func__,
			    path, strerror(errno));
			close(fd);
			continue;
		}
		if ((rdesc = hid_get_report_desc(fd)) == NULL) {
			fido_log_debug("%s: failed to get report descriptor: %s",
			    __func__, path);
			close(fd);
			continue;
		}
		if ((hdata = hid_start_parse(rdesc,
		    1<<hid_collection, -1)) == NULL) {
			fido_log_debug("%s: failed to parse report descriptor: %s",
			    __func__, path);
			hid_dispose_report_desc(rdesc);
			close(fd);
			continue;
		}
		is_fido = 0;
		for (is_fido = 0; !is_fido;) {
			memset(&hitem, 0, sizeof(hitem));
			if (hid_get_item(hdata, &hitem) <= 0)
				break;
			if ((hitem._usage_page & 0xFFFF0000) == 0xf1d00000)
				is_fido = 1;
		}
		hid_end_parse(hdata);
		hid_dispose_report_desc(rdesc);
		close(fd);

		if (!is_fido)
			continue;

		fido_log_debug("%s: %s: bus = 0x%02x, addr = 0x%02x",
		    __func__, path, udi.udi_bus, udi.udi_addr);
		fido_log_debug("%s: %s: vendor = \"%s\", product = \"%s\"",
		    __func__, path, udi.udi_vendor, udi.udi_product);
		fido_log_debug("%s: %s: productNo = 0x%04x, vendorNo = 0x%04x, "
		    "releaseNo = 0x%04x", __func__, path, udi.udi_productNo,
		    udi.udi_vendorNo, udi.udi_releaseNo);

		di = &devlist[*olen];
		memset(di, 0, sizeof(*di));
		if ((di->path = strdup(path)) == NULL ||
		    (di->manufacturer = strdup(udi.udi_vendor)) == NULL ||
		    (di->product = strdup(udi.udi_product)) == NULL) {
			free(di->path);
			free(di->manufacturer);
			free(di->product);
			explicit_bzero(di, sizeof(*di));
			return FIDO_ERR_INTERNAL;
		}
		di->vendor_id = udi.udi_vendorNo;
		di->product_id = udi.udi_productNo;
		(*olen)++;
	}

	return FIDO_OK;
}

/*
 * Workaround for OpenBSD <=6.6-current (as of 201910) bug that loses
 * sync of DATA0/DATA1 sequence bit across uhid open/close.
 * Send pings until we get a response - early pings with incorrect
 * sequence bits will be ignored as duplicate packets by the device.
 */
static int
terrible_ping_kludge(struct hid_openbsd *ctx)
{
	u_char data[256];
	int i, n;
	struct pollfd pfd;

	if (sizeof(data) < ctx->report_out_len + 1)
		return -1;
	for (i = 0; i < 4; i++) {
		memset(data, 0, sizeof(data));
		/* broadcast channel ID */
		data[1] = 0xff;
		data[2] = 0xff;
		data[3] = 0xff;
		data[4] = 0xff;
		/* Ping command */
		data[5] = 0x81;
		/* One byte ping only, Vasili */
		data[6] = 0;
		data[7] = 1;
		fido_log_debug("%s: send ping %d", __func__, i);
		if (fido_hid_write(ctx, data, ctx->report_out_len + 1) == -1)
			return -1;
		fido_log_debug("%s: wait reply", __func__);
		memset(&pfd, 0, sizeof(pfd));
		pfd.fd = ctx->fd;
		pfd.events = POLLIN;
		if ((n = poll(&pfd, 1, 100)) == -1) {
			fido_log_debug("%s: poll: %s", __func__, strerror(errno));
			return -1;
		} else if (n == 0) {
			fido_log_debug("%s: timed out", __func__);
			continue;
		}
		if (fido_hid_read(ctx, data, ctx->report_out_len, 250) == -1)
			return -1;
		/*
		 * Ping isn't always supported on the broadcast channel,
		 * so we might get an error, but we don't care - we're
		 * synched now.
		 */
		fido_log_debug("%s: got reply", __func__);
		fido_log_xxd(data, ctx->report_out_len);
		return 0;
	}
	fido_log_debug("%s: no response", __func__);
	return -1;
}

void *
fido_hid_open(const char *path)
{
	struct hid_openbsd *ret = NULL;
	report_desc_t rdesc = NULL;
	int len, usb_report_id = 0;

	if ((ret = calloc(1, sizeof(*ret))) == NULL ||
	    (ret->fd = open(path, O_RDWR)) < 0) {
		free(ret);
		return (NULL);
	}
	if (ioctl(ret->fd, USB_GET_REPORT_ID, &usb_report_id) != 0) {
		fido_log_debug("%s: failed to get report ID: %s", __func__,
		    strerror(errno));
		goto fail;
	}
	if ((rdesc = hid_get_report_desc(ret->fd)) == NULL) {
		fido_log_debug("%s: failed to get report descriptor", __func__);
		goto fail;
	}
	if ((len = hid_report_size(rdesc, hid_input, usb_report_id)) <= 0 ||
	    (size_t)len > MAX_REPORT_LEN) {
		fido_log_debug("%s: bad input report size %d", __func__, len);
		goto fail;
	}
	ret->report_in_len = (size_t)len;
	if ((len = hid_report_size(rdesc, hid_output, usb_report_id)) <= 0 ||
	    (size_t)len > MAX_REPORT_LEN) {
		fido_log_debug("%s: bad output report size %d", __func__, len);
 fail:
		hid_dispose_report_desc(rdesc);
		close(ret->fd);
		free(ret);
		return NULL;
	}	
	ret->report_out_len = (size_t)len;
	hid_dispose_report_desc(rdesc);
	fido_log_debug("%s: USB report ID %d, inlen = %zu outlen = %zu",
	    __func__, usb_report_id, ret->report_in_len, ret->report_out_len);

	/*
	 * OpenBSD (as of 201910) has a bug that causes it to lose
	 * track of the DATA0/DATA1 sequence toggle across uhid device
	 * open and close. This is a terrible hack to work around it.
	 */
	if (terrible_ping_kludge(ret) != 0) {
		fido_hid_close(ret);
		return NULL;
	}

	return (ret);
}

void
fido_hid_close(void *handle)
{
	struct hid_openbsd *ctx = (struct hid_openbsd *)handle;

	close(ctx->fd);
	free(ctx);
}

int
fido_hid_read(void *handle, unsigned char *buf, size_t len, int ms)
{
	struct hid_openbsd *ctx = (struct hid_openbsd *)handle;
	ssize_t r;

	(void)ms; /* XXX */

	if (len != ctx->report_in_len) {
		fido_log_debug("%s: invalid len: got %zu, want %zu", __func__,
		    len, ctx->report_in_len);
		return (-1);
	}
	if ((r = read(ctx->fd, buf, len)) == -1 || (size_t)r != len) {
		fido_log_debug("%s: read: %s", __func__, strerror(errno));
		return (-1);
	}
	return ((int)len);
}

int
fido_hid_write(void *handle, const unsigned char *buf, size_t len)
{
	struct hid_openbsd *ctx = (struct hid_openbsd *)handle;
	ssize_t r;

	if (len != ctx->report_out_len + 1) {
		fido_log_debug("%s: invalid len: got %zu, want %zu", __func__,
		    len, ctx->report_out_len);
		return (-1);
	}
	if ((r = write(ctx->fd, buf + 1, len - 1)) == -1 ||
	    (size_t)r != len - 1) {
		fido_log_debug("%s: write: %s", __func__, strerror(errno));
		return (-1);
	}
	return ((int)len);
}