mirror of
https://gitea.osmocom.org/sim-card/simtrace2.git
synced 2026-03-20 07:18:33 +03:00
apdu_split without C code remains
This commit is contained in:
78
usb_application/apdu_split.py
Normal file → Executable file
78
usb_application/apdu_split.py
Normal file → Executable file
@@ -1,3 +1,5 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
# Code ported from simtrace host program apdu_split.c
|
# Code ported from simtrace host program apdu_split.c
|
||||||
#
|
#
|
||||||
# (C) 2010 by Harald Welte <hwelte@hmw-consulting.de>
|
# (C) 2010 by Harald Welte <hwelte@hmw-consulting.de>
|
||||||
@@ -13,36 +15,24 @@
|
|||||||
|
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
|
|
||||||
def Apdu_States(Enum):
|
class Apdu_splitter:
|
||||||
APDU_S_CLA = 1
|
|
||||||
APDU_S_INS = 2
|
|
||||||
APDU_S_P1 = 3
|
|
||||||
APDU_S_P2 = 4
|
|
||||||
APDU_S_P3 = 5
|
|
||||||
APDU_S_DATA = 6
|
|
||||||
APDU_S_DATA_SINGLE = 7
|
|
||||||
APDU_S_SW1 = 8
|
|
||||||
APDU_S_SW2 = 9
|
|
||||||
APDU_S_FIN = 10
|
|
||||||
|
|
||||||
|
APDU_S_CLA = 1
|
||||||
|
APDU_S_INS = 2
|
||||||
|
APDU_S_P1 = 3
|
||||||
|
APDU_S_P2 = 4
|
||||||
|
APDU_S_P3 = 5
|
||||||
|
APDU_S_DATA = 6
|
||||||
|
APDU_S_DATA_SINGLE = 7
|
||||||
|
APDU_S_SW1 = 8
|
||||||
|
APDU_S_SW2 = 9
|
||||||
|
APDU_S_FIN = 10
|
||||||
|
|
||||||
Apdu_S = {
|
|
||||||
APDU_S_CLA : func_APDU_S_CLA_P1_P2,
|
|
||||||
APDU_S_INS : func_APDU_S_INS,
|
|
||||||
APDU_S_P1 : func_APDU_S_CLA_P1_P2,
|
|
||||||
APDU_S_P2 : func_APDU_S_CLA_P1_P2,
|
|
||||||
APDU_S_P3 : func_APDU_S_P3,
|
|
||||||
APDU_S_DATA : func_APDU_S_DATA,
|
|
||||||
APDU_S_DATA_SINGLE : func_APDU_S_DATA_SINGLE,
|
|
||||||
APDU_S_SW1 : func_APDU_S_SW1,
|
|
||||||
APDU_S_SW2 : func_APDU_S_SW2 }
|
|
||||||
|
|
||||||
class apdu_split:
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
pass
|
self.state = APDU_S_CLA
|
||||||
# FIXME: init
|
self.buf = []
|
||||||
|
|
||||||
def apdu_split_inbyte(self, c):
|
def split(self, c):
|
||||||
Apdu_S[state](c)
|
Apdu_S[state](c)
|
||||||
|
|
||||||
def func_APDU_S_INS(self, c):
|
def func_APDU_S_INS(self, c):
|
||||||
@@ -52,9 +42,9 @@ class apdu_split:
|
|||||||
self.buf.append(c)
|
self.buf.append(c)
|
||||||
self.state += 1
|
self.state += 1
|
||||||
|
|
||||||
def func_func_APDU_S_P3(self, c):
|
def func_APDU_S_P3(self, c):
|
||||||
self.buf.append(c)
|
self.buf.append(c)
|
||||||
data_remaining = 256 if c == 0 else c
|
self.data_remaining = 256 if c == 0 else c
|
||||||
self.state = func_APDU_S_SW1
|
self.state = func_APDU_S_SW1
|
||||||
|
|
||||||
def func_APDU_S_DATA(self, c):
|
def func_APDU_S_DATA(self, c):
|
||||||
@@ -69,23 +59,45 @@ class apdu_split:
|
|||||||
self.state = APDU_S_SW1
|
self.state = APDU_S_SW1
|
||||||
|
|
||||||
def func_APDU_S_SW1(self, c):
|
def func_APDU_S_SW1(self, c):
|
||||||
if (c == 0x60) {
|
if (c == 0x60):
|
||||||
print("APDU_S_SW1: NULL")
|
print("APDU_S_SW1: NULL")
|
||||||
else:
|
else:
|
||||||
# check for 'all remaining' type ACK
|
# check for 'all remaining' type ACK
|
||||||
if c == self.ins or c == self.ins + 1 or c == ~(self.ins+1):
|
if c == self.ins or c == self.ins + 1 or c == ~(self.ins+1):
|
||||||
print("ACK")
|
print("ACK")
|
||||||
self.state = APDU_S_DATA)
|
self.state = APDU_S_DATA
|
||||||
else:
|
else:
|
||||||
# check for 'only next byte' type ACK */
|
# check for 'only next byte' type ACK */
|
||||||
if c == ~(as->apdu_ins):
|
if c == ~(self.ins):
|
||||||
self.state = APDU_S_DATA_SINGLE
|
self.state = APDU_S_DATA_SINGLE
|
||||||
else:
|
else:
|
||||||
# must be SW1
|
# must be SW1
|
||||||
self.buf.append(c)
|
self.buf.append(c)
|
||||||
self.state = func_APDU_S_SW2
|
self.state = func_APDU_S_SW2
|
||||||
|
|
||||||
def func_APDU_S_SW2:
|
def func_APDU_S_SW2(self, c):
|
||||||
self.buf.append(c)
|
self.buf.append(c)
|
||||||
print("APDU:", self.buf, self.apdu_len)
|
print("APDU:", self.buf)
|
||||||
self.state = APDU_S_FIN
|
self.state = APDU_S_FIN
|
||||||
|
|
||||||
|
Apdu_S = {
|
||||||
|
APDU_S_CLA : func_APDU_S_CLA_P1_P2,
|
||||||
|
APDU_S_INS : func_APDU_S_INS,
|
||||||
|
APDU_S_P1 : func_APDU_S_CLA_P1_P2,
|
||||||
|
APDU_S_P2 : func_APDU_S_CLA_P1_P2,
|
||||||
|
APDU_S_P3 : func_APDU_S_P3,
|
||||||
|
APDU_S_DATA : func_APDU_S_DATA,
|
||||||
|
APDU_S_DATA_SINGLE : func_APDU_S_DATA_SINGLE,
|
||||||
|
APDU_S_SW1 : func_APDU_S_SW1,
|
||||||
|
APDU_S_SW2 : func_APDU_S_SW2 }
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
msg = [0xA0, 0xA4, 0x00, 0x00, 0x02]
|
||||||
|
apdus = Apdu_splitter()
|
||||||
|
|
||||||
|
for c in msg:
|
||||||
|
print(c)
|
||||||
|
apdus.split(c)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user