0

Assistance in trying to detect when an actual smart-card is inserted into a USB smart-card reader attached to a handheld Android device.

I have an existing (and completely working) instance of the BroadcastReceiver and am successfully detecting when the actual smart-card reader device is connected via the filters of

myFilter.AddAction(UsbManager.ActionUsbDeviceAttached);
myFilter.AddAction(UsbManager.ActionUsbDeviceDetached);

What I am looking for now is when an actual card is inserted or removed from the device.

By using ADB (Android Debug Bridge) program and using the logcat, I can redirect the output to a file to see what is going on such as

adb logcat -c   (to clear log)
adb logcat > c:\SomeFolder\MyAndroidLog.txt

Before starting the log above, I have my handheld device ready, the smart-card reader attached. Immediately after that is when I clear the log and start a fresh capture. Then I insert my smart card, wait a few seconds and then stop the log with Ctrl+C. Upon viewing the log, I am getting

--------- beginning of main
10-07 11:43:26.458   827   827 I chatty  : uid=1000(system) /system/bin/surfaceflinger expire 14 lines
10-07 11:43:26.459   827   827 I ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRDisplay retrieved: 0
10-07 11:43:26.861   556   556 W hwservicemanager: getTransport: Cannot find entry vendor.qti.hardware.servicetracker@1.0::IServicetracker/default in either framework or device manifest.
10-07 11:43:26.925   556   556 I chatty  : uid=1000(system) hwservicemanage identical 14 lines
10-07 11:43:26.928   556   556 W hwservicemanager: getTransport: Cannot find entry vendor.qti.hardware.servicetracker@1.0::IServicetracker/default in either framework or device manifest.
10-07 11:43:26.930  7529  7529 D BoundBrokerSvc: onUnbind: Intent { act=com.google.android.gms.feedback.internal.IFeedbackService dat=chimera-action:com.google.android.gms.feedback.internal.IFeedbackService cmp=com.google.android.gms/.chimera.GmsBoundBrokerService }
10-07 11:43:27.481   827   827 I ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRDisplay retrieved: 0
10-07 11:43:27.483   827   827 I chatty  : uid=1000(system) /system/bin/surfaceflinger identical 2 lines
10-07 11:43:27.484   827   827 I ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRDisplay retrieved: 0
10-07 11:43:27.780  1170  1170 I HSMCOUNTER: Wrote to EEPROM: ret: 0
10-07 11:43:28.444   827   827 I ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRDisplay retrieved: 0

So it is showing some context of a ConfigStore, but nothing that indicates the "intent" action that other activities are capturing such as when the USB device is connected such as:

10-07 12:05:15.068  1512  1828 I ActivityManager: START u0 {act=android.hardware.usb.action.USB_DEVICE_ATTACHED flg=0x11000000 cmp=com.gemalto.gempcsc.gempcscserviceactivity/com.gemalto.gempcsc.gempcscservice.USBBroadcastReceiver (has extras)} from uid 1000

Additionally, if there is a generic way to capture ALL intents going on vs having to manually add all filters and not even knowing what they all COULD be, might help me narrow it down.

DRapp
  • 47,638
  • 12
  • 72
  • 142
  • do the reader docs say what happens when a card is inserted? – Jason Oct 07 '22 at 17:26
  • There is no direct support in Android for this as it depends upon the reader hardware and the type of "smart card" you are trying to read (write), such as ISO7816, SEOS, PC/SC 2.01, etc. and you did not specify which card and reader type you are using. There are dozens (hundreds?) of smart card standards worldwide and when it comes to APDU commands (ISO7816?) there are well over one hundred possible `responses` to those commands, handling edge cases one by one is a coding/testing nightmare. I fall back to the hardware vendor's SDK and just create a binding library to access it via Xamarin... – SushiHangover Oct 07 '22 at 17:50
  • Before communicating with the USB accessory, your application must have permission from your users.https://developer.android.com/guide/topics/connectivity/usb/accessory#:~:text=Note%3A%20If%20your%20application%20uses%20an%20intent%20filter%20to%20discover%20accessories%20as%20they%27re%20connected%2C%20it%20automatically%20receives%20permission%20if%20the%20user%20allows%20your%20application%20to%20handle%20the%20intent.%20If%20not%2C%20you%20must%20request%20permission%20explicitly%20in%20your%20application%20before%20connecting%20to%20the%20accessory. – Zack Oct 10 '22 at 03:03
  • @DongzhiWang-MSFT, yes, I am getting proper request to access the USB device. Small steps. Trying to now understand ATR from power-on request but dont see documentation on bwICCStatus and bcCommandStatus from DWG_Smart-Card_CCID_Rev110 documentation to interpret responses. – DRapp Oct 10 '22 at 03:56
  • I don't know much about smart-card, about ATR, I hope this answer will help you:https://stackoverflow.com/questions/45099849/how-to-read-a-smart-card-microprocessor-card-using-a-smart-card-reader-in-andro?noredirect=1&lq=1#:~:text=Since%20there%20is,a%20smartcard%20reader. – Zack Oct 10 '22 at 09:23
  • @DongzhiWang-MSFT, that was one of the posts I had already referenced. Thanks though. – DRapp Oct 10 '22 at 11:43
  • You can try to create listen on USB events(ACTION_USB_DEVICE_ATTACHED & ACTION_USB_DEVICE_DETACHED).https://developer.android.com/reference/android/hardware/usb/UsbManager#:~:text=usb.action.USB_ACCESSORY_DETACHED%22-,ACTION_USB_DEVICE_ATTACHED,-Added%20in%20API – Zack Oct 11 '22 at 09:31
  • @DongzhiWang-MSFT, already am based on above sample code segment. Those filters are defined constants and that is already working perfectly. – DRapp Oct 11 '22 at 12:38

1 Answers1

1

You need to talk to the reader using the CCID protocol. See my previous answer.


For example (sequence numbers in bold):

Send PC_to_RDR_GetSlotStatus (message 0x65 for slot 0):

-> 65000000000000000000

And receive RDR_to_PC_SlotStatus (message 0x81):

  • Example response for no card present:

<- 81000000000000020000

  • Example response for inserted unpowered card

<- 81000000000000010000


Below is a base-64 encoded wireshark USB trace detecting card in a GemPC Twin Reader(decode it to binary file and open it in Wireshark, then right-click any packet and select "Decode As...", change to "USB Device" tab, select "USBCCID" protocol and click "OK"):

-----BEGIN PCAP-NG CAPTURE FILE-----
H4sIAAAAAAAAA+Pi5eWSYWBg8LXRlmIE0v+hACQG4usA8R0GEGBhYGJgZygtTsrNzzNg4ATKsjFA
AEgNiJ0D5X96xcrA/SrFzgvIBmGGA9Pfnpn0/38wM5MCI4Muw8opHskg4dO6rAy9QLu4gGwuBtwg
FYmdg8WumteYdjkzN6HYZaPPClaP0y4mCNWIxMNml7LHFLhdB8S6FiH7aw3ULuWjbCT4iw2nvxZ7
YtoF8xfMrsXH2EjwFxsjzC5WNLsi2HztGRlkGJzzS/NKUouKFQqK8ssyU1JTFJIqFVJKcwuSEwuY
GDjAas8cN7NjhrIDgPpYgOwUZohZrEA2MgDZAQDbbEGJZAIAAA==
-----END PCAP-NG CAPTURE FILE-----

(Note: Sequence numbers in capture file have holes as packets are extracted from a longer dump)

vlp
  • 7,811
  • 2
  • 23
  • 51