From: Klas Lindfors Date: Fri, 30 Nov 2012 11:49:57 +0000 (+0100) Subject: rework _ykusb_open_device() in the ykcore backends X-Git-Tag: v1.10.0~25 X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=54ccd10dd65de13bbb0e4ac579310189d4d79434;p=yubikey-personalization rework _ykusb_open_device() in the ykcore backends it now takes an array of product ids to search for, let yk_open_first_key() search for all known keys with a YubiKey interface. --- diff --git a/ykcore/ykcore.c b/ykcore/ykcore.c index f3985d2..02adf7b 100644 --- a/ykcore/ykcore.c +++ b/ykcore/ykcore.c @@ -59,13 +59,10 @@ int yk_release(void) YK_KEY *yk_open_first_key(void) { - YK_KEY *yk = _ykusb_open_device(YUBICO_VID, YUBIKEY_PID); - int rc = yk_errno; + int pids[] = {YUBIKEY_PID, NEO_OTP_PID, NEO_OTP_CCID_PID}; - if(!yk) { - yk = _ykusb_open_device(YUBICO_VID, YUBIKEY_NEO_PID); - rc = yk_errno; - } + YK_KEY *yk = _ykusb_open_device(YUBICO_VID, pids, sizeof(pids)); + int rc = yk_errno; if (yk) { rc = 0; diff --git a/ykcore/ykcore_backend.h b/ykcore/ykcore_backend.h index b5917cf..b8e1e48 100644 --- a/ykcore/ykcore_backend.h +++ b/ykcore/ykcore_backend.h @@ -39,7 +39,7 @@ int _ykusb_start(void); int _ykusb_stop(void); -void * _ykusb_open_device(int vendor_id, int product_id); +void * _ykusb_open_device(int vendor_id, int *product_ids, size_t pids_len); int _ykusb_close_device(void *); int _ykusb_read(void *dev, int report_type, int report_number, diff --git a/ykcore/ykcore_libusb-1.0.c b/ykcore/ykcore_libusb-1.0.c index 1156065..e6e04b5 100644 --- a/ykcore/ykcore_libusb-1.0.c +++ b/ykcore/ykcore_libusb-1.0.c @@ -157,7 +157,7 @@ extern int _ykusb_stop(void) return 0; } -void *_ykusb_open_device(int vendor_id, int product_id) +void *_ykusb_open_device(int vendor_id, int *product_ids, size_t pids_len) { libusb_device *dev; libusb_device_handle *h = NULL; @@ -173,7 +173,17 @@ void *_ykusb_open_device(int vendor_id, int product_id) if (ykl_errno != 0) goto done; - if (desc.idVendor == vendor_id && desc.idProduct == product_id) { + if (desc.idVendor == vendor_id) { + size_t j; + for(j = 0; j < pids_len; j++) { + if (desc.idProduct == product_ids[j]) { + break; + } + } + if (j == pids_len) { + goto done; + } + rc = YK_EUSBERR; ykl_errno = libusb_open(dev, &h); if (ykl_errno != 0) diff --git a/ykcore/ykcore_libusb.c b/ykcore/ykcore_libusb.c index dac4fc1..adae378 100644 --- a/ykcore/ykcore_libusb.c +++ b/ykcore/ykcore_libusb.c @@ -145,7 +145,7 @@ extern int _ykusb_stop(void) return 1; } -void *_ykusb_open_device(int vendor_id, int product_id) +void *_ykusb_open_device(int vendor_id, int *product_ids, size_t pids_len) { struct usb_bus *bus; struct usb_device *dev; @@ -155,8 +155,17 @@ void *_ykusb_open_device(int vendor_id, int product_id) for (bus = usb_get_busses(); bus; bus = bus->next) { rc = YK_ENOKEY; for (dev = bus->devices; dev; dev = dev->next) - if (dev->descriptor.idVendor == vendor_id - && dev->descriptor.idProduct == product_id) { + if (dev->descriptor.idVendor == vendor_id) { + size_t j; + for (j = 0; j < pids_len; j++) { + if (dev->descriptor.idProduct == prduct_ids[j]) { + break; + } + } + if(j == pids_len) { + goto done; + } + rc = YK_EUSBERR; h = usb_open(dev); #ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP diff --git a/ykcore/ykcore_osx.c b/ykcore/ykcore_osx.c index 839809a..22abdc0 100644 --- a/ykcore/ykcore_osx.c +++ b/ykcore/ykcore_osx.c @@ -68,24 +68,35 @@ static void _ykosx_CopyToCFArray(const void *value, void *context) CFArrayAppendValue( ( CFMutableArrayRef ) context, value ); } -void *_ykusb_open_device(int vendor_id, int product_id) +void *_ykusb_open_device(int vendor_id, int *product_ids, size_t pids_len) { void *yk = NULL; - CFDictionaryRef dict; - CFStringRef keys[2]; - CFStringRef values[2]; int rc = YK_ENOKEY; + size_t i; + + CFMutableArrayRef matches = CFArrayCreateMutable( kCFAllocatorDefault, pids_len, kCFTypeArrayCallBacks ); + CFNumberRef vendorID = CFNumberCreate( kCFAllocatorDefault, kCFNumberIntType, &vendor_id ); - CFNumberRef productID = CFNumberCreate( kCFAllocatorDefault, kCFNumberIntType, &product_id ); + for(i = 0; i < pids_len; i++) { + CFDictionaryRef dict; + CFStringRef keys[2]; + CFStringRef values[2]; + + CFNumberRef productID = CFNumberCreate( kCFAllocatorDefault, kCFNumberIntType, &product_ids[i] ); - keys[0] = CFSTR( kIOHIDVendorIDKey ); values[0] = (void *) vendorID; - keys[1] = CFSTR( kIOHIDProductIDKey ); values[1] = (void *) productID; + keys[0] = CFSTR( kIOHIDVendorIDKey ); values[0] = (void *) vendorID; + keys[1] = CFSTR( kIOHIDProductIDKey ); values[1] = (void *) productID; - dict = CFDictionaryCreate( kCFAllocatorDefault, (const void **) &keys, (const void **) &values, 1, NULL, NULL); + dict = CFDictionaryCreate( kCFAllocatorDefault, (const void **) &keys, (const void **) &values, 1, NULL, NULL); + CFArrayAppendValue( matches, dict ); + + CFRelease( productID ); + CFRelease( dict ); + } - IOHIDManagerSetDeviceMatching( ykosxManager, dict ); + IOHIDManagerSetDeviceMatchingMultiple( ykosxManager, matches ); CFSetRef devSet = IOHIDManagerCopyDevices( ykosxManager ); @@ -110,13 +121,12 @@ void *_ykusb_open_device(int vendor_id, int product_id) IOHIDManagerScheduleWithRunLoop( ykosxManager, CFRunLoopGetCurrent( ), kCFRunLoopDefaultMode ); IOHIDManagerUnscheduleFromRunLoop( ykosxManager, CFRunLoopGetCurrent( ), kCFRunLoopDefaultMode ); + CFRelease( vendorID ); CFRelease( array ); CFRelease( devSet ); } - CFRelease( dict ); - CFRelease( vendorID ); - CFRelease( productID ); + CFRelease( matches ); if (yk) { _ykusb_IOReturn = IOHIDDeviceOpen( yk, 0L ); diff --git a/ykcore/ykcore_stub.c b/ykcore/ykcore_stub.c index ef1671f..083d2be 100644 --- a/ykcore/ykcore_stub.c +++ b/ykcore/ykcore_stub.c @@ -46,7 +46,7 @@ int _ykusb_stop(void) return 0; } -void * _ykusb_open_device(int vendor_id, int product_id) +void * _ykusb_open_device(int vendor_id, int *product_ids, size_t pids_len) { yk_errno = YK_ENOTYETIMPL; return NULL; diff --git a/ykcore/ykcore_windows.c b/ykcore/ykcore_windows.c index b08590f..64f1669 100644 --- a/ykcore/ykcore_windows.c +++ b/ykcore/ykcore_windows.c @@ -49,7 +49,7 @@ int _ykusb_stop(void) return 1; } -void * _ykusb_open_device(int vendor_id, int product_id) +void * _ykusb_open_device(int vendor_id, int *product_ids, size_t pids_len) { HDEVINFO hi; SP_DEVICE_INTERFACE_DATA di; @@ -92,7 +92,17 @@ void * _ykusb_open_device(int vendor_id, int product_id) HIDD_ATTRIBUTES devInfo; if (HidD_GetAttributes(m_handle, &devInfo)) { - if (devInfo.VendorID == vendor_id && devInfo.ProductID == product_id) { + if (devInfo.VendorID == vendor_id) { + size_t j; + for (j = 0; j < pids_len; j++) { + if (devInfo.ProductID == product_ids[j]) { + break; + } + } + if (j == pids_len) { + goto done; + } + ret_handle = m_handle; free (pi); goto done;