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...

Showing posts with label Perl. Show all posts
Showing posts with label Perl. Show all posts

Monday, April 1, 2013

New version of pcsc-perl: 1.4.13

I just released a new version 1.4.13 of pcsc-perl, the Perl wrapper for PC/SC.
This version just fixes a warning when you use Perl 5.16.

 

See the article "PCSC sample in Perl" for code sample of PC/SC in Perl.

pcsc-perl is also available at CPAN: pcsc-perl-1.4.13 with the online API documentation for Chipcard::PCSC and Chipcard::PCSC::Card.

Sunday, December 18, 2011

new version of pcsc-tools: 1.4.18

I just released a new version of pcsc-tools. No new feature but some enhancements.

If you do not know what pcsc-tools is, it contains 4 tools:
  • pcsc_scan(1) regularly scans every PC/SC reader connected to the host if a card is inserted or removed a "line" is printed.
  • ATR_analysis(1) is a Perl script used to parse the smart card ATR. This script is called (by default) by pcsc_scan.
  • scriptor(1) is a Perl script to send commands to a smart card using a batch file or stdin.
  • gscriptor(1) the same idea as scriptor.pl(1) but with a Perl-Gtk2 GUI.

An equivalent of ATR_analysis is available online http://smartcard-atr.appspot.com/

Changes:
1.4.18 - 18 December 2011, Ludovic ROUSSEAU
  • gscriptor: Display hex dumps in lines of 16 bytes instead of 17
  • gscriptor: Display bytes of value 0x20 as ' ' instead of '.'
  • scriptor: Display lines of 16 bytes instead of 24
  • 223 new ATRs
  • pcsc_scan: Correctly detect reader Plug and Play support

Wednesday, November 3, 2010

SIM card phone book listing

During the Debian minconf Paris 2010 I was asked for a program to use a SIM card.

I do not know any program to correctly manage a SIM card in free software on Unix (but I have not really search for it).

SIM explorer v3.0

In 2004 I wrote a simple Perl program to dump the phone book of a SIM card using the PC/SC Perl wrapper. The program is available on my web site.

The program is very rude and only displays the phone book. No way to edit the phone book.

Conclusion

I am surprised to see so little interest in the area of SIM/USIM cards. The market penetration of GSM was already above 100% in 22 European markets in 2006. So I guess every smart card hacker also has one GSM/3G phone. So we do not lack developers.

Note: I do not have a GSM/3G/smart phone myself :-)


Flattr this

Wednesday, June 30, 2010

New version of pcsc-perl: 1.4.9

I just released a new version of pcsc-perl to fix a compilation bug.

The problem


pcsc-lite defined some error codes specific to pcsc-lite. One is SCARD_W_INSERTED_CARD.

This error code is never returned by pcsc-lite and has been removed in pcsc-lite revision 4574 included in pcsc-lite 1.6.0.

Solution


Do not reference this error code any more. The side effect is that if your Perl program uses SCARD_W_INSERTED_CARD it will fail and has to be corrected.

Thanks


Thanks to Olivier Huber for reporting the problem.

See also


A previous blog entry about this Perl PCSC wrapper: PCSC sample in Perl.


Flattr this

Monday, May 3, 2010

PCSC sample in scriptor

Installation


scriptor and gscriptor are tools included in the pcsc-tools package (http://ludovic.rousseau.free.fr/softwares/pcsc-tools/index.html)

The licence is GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

On a Debian system just do:
apt-get install pcsc-tools

Source code for scriptor


These 2 tools allows to send APDU to a card. They are very simple by yet useful for some basic cases.

If used in interactive mode (use Ctrl-d to quit):

$ scriptor
No reader given: using Gemplus GemPC Twin 00 00
Using T=1 protocol
Reading commands from STDIN
00 A4 04 00 0A A0 00 00 00 62 03 01 0C 06 01
> 00 A4 04 00 0A A0 00 00 00 62 03 01 0C 06 01
< 90 00 : Normal processing.
00 00 00 00
> 00 00 00 00
< 48 65 6C 6C 6F 20 77 6F 72 6C 64 21 90 00 : Normal processing.


If used as a script file:

$ cat essai.scriptor
#! /usr/bin/env scriptor
reset
# Select applet
00 A4 04 00 0A A0 00 00 00 62 03 01 0C 06 01
# APDU de test
00 00 00 00


Empty lines or lines starting by # are comments and ignored. reset is a keyword to reset the card and start with a known state.

Output


$ ./essai.scriptor
No reader given: using Gemplus GemPC Twin 00 00
Using T=1 protocol
Using given file: ./essai.scriptor
#! /usr/bin/env scriptor
reset
> RESET
< OK: 3B EA 00 00 81 31 20 43 80 65 A2 01 01 01 3D 72 D6 43 A5
# Select applet
00 A4 04 00 0A A0 00 00 00 62 03 01 0C 06 01
> 00 A4 04 00 0A A0 00 00 00 62 03 01 0C 06 01
< 90 00 : Normal processing.
# APDU de test
00 00 00 00
> 00 00 00 00
< 48 65 6C 6C 6F 20 77 6F 72 6C 64 21 90 00 : Normal processing.


Source code for gscriptor


The same script can be used by gscriptor. gscriptor is a graphical version of scriptor.

Output




gscriptor can display the results in ASCII. This is useful in our case since the test APDU just sends the string "Hello world!"



Conclusion


scriptor and gscriptor are easy to use for simple tasks. In general I use them to check the smart card stack is working correctly.


Flattr this

Thursday, April 22, 2010

PCSC sample in Perl

Here is the PCSC sample in Perl language I promised in PC/SC sample in different languages.

Installation

Get the source code from http://ludovic.rousseau.free.fr/softwares/pcsc-perl/. The current version is 1.4.8. If you distribution does not provide a package (Debian does with libpcsc-perl) you can install it by hand using:

pcsc-perl-1.4.8$ perl Makefile.PL 
osname: linux
LDDFLAGS: 
INC: `pkg-config --cflags libpcsclite`
Checking if your kit is complete...
Looks good
Writing Makefile for Chipcard::PCSC::Card
Writing Makefile for Chipcard::PCSC
pcsc-perl-1.4.7$ make
[...]
pcsc-perl-1.4.7$ make test
[...]
pcsc-perl-1.4.7$ make install
[...]

The wrapper works on GNU/Linux, Mac OS X and Windows.

API


The API documentation is available online at http://ludovic.rousseau.free.fr/softwares/pcsc-perl/PCSC.html and http://ludovic.rousseau.free.fr/softwares/pcsc-perl/Card.html.

You can also have a look at the project page project page on CPAN (Comprehensive Perl Archive Network).

Source code


#!/usr/bin/perl -w

use Chipcard::PCSC;

# create a new object
$hContext = new Chipcard::PCSC();
die ("Can't create the PCSC object: $Chipcard::PCSC::errno\n")
    unless defined $hContext;

# get the reader list
@ReadersList = $hContext->ListReaders();
die ("Can't get readers' list: $Chipcard::PCSC::errno\n")
    unless defined $ReadersList[0];

# connect to the first reader
$hCard = new Chipcard::PCSC::Card($hContext, $ReadersList[0]);
die ("Can't connect: $Chipcard::PCSC::errno\n")
    unless defined $hCard;

# send the Select Applet APDU
$cmd = Chipcard::PCSC::ascii_to_array("00 A4 04 00 0A A0 00 00 00 62 03 01 0C 06 01");
$RecvData = $hCard->Transmit($cmd);
die ("Can't transmit data: $Chipcard::PCSC::errno") unless defined $RecvData;
print Chipcard::PCSC::array_to_ascii($RecvData)."\n";

# send the test APDU
$cmd = Chipcard::PCSC::ascii_to_array("00 00 00 00");
$RecvData = $hCard->Transmit($cmd);
die ("Can't transmit data: $Chipcard::PCSC::errno") unless defined $RecvData;
print Chipcard::PCSC::array_to_ascii($RecvData)."\n";

$hCard->Disconnect();

Output


90 00
48 65 6C 6C 6F 20 77 6F 72 6C 64 21 90 00

Lessons learned


Portability


The same code can be used on any plateform. No more #ifdef like in C.

Low level API


The API is still low level and just wrap PC/SC calls from C to Perl.

Higher level API


TransmitWithCheck() is a little more easy to use than Transmit(). This method does the split between data and status word.

In the example above replace the two last blocks with:

# Send the Select Applet APDU
($sw, $RecvData) = $hCard->TransmitWithCheck("00 A4 04 00 0A A0 00 00 00 62 03 01 0C 06 01", "90 00");
die ("Can't transmit data: $Chipcard::PCSC::errno") unless defined $sw;
print $RecvData."\n";
print Chipcard::PCSC::Card::ISO7816Error($sw) . " ($sw)\n";

# Send the test APDU
($sw, $RecvData) = $hCard->TransmitWithCheck("00 00 00 00", "90 00");
die ("Can't transmit data: $Chipcard::PCSC::errno") unless defined $sw;
print $RecvData."\n";
print map { chr hex $_ } split ' ', $RecvData;
print "\n";
print Chipcard::PCSC::Card::ISO7816Error($sw) . " ($sw)\n";

This sample code also uses Chipcard::PCSC::Card::ISO7816Error($sw) to transform the status word is something human readable like Normal processing. for 90 00.

Output


Normal processing. (90 00)
48 65 6C 6C 6F 20 77 6F 72 6C 64 21 
Hello world!
Normal processing. (90 00)


Flattr this