0% found this document useful (0 votes)
44 views3 pages

PCSC Sample in Ruby

This document summarizes a blog post about using a PC/SC wrapper library in Ruby to communicate with smart cards. It provides installation instructions for the PC/SC wrapper gem, sample code to select an applet and send test APDUs to a card, and output showing the response is "Hello world!". The code demonstrates connecting to a reader, selecting an applet, transmitting commands, and disconnecting from the card.

Uploaded by

qaz qazy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views3 pages

PCSC Sample in Ruby

This document summarizes a blog post about using a PC/SC wrapper library in Ruby to communicate with smart cards. It provides installation instructions for the PC/SC wrapper gem, sample code to select an applet and send test APDUs to a card, and output showing the response is "Hello world!". The code demonstrates connecting to a reader, selecting an applet, transmitting commands, and disconnecting from the card.

Uploaded by

qaz qazy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

12/14/2017 Ludovic Rousseau's blog: PCSC sample in Ruby

更多 下一个博客» 创建博客 登录

Ludovic Rousseau's blog


My activities related to smart card and Free Software (as in free speech).

Tuesday, June 8, 2010 Google+ Badge

PCSC sample in Ruby Ludovic Rousseau blog

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

A PC/SC Wrapper for Ruby also exists at Smart­card communication, straight from ruby.. The latest version
is 0.4.11 from 2009. I used an older version 0.3.1 when I worked with this wrapper.
Blog Archive

Installation ► 2017 (33)


► 2016 (49)
You first need to install ruby and the other components needed for the compilation of smartcard for Ruby.
► 2015 (51)
► 2014 (61)
apt‐get install ruby rake rubygems libopenssl‐ruby ruby1.8‐dev
► 2013 (38)
► 2012 (27)

Then fetch the archive smartcard­0.4.11.gem ► 2011 (46)


▼ 2010 (55)
► December (5)

$ sudo gem install echoe ► November (5)

[...] ► October (9)


$ unzip smartcard.zip ► September (1)
$ cd smartcard ► August (8)
$ rake manifest
► July (1)
[...]
▼ June (10)
$ rake package
New version of pcsc­perl: 1.4.9
(in /home/rousseau/HSLM/smartcard)
PyKCS11 history
Successfully built RubyGem
Name: smartcard PyKCS11 introduction
Version: 0.3.1 Update on pcsc­lite security
advisory CVE­2010­040...
File: smartcard‐0.3.1.gem
Private key not found; gem will not be signed. PCSC sample in Java
Targeting "ruby" platform. pcsc­lite security advisory
CVE­2010­0407
$ rake test
(in /home/rousseau/HSLM/smartcard) PCSC sample in Ruby
/usr/bin/ruby1.8 extconf.rb PCSC sample in Prolog
checking for main() in ‐lpcsclite... yes Comments on my blog
checking for wintypes.h... yes PCSC sample in OCaml
checking for reader.h... yes
checking for winscard.h... yes ► May (6)

checking for pcsclite.h... yes ► April (10)


creating Makefile
[...]
Search This Blog
/usr/bin/ruby1.8 ‐Ilib:ext:bin:test "/var/lib/gems/1.8/gems/rake‐0.8.3/lib/rake/rake_test_loa
der.rb" "test/test_containers.rb" "test/test_smoke.rb" Search
Loaded suite /var/lib/gems/1.8/gems/rake‐0.8.3/lib/rake/rake_test_loader
Started Subscribe To
...
Posts
Finished in 0.046223 seconds.
Comments
3 tests, 14 assertions, 0 failures, 0 errors
$ rake docs
Google+ Followers
[...]

Source code

https://ludovicrousseau.blogspot.jp/2010/06/pcsc­sample­in­ruby.html 1/3
12/14/2017 Ludovic Rousseau's blog: PCSC sample in Ruby
Ludovic Rousseau b…
require 'smartcard'
Follow

context = Smartcard::PCSC::Context.new(Smartcard::PCSC::SCOPE_SYSTEM)
readers = context.list_readers nil
context.cancel

# Use the first reader


reader = readers.first

# Connect to the card


card = Smartcard::PCSC::Card.new(context, reader, Smartcard::PCSC::SHARE_SHARED, Smart
card::PCSC::PROTOCOL_ANY)

336 have us in View


# Get the protocol to use circles all
card_status = card.status

# Select applet
aid = [0xA0, 0x00, 0x00, 0x00, 0x62, 0x03, 0x01, 0x0C, 0x06, 0x01]
select_apdu = [0x00, 0xA4, 0x04, 0x00, aid.length, aid].flatten
send_ioreq = {Smartcard::PCSC::PROTOCOL_T0 => Smartcard::PCSC::IOREQUEST_T0,
Smartcard::PCSC::PROTOCOL_T1 => Smartcard::PCSC::IOREQUEST_T1}[card_stat
us[:protocol]]
recv_ioreq = Smartcard::PCSC::IoRequest.new
response = card.transmit(select_apdu.map {|byte| byte.chr}.join(''), send_ioreq, recv_
ioreq)
response_str = (0...response.length).map { |i| ' %02x' % response[i].to_i }.join('')
puts "Answer: #{response_str}\n"

# test APDU
test_apdu = [0, 0, 0, 0]
response = card.transmit(test_apdu.map {|byte| byte.chr}.join(''), send_ioreq, recv_io
req)
response_str = (0...response.length).map { |i| ' %02x' % response[i].to_i }.join('')
puts "Answer: #{response_str}\n"
response_str = (0...response.length‐2).map { |i| '%c' % response[i].to_i }.join('')
puts "Answer: #{response_str}\n"

# Deconnect
card.disconnect Smartcard::PCSC::DISPOSITION_LEAVE
context.release

Output

Answer: 90 00
Answer: 48 65 6c 6c 6f 20 77 6f 72 6c 64 21 90 00
Answer: Hello world!

Conclusion

Nothing more to add. If you are a Ruby user you may be interested by this wrapper.

Labels: code

Newer Post Home Older Post

Bitcoin

https://ludovicrousseau.blogspot.jp/2010/06/pcsc­sample­in­ruby.html 2/3
12/14/2017 Ludovic Rousseau's blog: PCSC sample in Ruby

License: by­nc­sa

This blog by Ludovic Rousseau is licensed under a Creative Commons Attribution­NonCommercial­ShareAlike 3.0 Unported License.

Simple theme. Powered by Blogger.

https://ludovicrousseau.blogspot.jp/2010/06/pcsc­sample­in­ruby.html 3/3

You might also like