summaryrefslogtreecommitdiff
path: root/src/hid_win.c
blob: f9705896dd7f3a1eb6970dc47f4d89e769121c99 (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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
 * Copyright (c) 2019 Yubico AB. 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 <fcntl.h>
#include <string.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <windows.h>
#include <setupapi.h>
#include <initguid.h>
#include <hidclass.h>
#include <hidsdi.h>

#include "fido.h"

#define REPORT_LEN	65

static bool
is_fido(HANDLE dev)
{
	PHIDP_PREPARSED_DATA	data = NULL;
	HIDP_CAPS		caps;
	uint16_t		usage_page = 0;

	if (HidD_GetPreparsedData(dev, &data) == false) {
		fido_log_debug("%s: HidD_GetPreparsedData", __func__);
		goto fail;
	}

	if (HidP_GetCaps(data, &caps) != HIDP_STATUS_SUCCESS) {
		fido_log_debug("%s: HidP_GetCaps", __func__);
		goto fail;
	}

	if (caps.OutputReportByteLength != REPORT_LEN ||
	    caps.InputReportByteLength != REPORT_LEN) {
		fido_log_debug("%s: unsupported report len", __func__);
		goto fail;
	}

	usage_page = caps.UsagePage;
fail:
	if (data != NULL)
		HidD_FreePreparsedData(data);

	return (usage_page == 0xf1d0);
}

static int
get_int(HANDLE dev, int16_t *vendor_id, int16_t *product_id)
{
	HIDD_ATTRIBUTES attr;

	attr.Size = sizeof(attr);

	if (HidD_GetAttributes(dev, &attr) == false) {
		fido_log_debug("%s: HidD_GetAttributes", __func__);
		return (-1);
	}

	*vendor_id = attr.VendorID;
	*product_id = attr.ProductID;

	return (0);
}

static int
get_str(HANDLE dev, char **manufacturer, char **product)
{
	wchar_t	buf[512];
	int	utf8_len;
	int	ok = -1;

	*manufacturer = NULL;
	*product = NULL;

	if (HidD_GetManufacturerString(dev, &buf, sizeof(buf)) == false) {
		fido_log_debug("%s: HidD_GetManufacturerString", __func__);
		goto fail;
	}

	if ((utf8_len = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, buf,
	    -1, NULL, 0, NULL, NULL)) <= 0 || utf8_len > 128) {
		fido_log_debug("%s: WideCharToMultiByte", __func__);
		goto fail;
	}

	if ((*manufacturer = malloc(utf8_len)) == NULL) {
		fido_log_debug("%s: malloc", __func__);
		goto fail;
	}

	if (WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, buf, -1,
	    *manufacturer, utf8_len, NULL, NULL) != utf8_len) {
		fido_log_debug("%s: WideCharToMultiByte", __func__);
		goto fail;
	}

	if (HidD_GetProductString(dev, &buf, sizeof(buf)) == false) {
		fido_log_debug("%s: HidD_GetProductString", __func__);
		goto fail;
	}

	if ((utf8_len = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, buf,
	    -1, NULL, 0, NULL, NULL)) <= 0 || utf8_len > 128) {
		fido_log_debug("%s: WideCharToMultiByte", __func__);
		goto fail;
	}

	if ((*product = malloc(utf8_len)) == NULL) {
		fido_log_debug("%s: malloc", __func__);
		goto fail;
	}

	if (WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, buf, -1,
	    *product, utf8_len, NULL, NULL) != utf8_len) {
		fido_log_debug("%s: WideCharToMultiByte", __func__);
		goto fail;
	}

	ok = 0;
fail:
	if (ok < 0) {
		free(*manufacturer);
		free(*product);
		*manufacturer = NULL;
		*product = NULL;
	}

	return (ok);
}

static int
copy_info(fido_dev_info_t *di, const char *path)
{
	HANDLE	dev = INVALID_HANDLE_VALUE;
	int	ok = -1;

	memset(di, 0, sizeof(*di));

	dev = CreateFileA(path, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
	    OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	if (dev == INVALID_HANDLE_VALUE || is_fido(dev) == 0)
		goto fail;

	if (get_int(dev, &di->vendor_id, &di->product_id) < 0 ||
	    get_str(dev, &di->manufacturer, &di->product) < 0)
		goto fail;

	if ((di->path = strdup(path)) == NULL)
		goto fail;

	ok = 0;
fail:
	if (dev != INVALID_HANDLE_VALUE)
		CloseHandle(dev);

	if (ok < 0) {
		free(di->path);
		free(di->manufacturer);
		free(di->product);
		explicit_bzero(di, sizeof(*di));
	}

	return (ok);
}

int
fido_hid_manifest(fido_dev_info_t *devlist, size_t ilen, size_t *olen)
{
	GUID					 hid_guid = GUID_DEVINTERFACE_HID;
	HDEVINFO				 devinfo = INVALID_HANDLE_VALUE;
	SP_DEVICE_INTERFACE_DATA		 ifdata;
	SP_DEVICE_INTERFACE_DETAIL_DATA_A	*ifdetail = NULL;
	DWORD					 len = 0;
	DWORD					 idx = 0;
	int					 r = FIDO_ERR_INTERNAL;

	*olen = 0;

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

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

	devinfo = SetupDiGetClassDevsA(&hid_guid, NULL, NULL,
	    DIGCF_DEVICEINTERFACE | DIGCF_PRESENT);
	if (devinfo == INVALID_HANDLE_VALUE) {
		fido_log_debug("%s: SetupDiGetClassDevsA", __func__);
		goto fail;
	}

	ifdata.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);

	while (SetupDiEnumDeviceInterfaces(devinfo, NULL, &hid_guid, idx++,
	    &ifdata) == true) {
		/*
		 * "Get the required buffer size. Call
		 * SetupDiGetDeviceInterfaceDetail with a NULL
		 * DeviceInterfaceDetailData pointer, a
		 * DeviceInterfaceDetailDataSize of zero, and a valid
		 * RequiredSize variable. In response to such a call, this
		 * function returns the required buffer size at RequiredSize
		 * and fails with GetLastError returning
		 * ERROR_INSUFFICIENT_BUFFER."
		 */
		if (SetupDiGetDeviceInterfaceDetailA(devinfo, &ifdata, NULL, 0,
		    &len, NULL) != false ||
		    GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
			fido_log_debug("%s: SetupDiGetDeviceInterfaceDetailA 1",
			    __func__);
			goto fail;
		}

		if ((ifdetail = malloc(len)) == NULL) {
			fido_log_debug("%s: malloc", __func__);
			goto fail;
		}

		ifdetail->cbSize = sizeof(*ifdetail);

		if (SetupDiGetDeviceInterfaceDetailA(devinfo, &ifdata, ifdetail,
		    len, NULL, NULL) == false) {
			fido_log_debug("%s: SetupDiGetDeviceInterfaceDetailA 2",
			    __func__);
			goto fail;
		}

		if (copy_info(&devlist[*olen], ifdetail->DevicePath) == 0) {
			devlist[*olen].io = (fido_dev_io_t) {
				fido_hid_open,
				fido_hid_close,
				fido_hid_read,
				fido_hid_write,
			};
			if (++(*olen) == ilen)
				break;
		}

		free(ifdetail);
		ifdetail = NULL;
	}

	r = FIDO_OK;
fail:
	if (devinfo != INVALID_HANDLE_VALUE)
		SetupDiDestroyDeviceInfoList(devinfo);

	free(ifdetail);

	return (r);
}

void *
fido_hid_open(const char *path)
{
	HANDLE dev;

	dev = CreateFileA(path, GENERIC_READ | GENERIC_WRITE,
	    FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
	    FILE_ATTRIBUTE_NORMAL, NULL);

	if (dev == INVALID_HANDLE_VALUE)
		return (NULL);

	return (dev);
}

void
fido_hid_close(void *handle)
{
	CloseHandle(handle);
}

int
fido_hid_read(void *handle, unsigned char *buf, size_t len, int ms)
{
	DWORD	n;
	int	r = -1;
	uint8_t	report[REPORT_LEN];

	(void)ms; /* XXX */

	memset(report, 0, sizeof(report));

	if (len != sizeof(report) - 1) {
		fido_log_debug("%s: invalid len", __func__);
		return (-1);
	}

	if (ReadFile(handle, report, sizeof(report), &n, NULL) == false ||
	    n != sizeof(report)) {
		fido_log_debug("%s: ReadFile", __func__);
		goto fail;
	}

	r = sizeof(report) - 1;
	memcpy(buf, report + 1, len);

fail:
	explicit_bzero(report, sizeof(report));

	return (r);
}

int
fido_hid_write(void *handle, const unsigned char *buf, size_t len)
{
	DWORD n;

	if (len != REPORT_LEN) {
		fido_log_debug("%s: invalid len", __func__);
		return (-1);
	}

	if (WriteFile(handle, buf, (DWORD)len, &n, NULL) == false ||
	    n != REPORT_LEN) {
		fido_log_debug("%s: WriteFile", __func__);
		return (-1);
	}

	return (REPORT_LEN);
}