Sunday, May 28, 2017

New version of pcsc-tools: 1.5.0, 1.5.1, 1.5.2

I just released a new version of pcsc-tools, a suite of tools for PC/SC.

The main changes are for the pcsc_scan command.

1.5.2 - 28 May 2017, Ludovic ROUSSEAU
  • include gscriptor.desktop and test.script in the archive

1.5.1 - 28 May 2017, Ludovic ROUSSEAU
  • rename the archive pcsc-tools

1.5.0 - 28 May 2017, Ludovic ROUSSEAU
  • 221 new ATRs
  • pcsc_scan:
    • add APDU stress mode (-s) (send "Select MF" APDU in a loop)
    • display an animation while waiting for a card or reader event
  • use autoconf/automake

Sunday, May 21, 2017

New PyKCS11 1.4.2 available

I just released a new version of PyKCS11, a Python wrapper above the PKCS#11 API.

See PyKCS11 introduction for more details about PyKCS11.

1.4.2 - May 2017, Ludovic Rousseau
  • Moved the project from to
  • Makefile: use a better default value for PREFIX
  • Fix PyKCS11.__del__(): test that every module is accessible
  • getSlotList(): add optional tokenPresent parameter
    By default the method returns all the slots (like before the change).
  • Always call C_Initialize() in ::Load() to work with some bogus PKCS#11 library (like libCryptoki2 from Safenet Luna SA HSM)
  • LowLevel samples: use PYKCS11LIB environment variable
  • some minor improvements

Available on:

New version of libccid: 1.4.27

I just released a version 1.4.27 of libccid the Free Software CCID class smart card reader driver.

1.4.27 - 21 May 2017, Ludovic Rousseau
  • Add support of
    • ACS ACR1255U-J1
    • ACS CryptoMate (T2)
    • ANCUD CCID USB Reader & RNG
    • DUALi DE-620 Combi
    • FT CCID
    • FT CCID KB
    • FT U2F CCID KB
    • HID Global OMNIKEY 5422 Smartcard Reader
    • InfoThink IT-102MU Reader
    • Kapsch TrafficCom USB SAM reader
    • MK Technology KeyPass S1
    • Mulann PVT
    • Regula RFID Reader
    • Spyrus Inc PocketVault P-3X
    • Unicept GmbH AirID USB
  • Add Microchip SEC1210 UART support (when connected on a serial port)
  • Add Zero Length Packet (ZLP) support for Gemalto IDBridge CT30 and K30
    enable the patch using ./configure --enable-zlp
  • Add support of HID Omnikey 5422 as multi slot reader (for macOS)
  • Escape command: signals buffer overflow instead of silently truncating the buffer
  • Fix a bug with multi readers and pcscd uses hotplug_libusb (not the recommended configuration)
  • Some minor improvements

Saturday, May 20, 2017

New version of pcsc-lite: 1.8.21

I just released a new version of pcsc-lite 1.8.21.
pcsc-lite is a Free Software implementation of the PC/SC (or WinSCard) API for Unix systems.

1.8.21: Ludovic Rousseau
20 May 2017
  • SCardCancel():
    • fix a race condition bug
    • fix a possible use after free
    • improve Doxygen documentation
  • SCardGetStatusChange(): fix a race condition when a reader is removed
  • SCardDisconnect(): fix status update for SCARD_UNPOWER_CARD
  • pcsc_stringify_error(): use Thread-local storage
  • pcsc_stringify_error() now returns a const char *
  • SCardControl() may return SCARD_E_INSUFFICIENT_BUFFER when pbRecvBuffer is not big enough to receive the card response.
  • Fix compilation error with GCC 4.9
  • UnitaryTests: add support of Python 3
  • pcsc-spy: add support of Python 3
  • Some other minor improvements

Saturday, April 1, 2017

Google Open Source Peer Bonus winner

I am pleased to announce that I am one of the 2017 "Google Open Source Peer Bonus winners" for my work on pcsc-lite and the CCID driver.

The latest round of Google Open Source Peer Bonus winners

Monday, March 27, 2017
Google relies on open source software throughout our systems, much of it written by non-Googlers. We’re always looking for ways to say “thank you!” so 5 years ago we started asking Googlers to nominate open source contributors outside of the company who have made significant contributions to codebases we use or think are important. We’ve recognized more than 500 developers from 30+ countries who have contributed their time and talent to over 400 open source projects since the program’s inception in 2011. Today we are pleased to announce the latest round of awardees, 52 individuals we’d like to recognize for their dedication to open source communities. The following is a list of everyone who gave us permission to thank them publicly:
Name Project Name Project
Philipp Hancke Adapter.js Fernando Perez Jupyter & IPython
Geoff Greer Ag Michelle Noorali Kubernetes & Helm
Dzmitry Shylovich Angular Prosper Otemuyiwa Laravel Hackathon Starter
David Kalnischkies Apt Keith Busch Linux kernel
Peter Mounce Bazel Thomas Caswell matplotlib
Yuki Yugui Sonoda Bazel Tatsuhiro Tsujikawa nghttp2
Eric Fiselier benchmark Anna Henningsen Node.js
Rob Stradling Certificate Transparency Charles Harris NumPy
Ke He Chromium Jeff Reback pandas
Daniel Micay CopperheadOS Ludovic Rousseau PCSC-Lite, CCID
Nico Huber coreboot Matti Picus PyPy
Kyösti Mälkki coreboot Salvatore Sanfilippo Redis
Jana Moudrá Dart Ralf Gommers SciPy
John Wiegley Emacs Kevin O'Connor SeaBIOS
Alex Saveau FirebaseUI-Android Sam Aaron Sonic Pi
Toke Hoiland-Jorgensen Flent Michael Tyson The Amazing Audio Engine
Hanno Böck Fuzzing Project Rob Landley Toybox
Luca Milanesio Gerrit Bin Meng U-Boot
Daniel Theophanes Go programming language Ben Noordhuis V8
Josh Snyder Go programming language Fatih Arslan vim-go
Brendan Tracey Go programming language Adam Treat WebKit
Elias Naur Go on Mobile Chris Dumez WebKit
Anthonios Partheniou Google Cloud Datalab Sean Larkin Webpack
Marcus Meissner gPhoto2 Tobias Koppers Webpack
Matt Butcher Helm Alexis La Goutte Wireshark dissector for QUIC
Congratulations to all of the awardees, past and present! Thank you for your contributions.
By Helen Hu, Open Source Programs Office

The bonus (I should receive) is a credit card with 250$.

That is not much compared to Google annual profit. Google operating profit for 2016 was 30 419 M$.
My bonus part is 250 / 30 419 000 000 = 0.000 000 008 218 548.

That is not much for Google, but this is twice the amount I received in bitcoin for now ("How to help my projects? Send me bitcoins!").

Friday, March 31, 2017

"PC/SC" sample in Objective-C (synchronous)

To continue the list of PC/SC wrappers initiated in 2010 with "PC/SC sample in different languages" I now present a new sample in Objective-C using the Apple Crypto Token Kit API.

I already proposed a sample code in Objective-C in "PCSC sample in Objective-C". This code used the asynchronous version of sendIns. The API is:
- (void)sendIns:(UInt8)ins 
           data:(NSData *)requestData 
             le:(NSNumber *)le 
          reply:(void (^)(NSData *replyData, UInt16 sw, NSError *error))reply;

The method returns immediately and a callback reply block is executed when the card response is received.

We will now use the synchronous version of sendIns. The API is:
- (NSData *)sendIns:(UInt8)ins 
               data:(NSData *)requestData 
                 le:(NSNumber *)le 
                 sw:(UInt16 *)sw 
              error:(NSError * _Nullable *)error;

Crypto Token Kit API

In Yosemite (Mac OS X 10.10) Apple introduced a new API to access smart cards. See OS X Yosemite and smart cards status.
This API is not a wrapper above PC/SC. It is the native API to be used on macOS. You do not need to install it, it comes with the OS.

Since PC/SC is not used here the blog title may be misleading. So I used " " around PC/SC this time.

Source code

Create a new Cocoa application in Xcode. You need to enable the App Sandbox and add/set the entitlement to yes.

My sample HellloWorld application does not use Cocoa. It is a text only application.

#import <CryptoTokenKit/CryptoTokenKit.h>

int main(int argc, const char * argv[])
    TKSmartCardSlotManager * mngr;
    mngr = [TKSmartCardSlotManager defaultManager];

    // Use the first reader/slot found
    NSString *slotName = (NSString *)mngr.slotNames[0];
    NSLog(@"slotName: %@", slotName);

    dispatch_semaphore_t sem = dispatch_semaphore_create(0);

    // connect to the slot
    [mngr getSlotWithName:slotName reply:^(TKSmartCardSlot *slot)
         // connect to the card
         TKSmartCard *card = [slot makeSmartCard];
         if (nil == card)
             NSLog(@"No card found");

             // signals end of getSlotWithName block


         // begin a session
         [card beginSessionWithReply:^(BOOL success, NSError *error)
              if (success)
                  NSData *response;
                  UInt16 sw;
                  NSString *newString;

                  // explicitly set the CLA byte even if 0 is already the default value
                  card.cla = 0x00;

                  // send 1st APDU
                  uint8_t aid[] = {0xA0, 0x00, 0x00, 0x00, 0x62, 0x03, 0x01, 0x0C, 0x06, 0x01};
                  NSData *data = [NSData dataWithBytes:aid length:sizeof aid];

                  response = [card sendIns:0xA4 p1:0x04 p2:0x00 data:data le:nil sw:&sw error:&error];

                  if (nil == response)
                      NSLog(@"sendIns error: %@", error);
                      goto out;

                  NSLog(@"Response: %@ 0x%04X", response, sw);

                  // send 2nd APDU
                  response = [card sendIns:0x00 p1:0x00 p2:0x00 data:nil le:@0 sw:&sw error:&error];

                  if (nil == response)
                      NSLog(@"sendIns error: %@", error);
                      goto out;

                  NSLog(@"Response: %@ 0x%04X", response, sw);

                  newString = [[NSString alloc] initWithData:response encoding:NSASCIIStringEncoding];
                  NSLog(@"%@", newString);

                  // end the session
                  [card endSession];
                  NSLog(@"Session error: %@", error);

              // signals end of beginSessionWithReply block

    // wait for the asynchronous blocks to finish
    dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);

    return 0;


2017-03-31 10:54:24.990581+0200 HelloWorld[19931:85555] slotName: Gemalto PC Twin Reader
2017-03-31 10:54:25.103855+0200 HelloWorld[19931:85584] Response: <> 0x9000
2017-03-31 10:54:25.115946+0200 HelloWorld[19931:85584] Response: <48656c6c 6f20776f="" 726c6421=""> 0x9000
2017-03-31 10:54:25.115993+0200 HelloWorld[19931:85584] Hello world!


Compared to the previous Objective-C sample in "PCSC sample in Objective-C" this code has some improvements/bugs fixes:
  • [card endSession]; is called.
    This is needed to close the session started by [card beginSessionWithReply:...].
  • The main thread is waiting for the callbacks from [mngr getSlotWithName:...] and [card beginSessionWithReply:...] to finish using a semaphore (instead of a sleep()).

The CryptoTokenKit API provides a in​Session​With​Error:​execute​Block: to synchronously begin a session instead of using begin​Session​With​Reply:​ and end​Session. But this method has some limitations/bugs and is not (yet) easy to use. I may use it in a next sample code when it will be fixed (in macOS 10.13?).


In general, I prefer to use synchronous calls. So the possibility to use a synchronous sendIns: method is nice.
Depending on your needs, the CryptoTokenKit TKSmart​Card API offers you the choice between a synchronous or asynchronous version.

Wednesday, March 22, 2017

Gemalto IDBridge K30, K50, CT30 and Zero Length Packet issue

Some Gemalto smart card readers with a specific firmware version have a problem and send bogus USB frames.

Possibly affected devices

Some firmware versions of the following devices are affected
  • K30
  • CT30


If you generate a pcscd log as described in "How to get support" and see:
00000023 commands.c:2251:SetParameters() length: 7 bytes
00000037 -> 000000 61 07 00 00 00 00 0E 01 00 00 18 10 02 58 00 FE 00
00001776 <- 000000
00000025 commands.c:2271:SetParameters() Not enough data received: 0 bytes

Then your reader may suffer from the Zero Length Packet issue. The reader, sometimes, sends a USB packet with no data (so a size of 0 bytes).


The problem is that it is not possible to specifically and uniquely identify the problematic devices.

In the CCID driver version 1.4.11 (12 June 2013) I added two patches ("ReadUSB: Zero Length Packet (ZLP) support", "Add Zero Length Packet (ZLP) support for Gemalto IDBridge CT30 and K30") to circumvent the problem.

But this code caused problem with some smart card reader that had not the problem and failed to work with the "fix" ("[#314691] Gemalto ID Bridge K50 Smart card Reader does not work on USB2 with ZLP fix").

So in CCID driver version 1.4.19 (13 May 2014) I removed the patch in "Remove ZLP patch for Gemalto IDBridge CT30 and K30.".


I added the patch again in the CCID driver ("Add Zero Length Packet (ZLP) support for Gemalto IDBridge CT30 and K30", "ZLP: enable the patch only if --enable-zlp is used") but this time the patch is disabled by default. So no bad side effect can happen.

You have to build the driver using the configure option --enable-zlp to explicitly enable the modification.

The new code will be provided in the next CCID driver release: 1.4.27 (not yet released).


I am sorry to not be able to correctly circumvent the problem in the CCID driver itself.

If your reader has the problem then you will have to (re)build the CCID driver yourself at each new CCID driver upgrade.

[Update] 21 May 2017

The CCID driver version 1.4.27 is now available.