10
0
Fork 0

better verbosity

This commit is contained in:
Kevin Redon 2011-05-02 20:03:41 +02:00
parent 93aa6bc904
commit 560c14687b
6 changed files with 31 additions and 39 deletions

View File

@ -17,18 +17,19 @@ along with SAP. If not, see <http://www.gnu.org/licenses/>.
Copyright (C) 2011 Kevin "tsaitgaist" Redon kevredon@mail.tsaitgaist.info
=end
# This programm will create a client which can be used to test servers
# this programm will create a client which can be used to test servers
require 'lib/client'
require 'lib/apdu'
# wich IO to use (:tcp,:unix,:bt)
client_io = :tcp
# the verbosity (from common)
$verbosity = 1
#=================
#== client type ==
#=================
# wich IO to use
client_io = :tcp
# the IO itself
io = nil
# create IO
case client_io
when :tcp
@ -41,11 +42,6 @@ when :bt
#sudo gem install serialport (http://rubygems.org/gems/serialport)
require 'rubygems'
require 'serialport'
=begin
to monitor bluetooth traffic
sudo aptitude install bluez-hcidump
sudo hcidump -x -i hci0 rfcomm
=end
bt = BluetoothSAPSerial.new
# using SerialPort because reading the File does not work (have to find right stty options)
io = SerialPort.new(bt.connect)
@ -57,11 +53,7 @@ end
#== constants ==
#===============
# to debug the program
# shows APDU IO
DEBUG = true
# the verbosity from common
VERBOSE = 0
#=============
#== methods ==
@ -78,13 +70,12 @@ end
#== main ==
#==========
@client = Client.new(io,VERBOSE)
@client = Client.new(io)
@client.start
@client.connect
atr = @client.atr
puts atr ? "ATR : #{atr.to_hex_disp}" : "could not get ATR"
# select MF
transmit(GET_RESPONSE+[0x1a])
select(MF)
@client.disconnect

View File

@ -18,22 +18,23 @@ along with SAP. If not, see <http://www.gnu.org/licenses/>.
Copyright (C) 2011 Kevin "tsaitgaist" Redon kevredon@mail.tsaitgaist.info
=end
# this program is there to start a server
require 'socket'
require 'pcsc_server'
require 'sim_server'
# the io the server should use (:tcp, :unix)
# the io the server should use (:tcp,:unix)
io_type = :tcp
# the server to use (:pcsc, :sim)
# the server to use (:pcsc,:sim)
server_type = :pcsc
# the verbosity (from common)
$verbosity = 1
# create the IO
case io_type
when :tcp
require 'socket'
TCP_HOST = "localhost"
TCP_PORT = "1337"
socket = TCPServer.new(TCP_HOST,TCP_PORT)
when :unix
require 'socket'
UNIX = "/tmp/sap_server.socket"
socket = UNIXServer.new(APDU_SOCKET)
else
@ -45,8 +46,10 @@ io = socket.accept
case server_type
when :pcsc
require 'pcsc_server'
server = PCSCServer.new(io)
when :sim
require 'sim_server'
server = SIMServer.new(io)
else
raise "unkown server type"

View File

@ -117,10 +117,7 @@ module APDU
def transmit(apdu)
# send APDU
puts "< "+apdu.to_hex_disp if DEBUG
resp = transmit_apdu(apdu)
puts "> "+resp.to_hex_disp if DEBUG
# parse response
response = resp[0..-3]
sw1 = resp[-2]

View File

@ -30,8 +30,8 @@ class Client < SAP
# make the class abstract
private :initialize
def initialize(io,verbosity=SAP::VERBOSE)
super(io,verbosity)
def initialize(io)
super(io)
# state of the state machine
@state = nil
@ -181,7 +181,7 @@ class Client < SAP
end
end
# return ATR (byts array)
# return ATR (byte array)
def atr
if @state==:idle then
connect = create_message("TRANSFER_ATR_REQ")
@ -191,6 +191,7 @@ class Client < SAP
until @state==:idle
sleep @wait_time
end
log("ATR","#{hex(@atr)}",1)
return @atr
else
raise "can not ask ATR. must be in state idle, current state : #{@state}"
@ -201,6 +202,7 @@ class Client < SAP
# return the response of the apdu request
def apdu(request)
raise "APDU request empty" unless request and request.size>=5
log("APDU","< #{hex(request)}",1)
if @state==:idle then
# ["CommandAPDU",[apdu]]
connect = create_message("TRANSFER_APDU_REQ",[[0x04,request]])
@ -210,6 +212,7 @@ class Client < SAP
until @state==:idle
sleep @wait_time
end
log("APDU","> #{hex(@apdu)}",1)
return @apdu
else
raise "can not sen APDU request. must be in state idle, current state : #{@state}"

View File

@ -16,8 +16,6 @@ along with SAP. If not, see <http://www.gnu.org/licenses/>.
Copyright (C) 2011 Kevin "tsaitgaist" Redon kevredon@mail.tsaitgaist.info
=end
require 'stringio'
# the common part of SAP server and client
# it includes :
# - constants
@ -155,12 +153,8 @@ class SAP
# create a new SAP client/server
# - io : the Input/Output to monitor
def initialize(io,verbosity=VERBOSE)
def initialize(io)
# the verbose output
#@verbose = StringIO.new # no output
@verbose = $> # std output
@verbosity = verbosity
# this has to be defined in child class
# @socket can be any IO
@io = io
@ -203,6 +197,7 @@ class SAP
end
end
# set the new state of the state machine
def set_state (new_state)
if @state then
log("state","state changed from #{@state} to #{new_state}",2)
@ -242,7 +237,7 @@ class SAP
end
# client : ask for the ATR from SAP server (must be connected)
# server : get ATR from SIM card
# server : get ATR from SIM card (or SAP result code)
# return : ATR
def atr
raise NotImplementedError
@ -450,13 +445,13 @@ class SAP
# - 3 : inner task (green)
# - 4 : messages (red)
# - 5 : byte traffic
VERBOSE = 5
$verbosity = 5 unless $verbosity
# for the logs
def log (group,message,level)
if @verbosity and @verbosity>=level then
if $verbosity and $verbosity>=level then
color = 95-level
@verbose.puts "\e[1m\e[#{color}m[#{group}]\e[0m #{message}"
puts "\e[1m\e[#{color}m[#{group}]\e[0m #{message}"
end
end

View File

@ -108,6 +108,7 @@ class Server < SAP
atr_result = atr
payload = []
if atr_result.kind_of?(Array) then
log("ATR","#{hex(atr_result)}",1)
# ["ResultCode",["OK, request processed correctly"]]
payload << [0x02,[0x00]]
# ["ATR",atr]
@ -127,7 +128,9 @@ class Server < SAP
set_state :processing_apdu_request
# apdu should return APDU response byte array, or error result code
raise "no APDU request in message" unless message[:payload].size==1
log("APDU","> #{hex(message[:payload][0][:value])}",1)
apdu_result = apdu(message[:payload][0][:value])
log("APDU","< #{hex(apdu_result)}",1)
payload = []
if apdu_result.kind_of?(Array) then
# ["ResultCode",["OK, request processed correctly"]]