blob: 8d35f1f8876b123ed95798b9d5dede8b311ceef7 [file] [log] [blame]
Christina Quast74876d22015-03-13 23:45:30 +01001#!/usr/bin/env python3
2
3import usb.core
4import usb.util
5import sys
6
Christina Quast5a691a32015-03-18 18:45:14 +01007import time # needed for sleep()
8import traceback # Exception timeout
9
10# Sniffed Phone to SIM card communication:
11# phone < sim : ATR
12# phone > sim : A0 A4 00 00 02 (Select File)
13# phone < sim : A4 (INS repeated)
14# phone > sim : 7F 02 (= ??)
15# phone < sim : 9F 16 (9F: success, can deliver 0x16 (=22) byte)
16# phone > sim : ?? (A0 C0 00 00 16)
17# phone < sim : C0 (INS repeated)
18# phone < sim : 00 00 00 00 7F 20 02 00 00 00 00 00 09 91 00 17 04 00 83 8A (data of length 22)
19# phone < sim : 90 00 (OK, everything went fine)
20# phone ? sim : 00 (??)
21
Christina Quast74876d22015-03-13 23:45:30 +010022# SuperSIM ATR
Christina Quast4bcc0232015-03-24 21:59:32 +010023# atr= [0x3B, 0x9A, 0x94, 0x00, 0x92, 0x02, 0x75, 0x93, 0x11, 0x00, 0x01, 0x02, 0x02, 0x19]
24
25# Faster sysmocom SIM
26#atr = [0x3B, 0x99, 0x18, 0x00, 0x11, 0x88, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x60]
27atr = [0x3B, 0x99, 0x11, 0x00, 0x11, 0x88, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x60]
28
Christina Quast69734e92015-03-15 16:09:55 +010029RESP_OK = [0x60, 0x00]
Christina Quast74876d22015-03-13 23:45:30 +010030
31def find_dev():
32 dev = usb.core.find(idVendor=0x03eb, idProduct=0x6004)
33 if dev is None:
34 raise ValueError("Device not found")
35 else:
36 print("Found device")
37 return dev
38
Christina Quast4e4682c2015-03-18 18:48:57 +010039WAIT_RST = 0
40WAIT_CMD = 1
41
42def handle_wait_rst(dev):
Christina Quast69734e92015-03-15 16:09:55 +010043 # ATR handling
Christina Quast4bcc0232015-03-24 21:59:32 +010044 print("Handle ATR")
Christina Quast1dd996a2015-04-03 11:42:29 +020045 arr = dev.read(PHONE_INT, 64, 300) # Notification endpoint
Christina Quast5134fb62015-03-22 19:09:02 +010046# print("arr: ", arr)
Christina Quast4e4682c2015-03-18 18:48:57 +010047 c=arr.pop()
Christina Quast5134fb62015-03-22 19:09:02 +010048# print(c)
Christina Quast74876d22015-03-13 23:45:30 +010049
Christina Quast4e4682c2015-03-18 18:48:57 +010050 if c == ord('R'):
51 # We received a Reset, so we send ATR
Christina Quast1dd996a2015-04-03 11:42:29 +020052 written = dev.write(PHONE_DATAOUT, atr, 1000)
Christina Quast4bcc0232015-03-24 21:59:32 +010053 print("Written ATR of size: ")
54 print(written)
Christina Quast4e4682c2015-03-18 18:48:57 +010055 state = WAIT_CMD;
56 return state
Christina Quast74876d22015-03-13 23:45:30 +010057
Christina Quast4e4682c2015-03-18 18:48:57 +010058def handle_wait_cmd(dev):
Christina Quast69734e92015-03-15 16:09:55 +010059 # Read phone request
Christina Quast4e4682c2015-03-18 18:48:57 +010060 print("Wait cmd")
Christina Quast1dd996a2015-04-03 11:42:29 +020061 cmd = dev.read(PHONE_DATAIN, 64, 1000)
Christina Quast4e4682c2015-03-18 18:48:57 +010062 print("Received request!: ")
63 print("".join("%02x " % b for b in cmd))
Christina Quast69734e92015-03-15 16:09:55 +010064
Christina Quast4bcc0232015-03-24 21:59:32 +010065 return send_response(dev, cmd);
Christina Quast69734e92015-03-15 16:09:55 +010066
Christina Quast4e4682c2015-03-18 18:48:57 +010067handle_msg_funcs = { WAIT_RST: handle_wait_rst,
68 WAIT_CMD: handle_wait_cmd }
69
70def handle_phone_request(dev, state):
Christina Quast4bcc0232015-03-24 21:59:32 +010071 if state == WAIT_CMD:
72 try:
73 state = handle_msg_funcs[WAIT_RST](dev)
74 except usb.USBError as e:
Christina Quastb65b8812015-04-04 10:51:37 +020075 print(e)
Christina Quast4e4682c2015-03-18 18:48:57 +010076 state = handle_msg_funcs[state](dev)
77 return state
78
79INS = 1
Christina Quast1dd996a2015-04-03 11:42:29 +020080CNT = 4
81
Christina Quastb65b8812015-04-04 10:51:37 +020082PHONE_DATAOUT = 0x04
83PHONE_DATAIN = 0x85
84PHONE_INT = 0x86
Christina Quast4e4682c2015-03-18 18:48:57 +010085
86def send_response(dev, cmd):
Christina Quast4e4682c2015-03-18 18:48:57 +010087# FIXME: We could get data of length 5 as well! Implement another distinct criteria!
Christina Quast4bcc0232015-03-24 21:59:32 +010088 state = WAIT_CMD
Christina Quast4e4682c2015-03-18 18:48:57 +010089 if len(cmd) == 5: # Received cmd from phone
90 if cmd[INS] == 0xA4:
91 resp = [cmd[INS]] # Respond with INS byte
Christina Quast4e4682c2015-03-18 18:48:57 +010092 elif cmd[INS] == 0xC0:
Christina Quaste5342b32015-03-19 19:29:49 +010093 data = [0x00, 0x00, 0x00, 0x00,
94 0x7F, 0x20, 0x02, 0x00,
95 0x00, 0x00, 0x00, 0x00,
96 0x09, 0x91, 0x00, 0x17,
97 0x04, 0x00, 0x83, 0x8A,
98 0x83, 0x8A]
Christina Quast4e4682c2015-03-18 18:48:57 +010099 SW = [0x90, 0x00]
Christina Quast5134fb62015-03-22 19:09:02 +0100100 resp = [cmd[INS]] + data + SW # Respond with INS byte
Christina Quast97922ba2015-04-03 11:46:34 +0200101 #state = WAIT_RST
Christina Quaste5342b32015-03-19 19:29:49 +0100102 else:
103 print("Unknown cmd")
104 resp = [0x60, 0x00]
105 elif len(cmd) == 2:
106 resp = [0x9F, 0x16]
Christina Quast4e4682c2015-03-18 18:48:57 +0100107 else:
Christina Quaste5342b32015-03-19 19:29:49 +0100108 resp = [0x60, 0x00]
Christina Quast4e4682c2015-03-18 18:48:57 +0100109
Christina Quast1dd996a2015-04-03 11:42:29 +0200110 written = dev.write(PHONE_DATAOUT, resp, 10000);
Christina Quaste5342b32015-03-19 19:29:49 +0100111 if written > 0:
112 print("Bytes written:")
113 print(written)
Christina Quast69734e92015-03-15 16:09:55 +0100114
Christina Quast5134fb62015-03-22 19:09:02 +0100115 print("Cmd, resp: ")
116 print("".join("%02x " % b for b in cmd))
117 print("".join("%02x " % b for b in resp))
Christina Quast4bcc0232015-03-24 21:59:32 +0100118
119 return state
Christina Quast5134fb62015-03-22 19:09:02 +0100120
Christina Quast69734e92015-03-15 16:09:55 +0100121def emulate_sim():
122 dev = find_dev()
Christina Quast4e4682c2015-03-18 18:48:57 +0100123 state = WAIT_RST;
Christina Quast69734e92015-03-15 16:09:55 +0100124
125 while True:
126 try:
Christina Quast4e4682c2015-03-18 18:48:57 +0100127 state = handle_phone_request(dev, state)
Christina Quast69734e92015-03-15 16:09:55 +0100128
Christina Quast4e4682c2015-03-18 18:48:57 +0100129 except usb.USBError as e:
Christina Quast5134fb62015-03-22 19:09:02 +0100130 # print e
131 pass