From: Klas Lindfors Date: Sun, 6 Jan 2013 10:58:04 +0000 (+0100) Subject: let _ykusb_open_device() error on several keys present X-Git-Tag: v1.11.2^2~4 X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c906fe83e7ae6af8497150e1f2bc897828d65d4b;p=yubikey-personalization let _ykusb_open_device() error on several keys present if more than one YubiKey is present _ykusb_open_device() will return YK_EMORETHANONE. --- diff --git a/ykcore/ykcore.c b/ykcore/ykcore.c index bd70a66..9694c71 100644 --- a/ykcore/ykcore.c +++ b/ykcore/ykcore.c @@ -394,7 +394,8 @@ static const char *errtext[] = { "not yet implemented", "checksum mismatch", "operation would block", - "invalid command for operation" + "invalid command for operation", + "expected only one YubiKey but several present", }; const char *yk_strerror(int errnum) { diff --git a/ykcore/ykcore.h b/ykcore/ykcore.h index 40c6f02..8d032a5 100644 --- a/ykcore/ykcore.h +++ b/ykcore/ykcore.h @@ -159,6 +159,7 @@ const char *yk_usb_strerror(void); #define YK_ECHECKSUM 0x0a /* checksum validation failed */ #define YK_EWOULDBLOCK 0x0b /* operation would block */ #define YK_EINVALIDCMD 0x0c /* supplied command is invalid for this operation */ +#define YK_EMORETHANONE 0x0d /* expected to find only one key but found more */ /* Flags for response reading. Use high numbers to not exclude the possibility * to combine these with for example SLOT commands from ykdef.h in the future. diff --git a/ykcore/ykcore_libusb-1.0.c b/ykcore/ykcore_libusb-1.0.c index 8c6e974..513fb8a 100644 --- a/ykcore/ykcore_libusb-1.0.c +++ b/ykcore/ykcore_libusb-1.0.c @@ -159,7 +159,7 @@ extern int _ykusb_stop(void) void *_ykusb_open_device(int vendor_id, int *product_ids, size_t pids_len) { - libusb_device *dev; + libusb_device *dev = NULL; libusb_device_handle *h = NULL; struct libusb_device_descriptor desc; libusb_device **list; @@ -168,8 +168,7 @@ void *_ykusb_open_device(int vendor_id, int *product_ids, size_t pids_len) int rc = YK_ENOKEY; for (i = 0; i < cnt; i++) { - dev = list[i]; - ykl_errno = libusb_get_device_descriptor(dev, &desc); + ykl_errno = libusb_get_device_descriptor(list[i], &desc); if (ykl_errno != 0) goto done; @@ -177,23 +176,30 @@ void *_ykusb_open_device(int vendor_id, int *product_ids, size_t pids_len) size_t j; for(j = 0; j < pids_len; j++) { if (desc.idProduct == product_ids[j]) { - break; + if(dev == NULL) { + dev = list[i]; + break; + } else { + rc = YK_EMORETHANONE; + goto done; + } } } - if (j != pids_len) { - rc = YK_EUSBERR; - ykl_errno = libusb_open(dev, &h); - if (ykl_errno != 0) - goto done; - ykl_errno = libusb_detach_kernel_driver(h, 0); - if (ykl_errno != 0) - goto done; - /* This is needed for yubikey-personalization to work inside virtualbox virtualization. */ - ykl_errno = libusb_set_configuration(h, 1); - goto done; - } } } + + if (dev) { + rc = YK_EUSBERR; + ykl_errno = libusb_open(dev, &h); + if (ykl_errno != 0) + goto done; + ykl_errno = libusb_detach_kernel_driver(h, 0); + if (ykl_errno != 0) + goto done; + /* This is needed for yubikey-personalization to work inside virtualbox virtualization. */ + ykl_errno = libusb_set_configuration(h, 1); + goto done; + } done: libusb_free_device_list(list, 1); if (h == NULL) diff --git a/ykcore/ykcore_libusb.c b/ykcore/ykcore_libusb.c index 0141a88..35cd680 100644 --- a/ykcore/ykcore_libusb.c +++ b/ykcore/ykcore_libusb.c @@ -151,30 +151,38 @@ void *_ykusb_open_device(int vendor_id, int *product_ids, size_t pids_len) struct usb_device *dev; struct usb_dev_handle *h = NULL; int rc = YK_EUSBERR; + int found = 0; for (bus = usb_get_busses(); bus; bus = bus->next) { rc = YK_ENOKEY; - for (dev = bus->devices; dev; dev = dev->next) + for (dev = bus->devices; dev; dev = dev->next) { if (dev->descriptor.idVendor == vendor_id) { size_t j; for (j = 0; j < pids_len; j++) { if (dev->descriptor.idProduct == product_ids[j]) { - break; + if(found == 0) { + found = 1; + break; + } else { + rc = YK_EMORETHANONE; + goto done; + } } } - if(j != pids_len) { - rc = YK_EUSBERR; - h = usb_open(dev); + } + } + } + if(found == 1) { + rc = YK_EUSBERR; + h = usb_open(dev); #ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP - if (h != NULL) - usb_detach_kernel_driver_np(h, 0); + if (h != NULL) + usb_detach_kernel_driver_np(h, 0); #endif - /* This is needed for yubikey-personalization to work inside virtualbox virtualization. */ - if (h != NULL) - usb_set_configuration(h, 1); - goto done; - } - } + /* This is needed for yubikey-personalization to work inside virtualbox virtualization. */ + if (h != NULL) + usb_set_configuration(h, 1); + goto done; } done: if (h == NULL) diff --git a/ykcore/ykcore_osx.c b/ykcore/ykcore_osx.c index c59f484..003802e 100644 --- a/ykcore/ykcore_osx.c +++ b/ykcore/ykcore_osx.c @@ -109,9 +109,11 @@ void *_ykusb_open_device(int vendor_id, int *product_ids, size_t pids_len) CFIndex cnt = CFArrayGetCount( array ); - if (cnt > 0) { + if (cnt == 1) { yk = (void *) CFArrayGetValueAtIndex( array, 0 ); CFRetain(yk); + } else if(cnt > 1) { + rc = YK_EMORETHANONE; } else { rc = YK_ENOKEY; diff --git a/ykcore/ykcore_windows.c b/ykcore/ykcore_windows.c index d91ef45..4f437ff 100644 --- a/ykcore/ykcore_windows.c +++ b/ykcore/ykcore_windows.c @@ -96,24 +96,24 @@ void * _ykusb_open_device(int vendor_id, int *product_ids, size_t pids_len) size_t j; for (j = 0; j < pids_len; j++) { if (devInfo.ProductID == product_ids[j]) { - break; + if(ret_handle == NULL) { + ret_handle = m_handle; + break; + } else { + rc = YK_EMORETHANONE; + goto done; + } } } - if (j != pids_len) { - ret_handle = m_handle; - free (pi); - goto done; - } } } } - CloseHandle (m_handle); + if(ret_handle == NULL) { + CloseHandle (m_handle); + } } free (pi); - - if (!rc) - break; } yk_errno = YK_ENOKEY;