array to hex function, reduced usb timeout

This commit is contained in:
Christina Quast 2015-04-11 08:42:38 +02:00
parent 95d6616ba1
commit f2e53f0553
3 changed files with 17 additions and 8 deletions

View File

@ -5,6 +5,8 @@ import smartcard.util
import array
from util import HEX
class SmartcardException(Exception):
pass
@ -22,7 +24,7 @@ class SmartcardConnection:
print 'Reader:', reader
print 'State:', state
print 'Protocol:', protocol
print 'ATR:', smartcard.util.toHexString(atr, smartcard.util.HEX)
print 'ATR:', HEX(atr)
return array.array('B', atr)
def reset_card(self):
@ -67,14 +69,13 @@ class SmartcardConnection:
print 'Released context.'
def send_receive_cmd(self, cmd):
print("Cmd: ")
print("Cmd to SIM: " + HEX(cmd))
hresult, resp = SCardTransmit(self.hcard, self.dwActiveProtocol,
cmd.tolist())
if hresult != SCARD_S_SUCCESS:
raise SmartcardException('Failed to transmit: ' +
SCardGetErrorMessage(hresult))
print 'Ans: ' + smartcard.util.toHexString(resp,
smartcard.util.HEX)
print 'SIM Ans: ' + HEX(resp)
return array.array('B', resp)
def disconnect_card(self):

View File

@ -6,6 +6,7 @@ import phone
from contextlib import closing
from util import HEX
def find_dev():
dev = usb.core.find(idVendor=0x03eb, idProduct=0x6004)
@ -36,14 +37,15 @@ ERR_TIMEOUT = 110
def poll_ep(dev, ep):
try:
return dev.read(ep, 64, 1000)
return dev.read(ep, 64, 100)
except usb.core.USBError as e:
if e.errno != ERR_TIMEOUT:
raise
return None
def write_phone(dev, resp):
dev.write(PHONE_WR, resp, 1000)
print("WR: ", HEX(resp))
dev.write(PHONE_WR, resp, 100)
def do_mitm():
dev = find_dev()
@ -52,14 +54,15 @@ def do_mitm():
while True:
cmd = poll_ep(dev, PHONE_INT)
if cmd is not None:
print(cmd)
print("Int line ", HEX(cmd))
assert cmd[0] == ord('R')
# FIXME: restart card anyways?
# sm_con.reset_card()
print("Write atr: ", HEX(atr))
write_phone(dev, atr)
cmd = poll_ep(dev, PHONE_RD)
if cmd is not None:
print(cmd)
print("RD: ", HEX(cmd))
sim_data = sm_con.send_receive_cmd(cmd)
write_phone(dev, sim_data)

5
usb_application/util.py Normal file
View File

@ -0,0 +1,5 @@
def HEX(vals):
if vals is not None:
return ' '.join('%.2x'%x for x in vals)