In a previous blog article "cardpeek: A tool to read the contents of smartcards" I presented an application using the lua language to interact with smart cards.
PC/SC access from lua
Cardpeek is a complete application and not a PC/SC wrapper. The cardpeek author implemented only what was needed for the application instead of a general PC/SC wrapper.The card functions available from lua are:
- connect
 - disconnect
 - warm_reset
 - last_atr
 - info
 - send
 - set_command_interval
 - make_file_path
 
One important missing function is an equivalent of
SCardListReaders(). The list of available readers and selection of the reader to use is not available from  the lua code. The reader selection is done by the cardpeek application using C code directly.Some work is missing in order to have a complete PC/SC wrapper for lua.
Source code
function hex_tostring(data)
 local r = ""
 local i
 for i=0,#data-1 do
  r = r .. string.char(data[i]) 
 end 
 return r
end
if card.connect() then
 card.tree_startup("ATR")
 -- Select applet
 select = bytes.new(8, "00 A4 04 00 0A A0 00 00 00 62 03 01 0C 06 01")
 sw, resp = card.send(select)
 print(string.format("SW: %X", sw))
 print(resp)
 -- Send command
 command = bytes.new(8, "00 00 00 00")
 sw, resp = card.send(command)
 print(string.format("SW: %X", sw))
 print(hex_tostring(resp))
 card.disconnect()
end
Remarks
The output is sent to the console using the luaprint() statement. You could also send the output to cardpeek using log.print() instead.Output
In the console
SW: 9000 SW: 9000 Hello world!
In cardpeek
Conclusion
I reported some improvements for cardpeek (issues 13, 14, 15 and 16). One month after, the issues are still open and the proposed patches not applied or reviewed. A bad news concerning the state of the cardpeek project :-(.This pseudo-wrapper is the 12th wrapper for PC/SC. What will be the next language?


