1 /* 2 * Copyright (c) 2020 Yubico AB. All rights reserved. 3 * Use of this source code is governed by a BSD-style 4 * license that can be found in the LICENSE file. 5 */ 6 7 #include <sys/types.h> 8 9 #include <dev/usb/usb_ioctl.h> 10 #include <dev/usb/usbhid.h> 11 12 #include <errno.h> 13 #include <unistd.h> 14 15 #include "fido.h" 16 17 #if defined(__MidnightBSD__) 18 #define UHID_VENDOR "MidnightBSD" 19 #else 20 #define UHID_VENDOR "FreeBSD" 21 #endif 22 23 #define MAX_UHID 64 24 25 struct hid_freebsd { 26 int fd; 27 size_t report_in_len; 28 size_t report_out_len; 29 sigset_t sigmask; 30 const sigset_t *sigmaskp; 31 }; 32 33 static bool 34 is_fido(int fd) 35 { 36 char buf[64]; 37 struct usb_gen_descriptor ugd; 38 uint32_t usage_page = 0; 39 40 memset(&buf, 0, sizeof(buf)); 41 memset(&ugd, 0, sizeof(ugd)); 42 43 ugd.ugd_report_type = UHID_FEATURE_REPORT; 44 ugd.ugd_data = buf; 45 ugd.ugd_maxlen = sizeof(buf); 46 47 if (ioctl(fd, IOCTL_REQ(USB_GET_REPORT_DESC), &ugd) == -1) { 48 fido_log_error(errno, "%s: ioctl", __func__); 49 return (false); 50 } 51 if (ugd.ugd_actlen > sizeof(buf) || fido_hid_get_usage(ugd.ugd_data, 52 ugd.ugd_actlen, &usage_page) < 0) { 53 fido_log_debug("%s: fido_hid_get_usage", __func__); 54 return (false); 55 } 56 57 return (usage_page == 0xf1d0); 58 } 59 60 static int 61 copy_info(fido_dev_info_t *di, const char *path) 62 { 63 int fd = -1; 64 int ok = -1; 65 struct usb_device_info udi; 66 67 memset(di, 0, sizeof(*di)); 68 memset(&udi, 0, sizeof(udi)); 69 70 if ((fd = fido_hid_unix_open(path)) == -1 || is_fido(fd) == 0) 71 goto fail; 72 73 if (ioctl(fd, IOCTL_REQ(USB_GET_DEVICEINFO), &udi) == -1) { 74 fido_log_error(errno, "%s: ioctl", __func__); 75 strlcpy(udi.udi_vendor, UHID_VENDOR, sizeof(udi.udi_vendor)); 76 strlcpy(udi.udi_product, "uhid(4)", sizeof(udi.udi_product)); 77 udi.udi_vendorNo = 0x0b5d; /* stolen from PCI_VENDOR_OPENBSD */ 78 } 79 80 if ((di->path = strdup(path)) == NULL || 81 (di->manufacturer = strdup(udi.udi_vendor)) == NULL || 82 (di->product = strdup(udi.udi_product)) == NULL) 83 goto fail; 84 85 di->vendor_id = (int16_t)udi.udi_vendorNo; 86 di->product_id = (int16_t)udi.udi_productNo; 87 88 ok = 0; 89 fail: 90 if (fd != -1) 91 close(fd); 92 93 if (ok < 0) { 94 free(di->path); 95 free(di->manufacturer); 96 free(di->product); 97 explicit_bzero(di, sizeof(*di)); 98 } 99 100 return (ok); 101 } 102 103 int 104 fido_hid_manifest(fido_dev_info_t *devlist, size_t ilen, size_t *olen) 105 { 106 char path[64]; 107 size_t i; 108 109 *olen = 0; 110 111 if (ilen == 0) 112 return (FIDO_OK); /* nothing to do */ 113 114 if (devlist == NULL || olen == NULL) 115 return (FIDO_ERR_INVALID_ARGUMENT); 116 117 for (i = *olen = 0; i < MAX_UHID && *olen < ilen; i++) { 118 snprintf(path, sizeof(path), "/dev/uhid%zu", i); 119 if (copy_info(&devlist[*olen], path) == 0) { 120 devlist[*olen].io = (fido_dev_io_t) { 121 fido_hid_open, 122 fido_hid_close, 123 fido_hid_read, 124 fido_hid_write, 125 }; 126 ++(*olen); 127 } 128 } 129 130 return (FIDO_OK); 131 } 132 133 void * 134 fido_hid_open(const char *path) 135 { 136 char buf[64]; 137 struct hid_freebsd *ctx; 138 struct usb_gen_descriptor ugd; 139 int r; 140 141 memset(&buf, 0, sizeof(buf)); 142 memset(&ugd, 0, sizeof(ugd)); 143 144 if ((ctx = calloc(1, sizeof(*ctx))) == NULL) 145 return (NULL); 146 147 if ((ctx->fd = fido_hid_unix_open(path)) == -1) { 148 free(ctx); 149 return (NULL); 150 } 151 152 ugd.ugd_report_type = UHID_FEATURE_REPORT; 153 ugd.ugd_data = buf; 154 ugd.ugd_maxlen = sizeof(buf); 155 156 if ((r = ioctl(ctx->fd, IOCTL_REQ(USB_GET_REPORT_DESC), &ugd) == -1) || 157 ugd.ugd_actlen > sizeof(buf) || 158 fido_hid_get_report_len(ugd.ugd_data, ugd.ugd_actlen, 159 &ctx->report_in_len, &ctx->report_out_len) < 0) { 160 if (r == -1) 161 fido_log_error(errno, "%s: ioctl", __func__); 162 fido_log_debug("%s: using default report sizes", __func__); 163 ctx->report_in_len = CTAP_MAX_REPORT_LEN; 164 ctx->report_out_len = CTAP_MAX_REPORT_LEN; 165 } 166 167 return (ctx); 168 } 169 170 void 171 fido_hid_close(void *handle) 172 { 173 struct hid_freebsd *ctx = handle; 174 175 if (close(ctx->fd) == -1) 176 fido_log_error(errno, "%s: close", __func__); 177 178 free(ctx); 179 } 180 181 int 182 fido_hid_set_sigmask(void *handle, const fido_sigset_t *sigmask) 183 { 184 struct hid_freebsd *ctx = handle; 185 186 ctx->sigmask = *sigmask; 187 ctx->sigmaskp = &ctx->sigmask; 188 189 return (FIDO_OK); 190 } 191 192 int 193 fido_hid_read(void *handle, unsigned char *buf, size_t len, int ms) 194 { 195 struct hid_freebsd *ctx = handle; 196 ssize_t r; 197 198 if (len != ctx->report_in_len) { 199 fido_log_debug("%s: len %zu", __func__, len); 200 return (-1); 201 } 202 203 if (fido_hid_unix_wait(ctx->fd, ms, ctx->sigmaskp) < 0) { 204 fido_log_debug("%s: fd not ready", __func__); 205 return (-1); 206 } 207 208 if ((r = read(ctx->fd, buf, len)) == -1) { 209 fido_log_error(errno, "%s: read", __func__); 210 return (-1); 211 } 212 213 if (r < 0 || (size_t)r != len) { 214 fido_log_debug("%s: %zd != %zu", __func__, r, len); 215 return (-1); 216 } 217 218 return ((int)r); 219 } 220 221 int 222 fido_hid_write(void *handle, const unsigned char *buf, size_t len) 223 { 224 struct hid_freebsd *ctx = handle; 225 ssize_t r; 226 227 if (len != ctx->report_out_len + 1) { 228 fido_log_debug("%s: len %zu", __func__, len); 229 return (-1); 230 } 231 232 if ((r = write(ctx->fd, buf + 1, len - 1)) == -1) { 233 fido_log_error(errno, "%s: write", __func__); 234 return (-1); 235 } 236 237 if (r < 0 || (size_t)r != len - 1) { 238 fido_log_debug("%s: %zd != %zu", __func__, r, len - 1); 239 return (-1); 240 } 241 242 return ((int)len); 243 } 244 245 size_t 246 fido_hid_report_in_len(void *handle) 247 { 248 struct hid_freebsd *ctx = handle; 249 250 return (ctx->report_in_len); 251 } 252 253 size_t 254 fido_hid_report_out_len(void *handle) 255 { 256 struct hid_freebsd *ctx = handle; 257 258 return (ctx->report_out_len); 259 } 260