some more T=0 vs. T=1 fixes

git-svn-id: svn+ssh://localhost/home/henryk/svn/cyberflex-shell/trunk@48 f711b948-2313-0410-aaa9-d29f33439f0b
This commit is contained in:
hploetz 2006-05-18 02:16:15 +00:00
parent 7d3953d48d
commit 036218d632
4 changed files with 43 additions and 33 deletions

View File

@ -16,9 +16,9 @@ SECURE_CHANNEL_MACENC = 3
MAC_LENGTH = 8
class Cyberflex_Card(Java_Card):
APDU_INITIALIZE_UPDATE = APDU('\x80\x50\x00\x00\x00')
APDU_INITIALIZE_UPDATE = APDU('\x80\x50\x00\x00')
APDU_EXTERNAL_AUTHENTICATE = APDU('\x84\x82\x00\x00')
APDU_GET_STATUS = APDU('\x84\xF2\x00\x00\x02\x4f\x00')
APDU_GET_STATUS = APDU('\x84\xF2\x00\x00\x4f\x00')
APDU_DELETE = APDU('\x84\xe4\x00\x00')
DRIVER_NAME = "Cyberflex"
@ -67,6 +67,7 @@ class Cyberflex_Card(Java_Card):
Is responsible for authenticating/encrypting commands when needed."""
if apdu[0] == '\x84':
## Need security
if self.secure_channel_state == SECURE_CHANNEL_NONE:
raise Exception, "Need security but channel is not established"
if self.secure_channel_state == SECURE_CHANNEL_CLEAR:

View File

@ -96,7 +96,7 @@ class Card:
if len(result) == 2 and result[0] == '\x61':
## Need to call GetResponse
gr_apdu = APDU(self.APDU_GET_RESPONSE, le = result[1]).get_string()
gr_apdu = APDU(self.APDU_GET_RESPONSE, le = result[1]).get_string(protocol=0)
result = self._real_send(gr_apdu)
if DEBUG:

View File

@ -94,13 +94,13 @@ if __name__ == "__main__":
response = card.send_apdu(apdu)
if len(response) > 2: ## The SW is already printed by _print_sw as a post_hook
print utils.hexdump(response[:-2])
print utils.hexdump(response[:-2]), "\n"
shell.fallback = do_raw_apdu
def _print_sw():
if card.sw_changed:
print "\n", card.decode_statusword()
print card.decode_statusword()
shell.register_post_hook(_print_sw)
shell.run()

View File

@ -104,7 +104,9 @@ class APDU:
form a valid APDU!
The keyword arguments can then be used to override those values.
Keywords recognized are: cla, ins, p1, p2, lc, le, content."""
Keywords recognized are: cla, ins, p1, p2, lc, le, content.
If protocol is set to 0 then the APDU will be parsed as a T=0 APDU."""
initbuff = list()
@ -142,30 +144,32 @@ class APDU:
lc_was_set = False
## if len(initbuff) == 4: ## ISO case 1
## self.le = 0
## self.lc = 0
## self.content = list()
## elif len(initbuff) == 5: ## ISO case 2
## self.le = initbuff[self.OFFSET_LE]
## self.lc = 0
## self.content = list()
## elif len(initbuff) > 5:
## self.lc = initbuff[self.OFFSET_LC]
## lc_was_set = True
## if len(initbuff) == 5 + self.lc: ## ISO case 3
## self.le = 0
## self.content = initbuff[5:5+self.lc]
## elif len(initbuff) == 5 + self.lc + 1: ## ISO case 4
## self.le = initbuff[-1]
## self.content = initbuff[5:5+self.lc]
## else:
## raise ValueError, "Invalid APDU, length(%i) != 4 + 1 + lc(%i) + 1" % (len(initbuff), self.lc)
## else:
## raise ValueError, "Invalid APDU, impossible"
self.le = 0
self.lc = len(initbuff)-4
self.content = initbuff[4:]
if kwargs.get("protocol", None) == 0:
if len(initbuff) == 4: ## ISO case 1
self.le = 0
self.lc = 0
self.content = list()
elif len(initbuff) == 5: ## ISO case 2
self.le = initbuff[self.OFFSET_LE]
self.lc = 0
self.content = list()
elif len(initbuff) > 5:
self.lc = initbuff[self.OFFSET_LC]
lc_was_set = True
if len(initbuff) == 5 + self.lc: ## ISO case 3
self.le = 0
self.content = initbuff[5:5+self.lc]
elif len(initbuff) == 5 + self.lc + 1: ## ISO case 4
self.le = initbuff[-1]
self.content = initbuff[5:5+self.lc]
else:
raise ValueError, "Invalid APDU, length(%i) != 4 + 1 + lc(%i) + 1" % (len(initbuff), self.lc)
else:
raise ValueError, "Invalid APDU, impossible"
else:
self.le = 0
self.lc = len(initbuff)-4
self.content = initbuff[4:]
for (kw_orig, arg) in kwargs.items():
kw = kw_orig.lower()
@ -258,8 +262,9 @@ class APDU:
else:
self.__dict__[name] = value
def get_string(self, protocol=0):
"""Return the contents of this APDU as a binary string."""
def get_string(self, protocol=None):
"""Return the contents of this APDU as a binary string.
If protocol is set to 0 then the APDU will be rendered as a T=0 APDU."""
contents = [self.cla, self.ins, self.p1, self.p2]
if protocol == 0:
if self.lc > 0:
@ -269,7 +274,11 @@ class APDU:
else:
contents.extend( self.content )
return "".join([i is not None and chr(i) or "?" for i in contents])
def append_content(self, string):
self.content = self.content + string
self.lc = len(self.content)
if __name__ == "__main__":
response = """
0000: 07 A0 00 00 00 03 00 00 07 00 07 A0 00 00 00 62 ...............b