Important!

Blog moved to https://blog.apdu.fr/

I moved my blog from https://ludovicrousseau.blogspot.com/ to https://blog.apdu.fr/ . Why? I wanted to move away from Blogger (owne...

Wednesday, September 24, 2014

PCSC sample in JavaScript (Node.js)

To continue the list of PC/SC wrappers initiated more than four years ago with "PC/SC sample in different languages" I now present a PC/SC sample written in JavaScript using Node.js.

node-pcsclite project

The node-pcsclite project is hosted at github and is quiet active. Support of Windows is not yet available. Support of Mac OS X is now correct after I proposed some patches.

The installation on a Debian unstable or testing (Jessie) system is easy. Just follow the project documentation. Debian stable (Whezzy) do not have the nodejs packages but these packages are available in wheezy-backports.

One potential problem is that the Node.js binary is called nodejs on Debian to avoid a conflict with another node binary. To have a node binary corresponding to Node.js you need to install the Debian package nodejs-legacy. It is not difficult but may be the source of some difficulties at the beginning.

PC/SC accesses from node-pcsclite

The wrapper provides access the following PC/SC functions:
  • connect
  • disconnect
  • transmit
  • control

A reader event (reader removed) is reported as an event.

The card status change is reported as an event.

The reconnect function is missing. A bug #10 is open requesting its addition.

Sample source code

#!/usr/bin/env node

var pcsc = require('./lib/pcsclite');

var pcsc = pcsc();

pcsc.on('reader', function(reader) {

    function exit() {
        reader.close();
        pcsc.close();
    }

    cmd_select = new Buffer([0x00, 0xA4, 0x04, 0x00, 0x0A, 0xA0, 0x00, 0x00, 0x00, 0x62, 0x03, 0x01, 0x0C, 0x06, 0x01]);
    cmd_command = new Buffer([0x00, 0x00, 0x00, 0x00]);

    console.log('Using:', reader.name);

    reader.connect(function(err, protocol) {
        if (err) {
            console.log(err);
            return exit();
        }
        reader.transmit(cmd_select, 255, protocol, function(err, data) {
            if (err) {
                console.log(err);
                return exit();
            }
            console.log('Data received', data);
            reader.transmit(cmd_command, 255, protocol, function(err, data) {
                if (err) {
                    console.log(err);
                } else {
                    console.log('Data received', data);
                    console.log('Data received', data.toString());
                }
                return exit();
            });
        });
    });
});

pcsc.on('error', function(err) {
    console.log('PCSC error', err.message);
});

Remarks

Node.js is an asynchronous framework. So a typical Node.js design pattern is to use a call-back instead of blocking the execution of a function.

The code can be complex to follow since you have a cascade of call-backs if you need to send a sequence of APDU. In the sample we only need to send 2 consecutive APDU.

The program is not sequential but event based. So without the explicit exit after 1 second the program never terminates and you need to stop it using Control-C. It is strange for me.

Output

Using: Gemalto PC Twin Reader 00 00
Data received <SlowBuffer 90 00>
Data received <SlowBuffer 48 65 6c 6c 6f 20 77 6f 72 6c 64 21 90 00>
Data received Hello world!�

Similar projects

Two other similar projects are also found at github. They have both the same name node-pcsc but are not the same project:

coolbong node-pcsc

The node-pcsc interface from coolbong uses a synchronous API so no call-back are involved for PC/SC calls. You can send a sequence of APDU as you would do in C.

This wrapper is for Windows only and need some work to port it to Unix. I opened a bug #1 requesting Unix support.

jokesterfr node-pcsc

This wrapper is not yet able to send arbitrary APDU to a card. It looks like a work in progress that stopped in November 2013.

Conclusion

If you want to use a smart card from a JavaScript program using Node.js the best choice may be the node-pcsclite project. The project maintainer is nice and reactive.

If you know a PC/SC wrapper that is not yet in my list then please contact me.

Edit, October 3rd 2014

After discussing with Santiago Gimeno (node-pcsclite author) and fixing Mac OS X bugs in node-pcsclite I modified the sample source code to add the clean up exit() function and exit properly from the program when no more callbacks are waiting.

New version of pcsc-lite: 1.8.12

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

Changes:
pcsc-lite-1.8.12: Ludovic Rousseau
24 September 2014
  • make hotplug using libudev (default) more robust
  • add ReiserFS file system support (for configuration files)
  • add musl libC support (increase the thread stack)
  • Some other minor improvements and bug corrections

Saturday, September 20, 2014

Open Silicium n°12 en kiosque !

The french magazine Open Silicium latest number Sept., Oct., Nov. 2014 has NFC as its main topic.

If you can read French and you are interested in NFC it is a good reading.



Table of content

Sorry, it is in French.

Au sommaire du magazine :

News

4 La plateforme de développement mbed

En couverture

9 Un système RFID/NFC pour notre GNU/Linux
14 Utilisation et manipulation des tags Mifare Classic
24 Pour explorer, utilisons Python !
26 Mifare DESFire : un niveau de sécurité adapté
34 RFID : Quelques applications intéressantes pour Android

Réseau

39 Visualisation temps réel de trafic avec Python et l'API Google Analytics

Mobilité & téléphonie

46 Applications connectées en 3G : pourquoi la ressource radio impacte tant votre batterie
52 Google Projet Ara : Votre smartphone (Android) sur mesure !

Repères

59 Reconstruction de structures tridimensionnelles par photographies : le logiciel MicMac

Code & développement

80 Le bootloader DFU des STM32

Saturday, September 13, 2014

New version of libccid: 1.4.18

I just released a version 1.4.18 of libccid the free software CCID class smart card reader driver.

Changes:
1.4.18 - 13 September 2014, Ludovic Rousseau
  • Add support of
    • Cherry Cherry TC 1100
    • Cherry Smart Card Reader USB
    • Cherry Smartcard Keyboard G87-1xx44
    • FujitsuTechnologySolutions GmbH Keyboard KB SCR2
    • Lenovo Lenovo USB Smartcard Keyboard
    • Yubico Yubikey NEO OTP+U2F+CCID
    • Yubico Yubikey NEO U2F+CCID
    • eID_R6 001 X8
  • fix support of Omnikey CardMan 3121
  • reduce memory consumed when configured with --enable-embedded
  • prepare the port to UEFI

New version of pcsc-tools: 1.4.23

I just released a new version of pcsc-tools, a suite of tools for PC/SC.
The only change is the update of the ATR list.

Changes:
1.4.23 - 13 September 2014, Ludovic ROUSSEAU
  • 137 new ATRs