blob: c121915379670a1024248c42ba3baaff698626b9 [file] [log] [blame]
Christina Quast69d1f902015-04-03 11:41:23 +02001import usb.core
2import usb.util
3
Christina Quast88c7fa12015-04-06 00:35:03 +02004from ccid_raw import SmartcardConnection
Christina Quast95270b12015-04-04 19:59:03 +02005import phone
6
Christina Quast88c7fa12015-04-06 00:35:03 +02007from contextlib import closing
8
9
Christina Quast69d1f902015-04-03 11:41:23 +020010def find_dev():
11 dev = usb.core.find(idVendor=0x03eb, idProduct=0x6004)
12 if dev is None:
13 raise ValueError("Device not found")
14 else:
15 print("Found device")
16 return dev
17
Christina Quast88c7fa12015-04-06 00:35:03 +020018def pattern_match(inpt):
19 print("Matching inpt", inpt)
20 if (inpt == ATR_SYSMOCOM1):
21 return NEW_ATR
22 elif (inpt == CMD_SEL_FILE):
23 return CMD_SEL_ROOT
24 else:
25 return inpt
Christina Quast69d1f902015-04-03 11:41:23 +020026
27SIM_WR = 0x1
28SIM_RD = 0x82
29SIM_INT = 0x83
30
31PHONE_WR = 0x4
32PHONE_RD = 0x85
33PHONE_INT = 0x86
34
Christina Quast88c7fa12015-04-06 00:35:03 +020035ERR_TIMEOUT = 110
Christina Quast69d1f902015-04-03 11:41:23 +020036
Christina Quast88c7fa12015-04-06 00:35:03 +020037def poll_ep(dev, ep):
38 try:
39 return dev.read(ep, 64, 1000)
40 except usb.core.USBError as e:
41 if e.errno != ERR_TIMEOUT:
42 raise
43 return None
Christina Quast69d1f902015-04-03 11:41:23 +020044
Christina Quast88c7fa12015-04-06 00:35:03 +020045def write_phone(dev, resp):
46 dev.write(PHONE_WR, resp, 1000)
47
48def reset_sim(sm_con):
49 sm_con.disconnect_card()
50 sm_con.connect_card()
Christina Quast69d1f902015-04-03 11:41:23 +020051
Christina Quast95270b12015-04-04 19:59:03 +020052def do_mitm():
Christina Quast69d1f902015-04-03 11:41:23 +020053 dev = find_dev()
Christina Quast88c7fa12015-04-06 00:35:03 +020054 with closing(SmartcardConnection()) as sm_con:
Christina Quast69d1f902015-04-03 11:41:23 +020055
Christina Quast88c7fa12015-04-06 00:35:03 +020056 while True:
57 cmd = poll_ep(dev, PHONE_INT)
58 if cmd is not None:
59 print(cmd)
60 assert cmd[0] == ord('R')
61 reset_sim(sm_con)
Christina Quast69d1f902015-04-03 11:41:23 +020062
Christina Quast88c7fa12015-04-06 00:35:03 +020063 cmd = poll_ep(dev, PHONE_RD)
64 if cmd is not None:
65 print(cmd)
66 sim_data = sm_con.send_receive_cmd(cmd)
67 if sim_data is None:
68 continue
69 write_phone(dev, sim_data)