aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKevin Redon <kevredon@mail.tsaitgaist.info>2011-05-05 23:10:44 +0200
committerKevin Redon <kevredon@mail.tsaitgaist.info>2011-05-05 23:10:44 +0200
commitbb1c11fad4b8f1db8a4b3c89cabb8638b30d28f6 (patch)
treed3857a057e9bf160d754d0e68c41a3e83d9e4b31
parent238fa1025e6a6b950a85a307a2e4588f452af66b (diff)
client is now CLI + more apdu commands used
-rwxr-xr-xsrc/demo_client.rb154
1 files changed, 114 insertions, 40 deletions
diff --git a/src/demo_client.rb b/src/demo_client.rb
index a739ec9..b13aa11 100755
--- a/src/demo_client.rb
+++ b/src/demo_client.rb
@@ -21,40 +21,23 @@ Copyright (C) 2011 Kevin "tsaitgaist" Redon kevredon@mail.tsaitgaist.info
require 'lib/client'
require 'lib/apdu'
-# wich IO to use (:tcp,:unix,:bt)
-client_io = :tcp
+#=============
+#== default ==
+#=============
+
+# which IO to use (tcp,unix,bt)
+@socket = "tcp"
+# tcp port
+@port = 1337
+# tcp host
+@host = "localhost"
+# unix socket
+@unix = "/tmp/sap.socket"
+# bluetooth rfcomm serial port
+@bt = nil
# the verbosity (from common)
$verbosity = 1
-#=================
-#== client type ==
-#=================
-
-# create IO
-case client_io
-when :tcp
- require 'socket'
- host = "localhost"
- port = 1337
- io = TCPSocket.open(host,port)
-when :bt
- require 'lib/bluetooth_sap_serial'
- #sudo gem install serialport (http://rubygems.org/gems/serialport)
- require 'rubygems'
- require 'serialport'
- bt = BluetoothSAPSerial.new
- # using SerialPort because reading the File does not work (have to find right stty options)
- io = SerialPort.new(bt.connect)
-else
- raise "please defined which client to use"
-end
-
-#===============
-#== constants ==
-#===============
-
-
-
#=============
#== methods ==
#=============
@@ -66,24 +49,115 @@ def transmit_apdu(apdu)
return @client.apdu(apdu)
end
+# show help
+def print_help
+ puts "demo_client.rb [options]"
+ puts ""
+ puts "demonstration SAP client connecting to an indicated SAP server"
+ puts "it executes some common commands"
+ puts ""
+ puts "options :"
+ puts " --help,-h\t\tprint this help"
+ puts " --socket,-s type\tsocket type : tcp,unix,bt (default #{@socket})"
+ puts " --tcp,-t port\t\ttcp port (default #{@port})"
+ puts " --host,-l host\t\ttcp host (default #{@host})"
+ puts " --unix,-u file\t\tunix socket (default #{@unix})"
+ puts " --bluetooth,-s rfcomm\tbluetooth rfcomm serial port (default #{@bt ? @bt : 'self discovery'})"
+ puts " --verbosity,-v\t\tdebug verbosity 0..5 (default #{$verbosity})"
+end
+
#==========
#== main ==
#==========
+# parse CLI arguments
+while arg=ARGV.shift do
+
+ case arg
+ when "--help","-h"
+ print_help
+ exit 0
+ when "--socket","-s"
+ param = ARGV.shift
+ @socket = param if param
+ when "--tcp","-t"
+ param = ARGV.shift.to_i
+ @tcp = param if param
+ when "--host","-l"
+ param = ARGV.shift
+ @host = param if param
+ when "--unix","-u"
+ param = ARGV.shift
+ @unix = param if param
+ when "--bluetooth","-s"
+ param = ARGV.shift
+ @bt = param if param
+ when "--verbosity","-v"
+ param = ARGV.shift.to_i
+ $verbosity = param if param
+ else
+ puts "unknown argument #{arg}"
+ exit 0
+ end
+end
+
+# create IO
+case @socket
+when "tcp"
+ require 'socket'
+ io = TCPSocket.open(@host,@port)
+when "unix"
+ require 'socket'
+ io = UNIXSocket.open(@unix)
+when "bt"
+ #sudo gem install serialport (http://rubygems.org/gems/serialport)
+ require 'rubygems'
+ require 'serialport'
+ if @bt then
+ io = SerialPort.new(@bt)
+ else
+ require 'lib/bluetooth_sap_serial'
+ bt = BluetoothSAPSerial.new
+ # using SerialPort because reading the File does not work (have to find right stty options)
+ io = SerialPort.new(bt.connect)
+ end
+else
+ raise "please defined which socket to use"
+end
+
+# create client
@client = Client.new(io)
@client.start
@client.connect
+
+# get ATR
atr = @client.atr
puts atr ? "ATR : #{atr.to_hex_disp}" : "could not get ATR"
-# select MF
-select(MF)
+
+# get IMSI
+imsi = read_ef([MF,DF_GSM,EF_IMSI])
+# byte 1 is the length of the IMSI
+imsi_length = imsi[0]
+imsi = imsi[1,imsi_length]
+# first nibble is for parity check (not done)
+imsi = imsi.nibble_str[1..-1]
+puts "IMSI : "+imsi
+
+# run A38 algo
+# the rands
+rands = []
+4.times do |i|
+ rands << [(i<<4)+i]*16
+end
+# the results
+puts "some KCs (RAND SRES Kc) :"
+rands.each do |r|
+ response = a38(r)
+ puts " - #{r.to_hex_disp.gsub(' ','')} #{response[0,4].to_hex_disp.gsub(' ','')} #{response[4..-1].to_hex_disp.gsub(' ','')}"
+end
+
@client.disconnect
# close client_io
-case client_io
-when :tcp
- io.close
-when :bt
- io.close
- bt.close
-end
+io.close
+bt.close if @socket=="bt" and !@bt