2005-09-29 03:39:27 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: iso-8859-1 -*-
|
|
|
|
|
2005-09-29 21:37:28 +00:00
|
|
|
import pycsc, utils, cards, os, re, binascii, sys, exceptions, traceback
|
2005-10-09 02:06:32 +00:00
|
|
|
from shell import Shell
|
2005-09-30 04:13:34 +00:00
|
|
|
print_backtrace = True
|
2005-09-29 03:39:27 +00:00
|
|
|
|
2005-09-29 15:00:26 +00:00
|
|
|
def cmd_atr(card, *args):
|
2005-10-09 02:06:32 +00:00
|
|
|
"""Print the ATR of the currently inserted card."""
|
2005-09-29 15:00:26 +00:00
|
|
|
print "ATR: %s" % utils.hexdump(card.card.status()['ATR'], short=True)
|
2005-09-29 03:39:27 +00:00
|
|
|
|
2005-09-29 14:41:20 +00:00
|
|
|
COMMANDS = {
|
2005-10-09 02:06:32 +00:00
|
|
|
"atr": cmd_atr
|
2005-09-29 14:41:20 +00:00
|
|
|
}
|
2005-09-29 03:39:27 +00:00
|
|
|
|
2005-09-29 14:41:20 +00:00
|
|
|
if __name__ == "__main__":
|
2005-09-29 03:39:27 +00:00
|
|
|
|
2005-09-29 14:41:20 +00:00
|
|
|
readerName = pycsc.listReader()[0]
|
|
|
|
newState = pycsc.getStatusChange(ReaderStates=[{'Reader': readerName, 'CurrentState':pycsc.SCARD_STATE_UNAWARE}])
|
2005-09-29 03:39:27 +00:00
|
|
|
|
2005-09-29 14:41:20 +00:00
|
|
|
print "Cyberflex shell"
|
|
|
|
print "Using reader: %s" % readerName
|
|
|
|
print "Card present: %s" % ((newState[0]['EventState'] & pycsc.SCARD_STATE_PRESENT) and "yes" or "no")
|
|
|
|
|
|
|
|
if not newState[0]['EventState'] & pycsc.SCARD_STATE_PRESENT:
|
|
|
|
print "Please insert card ..."
|
2005-09-29 03:39:27 +00:00
|
|
|
|
2005-09-29 14:41:20 +00:00
|
|
|
while not newState[0]['EventState'] & pycsc.SCARD_STATE_PRESENT \
|
|
|
|
or newState[0]['EventState'] & pycsc.SCARD_STATE_MUTE:
|
|
|
|
|
|
|
|
newState = pycsc.getStatusChange(ReaderStates=[{'Reader': readerName, 'CurrentState':newState[0]['EventState']}])
|
|
|
|
|
|
|
|
if newState[0]['EventState'] & pycsc.SCARD_STATE_MUTE:
|
|
|
|
print "Card is mute, please retry ..."
|
|
|
|
|
|
|
|
print "Card present: %s" % ((newState[0]['EventState'] & pycsc.SCARD_STATE_PRESENT) and "yes" or "no")
|
|
|
|
|
|
|
|
print "ATR: %s" % utils.hexdump(newState[0]['Atr'], short = True)
|
|
|
|
|
2005-10-12 02:09:15 +00:00
|
|
|
pycsc_card = pycsc.pycsc(protocol = pycsc.SCARD_PROTOCOL_ANY)
|
|
|
|
card = cards.new_card_object(pycsc_card)
|
|
|
|
|
2005-10-09 02:06:32 +00:00
|
|
|
shell = Shell("cyberflex-shell")
|
|
|
|
shell.register_commands(card, COMMANDS)
|
|
|
|
shell.register_commands(card)
|
2005-09-29 14:41:20 +00:00
|
|
|
|
2005-10-09 02:06:32 +00:00
|
|
|
def _update_prompt():
|
|
|
|
shell.set_prompt(card.get_prompt() + " ")
|
|
|
|
shell.register_pre_hook(_update_prompt)
|
2005-09-29 14:41:20 +00:00
|
|
|
|
2005-10-09 02:06:32 +00:00
|
|
|
def _clear_sw():
|
2005-09-29 17:16:27 +00:00
|
|
|
card.sw_changed = False
|
2005-10-09 02:06:32 +00:00
|
|
|
shell.register_pre_hook(_clear_sw)
|
|
|
|
|
|
|
|
apduregex = re.compile(r'^\s*([0-9a-f]{2}\s*){4,}$', re.I)
|
|
|
|
def do_raw_apdu(*args):
|
|
|
|
apdu_string = "".join(args)
|
|
|
|
if not apduregex.match(apdu_string):
|
|
|
|
raise NotImplementedError
|
2005-09-29 17:16:27 +00:00
|
|
|
|
2005-10-09 02:06:32 +00:00
|
|
|
apdu_binary = binascii.a2b_hex("".join(apdu_string.split()))
|
|
|
|
apdu = utils.APDU(apdu_binary)
|
|
|
|
response = card.send_apdu(apdu)
|
|
|
|
print utils.hexdump(response)
|
2005-09-29 17:16:27 +00:00
|
|
|
|
2005-10-09 02:06:32 +00:00
|
|
|
shell.fallback = do_raw_apdu
|
|
|
|
|
|
|
|
def _print_sw():
|
2005-09-29 17:16:27 +00:00
|
|
|
if card.sw_changed:
|
|
|
|
print card.decode_statusword()
|
2005-10-09 02:06:32 +00:00
|
|
|
shell.register_post_hook(_print_sw)
|
|
|
|
|
|
|
|
shell.run()
|