mirror of
https://gitea.osmocom.org/sim-card/simtrace2.git
synced 2026-03-23 16:58:33 +03:00
simcard communication python class
This commit is contained in:
@@ -3,101 +3,81 @@
|
|||||||
from smartcard.scard import *
|
from smartcard.scard import *
|
||||||
import smartcard.util
|
import smartcard.util
|
||||||
|
|
||||||
CMD_SEL_ROOT = [0xA0, 0xA4, 0x00, 0x00, 0x02, 0x3F, 0x00]
|
import array
|
||||||
CMD_SEL_FILE = [0xA0, 0xA4, 0x00, 0x00, 0x02, 0x7F, 0x20]
|
|
||||||
CMD_GET_DATA = [0xA0, 0xC0, 0x00, 0x00, 0x16]
|
|
||||||
# SuperSIM ATR
|
|
||||||
atr_supersim= [0x3B, 0x9A, 0x94, 0x00, 0x92, 0x02, 0x75, 0x93, 0x11, 0x00, 0x01, 0x02, 0x02, 0x19]
|
|
||||||
|
|
||||||
# Faster sysmocom SIM
|
class SmartcardException(Exception):
|
||||||
ATR_SYSMOCOM1 = [0x3B, 0x99, 0x18, 0x00, 0x11, 0x88, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x60]
|
pass
|
||||||
ATR_SYSMOCOM2 = [0x3B, 0x99, 0x11, 0x00, 0x11, 0x88, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x60]
|
|
||||||
NEW_ATR = ATR_SYSMOCOM2
|
|
||||||
|
|
||||||
def pattern_match(inpt):
|
class SmartcardConnection:
|
||||||
if (inpt == ATR_SYSMOCOM1):
|
# hcard, dwActiveProtocol, hcontext, reader
|
||||||
return NEW_ATR
|
|
||||||
elif (inpt == CMD_SEL_FILE):
|
|
||||||
return CMD_SEL_ROOT
|
|
||||||
else:
|
|
||||||
return inpt
|
|
||||||
|
|
||||||
def connect_to_card(hcontext, reader):
|
def __init__(self):
|
||||||
hresult, hcard, dwActiveProtocol = SCardConnect(hcontext, reader,
|
self.establish_context()
|
||||||
SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1)
|
self.connect_card()
|
||||||
if hresult != SCARD_S_SUCCESS:
|
|
||||||
raise Exception('Unable to connect: ' +
|
|
||||||
SCardGetErrorMessage(hresult))
|
|
||||||
print 'Connected with active protocol', dwActiveProtocol
|
|
||||||
return hresult, hcard, dwActiveProtocol
|
|
||||||
|
|
||||||
def establish_context():
|
def connect_card(self):
|
||||||
hresult, hcontext = SCardEstablishContext(SCARD_SCOPE_USER)
|
hresult, self.hcard, self.dwActiveProtocol = SCardConnect(self.hcontext, self.reader,
|
||||||
if hresult != SCARD_S_SUCCESS:
|
SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1)
|
||||||
raise Exception('Failed to establish context : ' +
|
if hresult != SCARD_S_SUCCESS:
|
||||||
SCardGetErrorMessage(hresult))
|
raise SmartcardException('Unable to connect: ' +
|
||||||
print 'Context established!'
|
|
||||||
|
|
||||||
hresult, readers = SCardListReaders(hcontext, [])
|
|
||||||
if hresult != SCARD_S_SUCCESS:
|
|
||||||
raise Exception('Failed to list readers: ' +
|
|
||||||
SCardGetErrorMessage(hresult))
|
|
||||||
print 'PCSC Readers:', readers
|
|
||||||
|
|
||||||
if len(readers) < 1:
|
|
||||||
raise Exception('No smart card readers')
|
|
||||||
|
|
||||||
reader = readers[0]
|
|
||||||
print "Using reader:", reader
|
|
||||||
|
|
||||||
return (hcontext, reader)
|
|
||||||
|
|
||||||
|
|
||||||
def release_context(hcontext):
|
|
||||||
hresult = SCardReleaseContext(hcontext)
|
|
||||||
if hresult != SCARD_S_SUCCESS:
|
|
||||||
raise Exception('Failed to release context: ' +
|
|
||||||
SCardGetErrorMessage(hresult))
|
SCardGetErrorMessage(hresult))
|
||||||
print 'Released context.'
|
print 'Connected with active protocol', self.dwActiveProtocol
|
||||||
|
|
||||||
def send_receive_cmd(cmd, hcard, dwActiveProtocol):
|
def establish_context(self):
|
||||||
print("Response: ")
|
hresult, self.hcontext = SCardEstablishContext(SCARD_SCOPE_USER)
|
||||||
new_cmd = pattern_match(cmd)
|
if hresult != SCARD_S_SUCCESS:
|
||||||
print(' '.join([hex(i) for i in cmd]))
|
raise SmartcardException('Failed to establish context : ' +
|
||||||
hresult, response = SCardTransmit(hcard, dwActiveProtocol,
|
SCardGetErrorMessage(hresult))
|
||||||
new_cmd)
|
print 'Context established!'
|
||||||
if hresult != SCARD_S_SUCCESS:
|
|
||||||
raise Exception('Failed to transmit: ' +
|
|
||||||
SCardGetErrorMessage(hresult))
|
|
||||||
resp = pattern_match(response)
|
|
||||||
print 'Ans: ' + smartcard.util.toHexString(resp,
|
|
||||||
smartcard.util.HEX)
|
|
||||||
return response
|
|
||||||
|
|
||||||
def disconnect_card(hcard):
|
hresult, readers = SCardListReaders(self.hcontext, [])
|
||||||
hresult = SCardDisconnect(hcard, SCARD_UNPOWER_CARD)
|
if hresult != SCARD_S_SUCCESS:
|
||||||
if hresult != SCARD_S_SUCCESS:
|
raise SmartcardException('Failed to list readers: ' +
|
||||||
raise Exception('Failed to disconnect: ' +
|
SCardGetErrorMessage(hresult))
|
||||||
SCardGetErrorMessage(hresult))
|
print 'PCSC Readers:', readers
|
||||||
print 'Disconnected'
|
|
||||||
|
if len(readers) < 1:
|
||||||
|
raise SmartcardException('No smart card readers')
|
||||||
|
|
||||||
|
self.reader = readers[0]
|
||||||
|
print "Using reader:", self.reader
|
||||||
|
|
||||||
|
def release_context(self):
|
||||||
|
hresult = SCardReleaseContext(self.hcontext)
|
||||||
|
if hresult != SCARD_S_SUCCESS:
|
||||||
|
raise SmartcardException('Failed to release context: ' +
|
||||||
|
SCardGetErrorMessage(hresult))
|
||||||
|
print 'Released context.'
|
||||||
|
|
||||||
|
def send_receive_cmd(self, cmd):
|
||||||
|
print("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)
|
||||||
|
return array.array('B', resp)
|
||||||
|
|
||||||
|
def disconnect_card(self):
|
||||||
|
hresult = SCardDisconnect(self.hcard, SCARD_UNPOWER_CARD)
|
||||||
|
if hresult != SCARD_S_SUCCESS:
|
||||||
|
raise SmartcardException('Failed to disconnect: ' +
|
||||||
|
SCardGetErrorMessage(hresult))
|
||||||
|
print 'Disconnected'
|
||||||
|
|
||||||
|
|
||||||
def do_intercept(cmd, dwActiveProtocol):
|
def close(self):
|
||||||
return send_receive_cmd(cmd, hcard, dwActiveProtocol)
|
self.disconnect_card()
|
||||||
|
self.release_context()
|
||||||
def ccid_raw_init():
|
|
||||||
(hcontext, reader) = establish_context()
|
|
||||||
hresult, hcard, dwActiveProtocol = connect_to_card(hcontext, reader)
|
|
||||||
return hcard, hcontext, dwActiveProtocol
|
|
||||||
|
|
||||||
def ccid_raw_exit(hcard, hcontext):
|
|
||||||
disconnect_card(hcard)
|
|
||||||
release_context(hcontext)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
hcard, hcontext, dwActiveProtocol = ccid_raw_init()
|
import constants
|
||||||
do_intercept(CMD_SEL_ROOT, dwActiveProtocol)
|
|
||||||
do_intercept(CMD_SEL_FILE, dwActiveProtocol)
|
sm_con = SmartcardConnection()
|
||||||
do_intercept(CMD_GET_DATA, dwActiveProtocol)
|
print(sm_con.send_receive_cmd(constants.CMD_SEL_ROOT))
|
||||||
ccid_raw_exit()
|
print(sm_con.send_receive_cmd(constants.CMD_SEL_FILE))
|
||||||
|
print(sm_con.send_receive_cmd(constants.CMD_GET_DATA))
|
||||||
|
sm_con.close()
|
||||||
|
|||||||
Reference in New Issue
Block a user