Get Drive name of the SmartCard - windows

I am writing a java smart card application in windows MFC. How do i find the drive name( name display in "My Computer") of the connected smart card. For eg. once i connect the smart card(even though it a smart card, it will work as memory card ) it will come as "** Removable Disk(F:)**" . I am able to connect to smart card using SCardConnect function. from the handle return from SCardConnect function, can i get the drive name .
Or is there any way to find out whether a particular drive is smart card. ie. we can easily find out the removable disks in the machine and from that is it possible to find out whether its a smart card or not.
Thanks in advance

I have written code to enumerate SmartCard devices, in C; here is the code I use;
Cheers,
Con
int findDevice(){
hContext = NULL;
log("\nSearching for the following device: XXX eKrypto Pinpad\n");
//Get a context to the resource manager
lReturn = SCardEstablishContext(SCARD_SCOPE_USER,NULL,NULL,&hContext);
log("Context established.\n");
if(lReturn != SCARD_S_SUCCESS){
logBytes("SCardEstablishContext failed with error: ", (byte *)&lReturn, sizeof(LONG));
return 0;
}
contextEstablished = true;
//Get the readers list
DWORD chReaders = 250;
lReturn = SCardListReaders(hContext,NULL,readerNames,&chReaders);
log("Reader List obtained.\n");
if(lReturn != SCARD_S_SUCCESS){
logBytes(" SCardListReaders failed with error: \n", (byte *)&lReturn, sizeof(LONG));
return 0;
}
//Search for device
char *pReaders = readerNames;
log(pReaders);
while(strlen(pReaders) != 0 && deviceFound == false){
if(!memcmp(pReaders, "ETS eKrypto Pinpad", 18)){
deviceFound = true;
memcpy(currentReader, pReaders, strlen(pReaders));
}
pReaders += strlen (pReaders)+1;
}
if(deviceFound == false){
log(" Specified ETS device not found\n");
return 0;
}
log(" Successful\n");
return 1;
}

Related

How can we open a hard drive using kernel extension in Mac OSx?

I am trying to open(access) a volume of hard drive using below code in app.
int mode=0;
int hd_h=-1;
const char* device = "/dev/rdisk1”
mode=O_RDWR|mode_basic;
hd_h = open(device, mode);
When I executed above code it returns error "Operation not permitted". Then I searched how it can be achieved and found one answer using kext we can achieve it. I created a sample kext(driver) to access harddrive in kernel space. can anybody assist how can I open volume in kext and return to user space. I have created a IOUserClient subclass and calling IOServiceOpen() in user space .
CFDictionaryRef matchingDict = NULL;
io_iterator_t iter = 0;
io_service_t service = 0;
kern_return_t kr;
// Create a matching dictionary that will find any USB device
matchingDict = IOServiceMatching("com_osxkernel_driver_IOKitTest");
// Create an iterator for all IO Registry objects that match the dictionary
kr = IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDict, &iter);
if (kr != KERN_SUCCESS)
return -1;

MAC addresses on a machine / filtering out the MAC addresses of plug and play devices

I have the following code which runs through and populates a character array with up to 3 WiFi and Ethernet adapter MAC addresses on a Windows machine:
IP_ADAPTER_INFO *info = NULL, *pos;
DWORD size = 0;
if (GetAdaptersInfo(info, &size) != ERROR_BUFFER_OVERFLOW)
return;
info = (IP_ADAPTER_INFO *)malloc(size);
if (GetAdaptersInfo(info, &size) != ERROR_SUCCESS)
return;
char addresses[1024];
char buffer[1024];
memset(addresses, 0, sizeof(addresses));
memset(buffer, 0, sizeof(buffer));
int recordedAddresses = 0;
for (pos = info; pos != NULL; pos = pos->Next) {
if (pos->Type != IF_TYPE_IEEE80211 && pos->Type != MIB_IF_TYPE_ETHERNET)
continue;
if (recordedAddresses > 0)
strcat_s<sizeof(addresses)>(addresses, " ");
for (int i = 0; i < pos->AddressLength; i++) {
sprintf_s<sizeof(buffer)>(buffer, i == 0 ? "%2.2x" : ":%2.2x", pos->Address[i]);
strcat_s<sizeof(addresses)>(addresses, buffer);
}
recordedAddresses++;
if (recordedAddresses >= 3)
break;
}
free(info);
// The array called 'addresses' now contains something like this: a0:b1:c2:d3:e4:f5 0a:1b:2c:3d:4e:5f 00:01:02:03:04:05
How can I detect if any of these IP_ADAPTER_INFO structures refer to plug and play devices? Is there a standard way of doing this? I have been searching for a solution. Ideally, I wish to filter out Plug-and-Play WiFi dongles from my list of addresses, the type of dongles that have a USB interface and allow you to get a WiFi connection running on your Windows machine via USB dongle (if possible).
You need to use IP_ADAPTER_ADDRESSES NOT IP_ADAPTER_INFO Struct.
Look specifically for PhysicalAddress and interate through the addresses.

How do cdev and its associated file operation work?

Actually working on a PCI driver.
I have two PCIe cards with same device ID and vendor ID.
So to make a difference, I assign these two cards with two different MINOR numbers.
//request for device numbers
error = alloc_chrdev_region(&devt, 0, cards_found, DEVICE_NAME);
if (error == 0)
{
major = MAJOR(devt);
printk(KERN_INFO "(drv_init): MAJOR number is %d\n", major);
printk(KERN_INFO "(drv_init): MINOR number range from 0 to %d\n", cards_found-1);
cdevs = cdev_alloc();
cdevs->owner = THIS_MODULE;
cdev_init(cdevs, fops);
for(i=0;i<cards_found,i++)
{
devt = MKDEV(major, i);
error = cdev_add(cdevs, devt, 1);
if (error == 0)
{
printk(KERN_INFO "(drv_init): cdev_add success for minor number: %d", i);
}
else
{
printk(KERN_ALERT "(drv_init): cdev_add failed for minor number: %d,error code: %d, exit driver\n", i, error);
devt = MKDEV(major, 0);
unregister_chrdev_region(devt, cards_found);
pci_unregister_driver(&my_pci_driver);
return(error);
}
}
} `
I'm doing this because all docs I found on internet suggests that one MINOR number for one device.
But I can't understand how could OS know which card is targeted when I do a fops_open, since the fops is bundled to all devices.
Need your help, Thx everyone.
PS: fops = file operations
Signature of .open operation is
int open(struct inode* inode, struct file* file)
Minor number of device opened can be obtained via
iminor(inode)
Other file operations also may obtain device number using file->f_inode as inode.
Alternatively, .open may store some device-specific data in file->f_private, and other operations may access them that way.

Issue retrieving serial number of USB devices on Mac OS X

I'm working with libusb 0.1 and Qt in a Mac OS X v10.10 (Yosemite) environment. My goal is to get the serial numbers of all connected USB keys (usbclass = 8).
Usually the first reading occur properly, and I cannot understand why, but sometimes from subsequent readings, the device is no longer detected. Sometimes I get -60 error code on the usb_get_string_simple(handle, device->descriptor.iSerialNumber) function. How can I fix this problem?
I've tried USB keys with FAT, FAT32 and NTFS file systems, but they all had the same issue.
In the same project, I use libusb to read/write with printer devices connected to the Mac, and read serialnumbers too. No issue with this type of devices. So strange.
In the .pro file:
INCLUDEPATH += /Users/sborfedor/Desktop/root/current/includes
DEPENDPATH += /Users/sborfedor/Desktop/root/current/includes
LIBS += -L/Users/sborfedor/Desktop/root/current/bin_osx/libs/ -lusb-legacy
DESTDIR = /Users/sborfedor/Desktop/root/current/bin_osx
This is my code:
void MainWindow::run() {
struct usb_bus* bus = NULL;
struct usb_device* device = NULL;
const int MASSSTORAGE_CLASS = 8;
usb_init();
usb_set_debug(3);
usb_find_busses();
usb_find_devices();
int ret;
for ( bus = usb_get_busses(); bus; bus = bus->next ) {
for ( device = bus->devices; device; device = device->next ) {
if (device->descriptor.bDeviceClass != MASSSTORAGE_CLASS && device->descriptor.bDeviceClass != 0) {
continue;
}
for ( int cid = 0; cid < device->descriptor.bNumConfigurations; ++cid ) {
struct usb_config_descriptor* config = &( device->config[cid] );
if (config == NULL)
continue;
for ( int iid = 0; iid < config->bNumInterfaces; ++iid ) {
struct usb_interface* interface = &( config->interface[iid] );
if (interface == NULL)
continue;
usb_dev_handle* handle = usb_open( device );
if (handle == NULL)
continue;
for ( int sid = 0; sid < interface->num_altsetting; sid++ ) {
struct usb_interface_descriptor* settings = &( interface->altsetting[sid] );
if (settings == NULL)
continue;
int usbClass = device->descriptor.bDeviceClass;
if (usbClass == 0) {
usbClass = settings->bInterfaceClass;
}
if (usbClass != MASSSTORAGE_CLASS)
continue;
char device_serial_number[255];
ret = usb_get_string_simple( handle, device->descriptor.iSerialNumber, device_serial_number, sizeof(device_serial_number) );
if (ret > 0 ) {
qDebug() << "SERIAL_NUMBER="<<QString(device_serial_number);
} else {
qDebug() << "*** usb_get_string_simple( handle, device->descriptor.iSerialNumber, device_serial_number, USBX_DEVICE_SERIAL_NUMBER_MAX_SIZE ) RET="<<ret;
}
}
usb_close( handle );
}
}
}
}
}
I'm not especially familiar with libusb, but OSX already reads out serial numbers and stores them in the relevant property on IOKit device nubs. So there is no need to send requests to hardware, which might be confusing the active driver? (Libusb presumably creates a user client driver instance on the device)
The Disk Arbitration Framework is a good way of enumerating disks in the system and querying their properties - this is a bit easier than using IOKit directly.

Drive Letter to Device Instance ID

How do I get from a drive letter to a device instance ID?
My process starts with a device arrival message. I have been successful in getting the drive letter from the arrival message and in opening the dvd tray.
I have searched the various Setup API items; but I haven't found anything that gets me from a drive letter to a device instance ID.
A solution in C# or VB.NET would be ideal, but I'm willing to figure it out from any other language as long as I can see the API calls.
Thanks in advance...
You cannot do it directly.
The link is to use STORAGE_DEVICE_NUMBER. You can use DeviceIoControl with IOCTL_STORAGE_GET_DEVICE_NUMBER on your device name to populate this structure. Put this value to one side.
You then need to get device infomation on your system using SetupDiGetClassDevs setting the GUIDS as approriate, indicicating the drives your are insterested in. Then enumerate through the devices using SetupDiEnumDeviceInfo. Then enumerate the interfaces using SetupDiEnumDeviceInterfaces and finally get the information using SetupDiGetDeviceInterfaceDetail. In this structure returned you can get a DevicePath you can use to get the STORAGE_DEVICE_NUMBER as above. Match this with the STORAGE_DEVICE_NUMBER from your drive letter, and you have now linked a driver letter to your structure. Phew! Inside this structure is a DevInst.
i know it's late for you now but not for everybody ^^
I had the same need and this is main line of how I did it:
-You need a window to receive device arrival and removal (as you said)
-Then you create a DeviceNotificationFilter initiated to dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE
-Then in the message loop of your window you look for VM_DEVICECHANGE
-When u receive it if wParam == DBT_DEVICEARRIVAL, use the lParam to check if it is a DBT_DEVTYPE_VOLUME (i was getting the letter and the type of the drive here) or a DBT_DEVTYPE_DEVICEINTERFACE ( there you can use your wellcasted lParam to get the InstanceId from the input structure).
When you connect a drive your receive DEVINTERFACE first then the other.
I only give the main line beacause i did this long time ago and i don't have the code here, and also I had found a lot of code pieces on the net (long time ago so there should be more now ^^^) maybe msdn give a full code example to do that now.
If you read this and need more informations, i'll reply or make a full documented answer if many need it.
Hope it will help some of you.
I know it's years later but I had to do this and searching brought me here and #DanDan 's answer worked. In order to save future people a lot of work, I thought I'd give back a little and present the technique a bit more explicitly. You'll still have to write a bit of code, but the part I found difficult is below as code:
As DanDan mentioned, the idea is to use CreateFile and DeviceIoControl to get the Windows STORAGE_DEVICE_NUMBER for the disk associated with a file path, and then use the Setup API to enumerate disk devices until we find one whose device instance equals the SDN.
First, here's a summary of how you get the STORAGE_DEVICE_NUMBER from the path (e.g. c:\\users\\bob);
Strip the path to the root (e.g down to C:) and prepend it with \\\\.\\ so you have \\\\.\\C:
Open that path up using CreateFileW with to get metadata
Use DeviceIoControl with IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS to get the extents
Get the DiskNumber member from the first extent returned.
Close the file
Open up \\\\.\\PhysicalDrive<n> where <n> is the that DiskNumber from the first extent
Use DeviceIoControl with code IOCTL_STORAGE_GET_DEVICE_NUMBER to get make it fill out a STORAGE_DEVICE_NUMBER struct as output
Use SetupDiGetClassDevs with arguments &GUID_DEVCLASS_DISKDRIVE and DICGF_PRESENT to get all disks on the system
In a loop, use SetupDiEnumDeviceInfo to get a SP_DEVINFO_DATA repeatedly (on the device list returned by step #8 above) and a call the function below to determine which one, if any, matches the STORAGE_DEVICE_NUMBER for the give path.
(This is edited to remove custom utility classes of mine right on the SO web page so I might have introduced errors/typos)
bool DoesDeviceInstanceEqualStorageDeviceNumber(
const std::string& devInstance,
STORAGE_DEVICE_NUMBER sdn)
{
// Open up this device instance, specifying that we want the *interfaces*.
// The interfaces are key key because examining them will let us get a
// string we can use the Win32 CreateFile function.
const auto hDevInfo = SetupDiGetClassDevsA(
nullptr,
devInstance.c_str(),
nullptr,
DIGCF_DEVICEINTERFACE | DIGCF_ALLCLASSES);
if (hDevInfo == INVALID_HANDLE_VALUE)
throws std::runtime_error("Unable to get disk devices");
DWORD dwSize = 0;
SP_DEVINFO_DATA did;
WCHAR buffer[4096];
did.cbSize = sizeof (did);
bool foundValidMatch = false;
int deviceNumber = 0;
// Iterate through all such devices, looking for one that has a storage device number that matches the given one.
while ( !foundValidMatch && SetupDiEnumDeviceInfo(hDevInfo, deviceNumber, &did))
{
deviceNumber++;
DEVPROPTYPE devPropType;
// We'll only bother comparing this one if it is fixed. Determine that.
const auto getPropResult = SetupDiGetDevicePropertyW (
hDevInfo,
&did,
&DEVPKEY_Device_RemovalPolicy, // Ask for the "removal policy"
&devPropType,
(BYTE*)buffer,
sizeof(buffer),
&dwSize,
0);
if (!getPropResult)
{
std::cerr << "Unable to to get removal policy for disk device: " << ::GetLastError() << std::endl;
continue;
}
/* This bit *would* skip removable disks, you wanted...
else if (buffer[0] != 1)
{
std::cerr << "Skipping removable disk device " << devInstance << std::endl;
continue;
}
*/
// OK this is a fixed disk so it might be the one we'll compare against
// 1. Get the very first disk interface from this particular disk device
// 2. Open a file on it
// 3. Query the resulting file for its device number.
// 4. Compare the device number to the one we determined above
// 5. If it matches ours, then we succeed. If not, continue
SP_DEVICE_INTERFACE_DATA devIntData;
devIntData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
// Get the disk interfaces
const auto result = SetupDiEnumDeviceInterfaces(
hDevInfo,
&did, //&did,
&GUID_DEVINTERFACE_DISK, // Get Disk Device Interface (from winioctl.h)
0, // We only need the very FIRST one. I think...
&devIntData);
if (!result)
continue;
DWORD dwRequiredSize = 0;
// Want to get the detail but don't yet know how much space we'll need
// Do a dummy call to find out
SetupDiGetDeviceInterfaceDetail(
hDevInfo,
&devIntData,
nullptr,
0,
&dwRequiredSize,
nullptr);
if (ERROR_INSUFFICIENT_BUFFER != ::GetLastError())
{
std::cerr << "Unable to get device interface Detail: " << ::GetLastError() << std::endl;;
}
else
{
// Get the detail data so we can get the device path and open a file.
std::vector<TCHAR> buf(dwRequiredSize);
auto pDidd = reinterpret_cast<PSP_DEVICE_INTERFACE_DETAIL_DATA>(buf.data());
// WARNING: HARD CODED HACK
// ------------------------
// https://stackoverflow.com/questions/10405193/vb-net-hid-setupdigetdeviceinterfacedetail-getlasterror-shows-1784-error-inv
//
// Don't ask. Just do what they tell you.
// -----------------------------------------------------------------
#ifdef BUILD_64
pDidd->cbSize = 8;
#else
pDidd->cbSize = 6;
#endif
// -----------------------------------------------------------------
if (!SetupDiGetDeviceInterfaceDetail(
hDevInfo,
&devIntData,
pDidd,
dwRequiredSize,
&dwRequiredSize,
nullptr))
{
std::cerr << "Cannot get interface detail: " << ::GetLastError());
}
else
{
// FINALLY: We now have a DevicePath that we can use to open up
// in a Win32 CreateFile() call. That will let us get the
// STORAGE_DEVICE_NUMBER and compare it to the one we were given.
const auto hFile = ::CreateFileW(pDidd->DevicePath, 0, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, NULL);
if (INVALID_HANDLE_VALUE != hFile)
{
std::cerr << "Unable to open logical volume: " + devicePath << std::endl;
continue;
}
STORAGE_DEVICE_NUMBER sdnTest;
ZeroMemory(&sdnTest, sizeof(STORAGE_DEVICE_NUMBER));
if (0 == DeviceIoControl(
hDevInfo
IOCTL_STORAGE_GET_DEVICE_NUMBER,
nullptr, // output only so not needed
0, // output only so not needed
&sdnTest,
sizeof(STORAGE_DEVICE_NUMBER),
nullptr,
nullptr))
{
std::cerr << "Unable to determine storage device number: " << ::GetLastError() << std::endl;);
}
else
{
// All this for a one-line test...
foundValidMatch = sdnTest.DeviceNumber == sdn.DeviceNumber;
}
}
}
}
SetupDiDestroyDeviceInfoList(hDevInfo);
return foundValidMatch;
}
I hope this saves someone a headache

Resources