def transmit(self, bytes): self.vm.resetlog() self.current_channel = bytes[0] & 0x3 if self.selected[self.current_channel]: self.selected[self.current_channel]._selectingApplet = False if not bool(bytes[0] & 0x80): # ISO command if bytes[1:4] == [-92, 4, 0]: aid = bytes[5:5 + bytes[4]] # select command A4 04 00 if not self._cmselect(aid): return d2a('\x69\x99') elif bytes[1:4] == [112, 0, 0]: # open channel : 70 00 00 for idx in xrange(4): if not self.channels[idx]: self.channels[idx] = True buf = [idx] buf.extend(d2a('\x90\x00')) return buf return d2a('\x6A\x86') elif bytes[1:3] == [112, -128]: # close channel: 70 80 idx = bytes[3] if self.channels[idx]: self.channels[idx] = False return d2a('\x90\x00') return d2a('\x6A\x86') elif bytes[1:4] == [-26, 12, 0]: # install : E6 0C 00 self.install(bytes, 5) applet = self.selected[self.current_channel] if applet is None: # no applet selected on current channel return d2a('\x6A\x82') # Make an APDU object apdu = APDU(bytes) # pass to the process method self.vm.frame.push(applet) self.vm.frame.push(apdu) # invoke the process method self.vm._invokevirtualjava( JavaCardVirtualMethod( applet._ref.offset, 7, # process False, self.vm.cap_file, self.vm.resolver)) try: while self.vm.step(): pass except ISOException, isoe: sw = isoe.getReason() return [signed1((sw & 0xff00) >> 8), signed1(sw & 0x00ff)]
def transmit(self, bytes): self.vm.resetlog() self.current_channel = bytes[0] & 0x3 if self.selected[self.current_channel]: self.selected[self.current_channel]._selectingApplet = False if not bool(bytes[0] & 0x80): # ISO command if bytes[1:4] == [-92, 4, 0]: aid = bytes[5:5 + bytes[4]] # select command A4 04 00 if not self._cmselect(aid): return d2a('\x69\x99') elif bytes[1:4] == [112, 0, 0]: # open channel : 70 00 00 for idx in xrange(4): if not self.channels[idx]: self.channels[idx] = True buf = [idx] buf.extend(d2a('\x90\x00')) return buf return d2a('\x6A\x86') elif bytes[1:3] == [112, -128]: # close channel: 70 80 idx = bytes[3] if self.channels[idx]: self.channels[idx] = False return d2a('\x90\x00') return d2a('\x6A\x86') elif bytes[1:4] == [-26, 12, 0]: # install : E6 0C 00 self.install(bytes, 5) applet = self.selected[self.current_channel] if applet is None: # no applet selected on current channel return d2a('\x6A\x82') # Make an APDU object apdu = APDU(bytes) # pass to the process method self.vm.frame.push(applet) self.vm.frame.push(apdu) # invoke the process method self.vm._invokevirtualjava(JavaCardVirtualMethod( applet._ref.offset, 7, # process False, self.vm.cap_file, self.vm.resolver)) try: while self.vm.step(): pass except ISOException, isoe: sw = isoe.getReason() return [signed1((sw & 0xff00) >> 8), signed1(sw & 0x00ff)]
class Token(object): """ This token is a Java Token. The Applet is actually a capfile. The code below is taken from runcap.py """ def __init__(self, jcversion=(3, 0, 1)): self.current_applet_aid = None # a2d(aid) => Applet self.applets = {} # channel => Applet self.selected = [None, None, None, None] # opened self.channels = [True, False, False, False] # current one self.current_channel = 0 # Create the VM self.vm = JavaCardVM(resolver.linkResolver(jcversion)) self.installJCFunctions() def echo(self, *args, **kwargs): # to be redefined in the subclassing class pass def installJCFunctions(self): """ This tweak the JC Framework to make it fit our environment We make a big usage of closures to pass the local `self` to the JC functions """ def defineMyregister(self): def myregister(applet, bArray=[], bOffset=0, bLength=0): if bLength != 0: aid = bArray[bOffset:bOffset + bLength] # also update the current one self.current_applet_aid = aid else: aid = self.current_applet_aid self.applets[a2d(aid)] = applet self.echo("registered %s as %s" % (a2s(aid), applet)) return myregister Applet.register = defineMyregister(self) def defineMygetAID(self): def mygetAID(): return AID(self.current_applet_aid, 0, len(self.current_applet_aid)) return mygetAID JCSystem.getAID = defineMygetAID(self) Applet.getAID = JCSystem.getAID def defineMylookupAID(self): def mylookupAID(buffer, offset, length): if a2d(buffer[offset:offset + length]) in self.applets: return AID(buffer, offset, length) return None return mylookupAID JCSystem.lookupAID = defineMylookupAID(self) def defineMyisAppletActive(self): def myisAppletActive(aid): return self.applets[a2d(aid.aid)] in self.selected return myisAppletActive JCSystem.isAppletActive = defineMyisAppletActive(self) def defineMygetAssignedChannel(self): def mygetAssignedChannel(): return self.current_channel return mygetAssignedChannel JCSystem.getAssignedChannel = defineMygetAssignedChannel(self) def transmit(self, bytes): self.vm.resetlog() self.current_channel = bytes[0] & 0x3 if self.selected[self.current_channel]: self.selected[self.current_channel]._selectingApplet = False if not bool(bytes[0] & 0x80): # ISO command if bytes[1:4] == [-92, 4, 0]: aid = bytes[5:5 + bytes[4]] # select command A4 04 00 if not self._cmselect(aid): return d2a('\x69\x99') elif bytes[1:4] == [112, 0, 0]: # open channel : 70 00 00 for idx in xrange(4): if not self.channels[idx]: self.channels[idx] = True buf = [idx] buf.extend(d2a('\x90\x00')) return buf return d2a('\x6A\x86') elif bytes[1:3] == [112, -128]: # close channel: 70 80 idx = bytes[3] if self.channels[idx]: self.channels[idx] = False return d2a('\x90\x00') return d2a('\x6A\x86') elif bytes[1:4] == [-26, 12, 0]: # install : E6 0C 00 self.install(bytes, 5) applet = self.selected[self.current_channel] if applet is None: # no applet selected on current channel return d2a('\x6A\x82') # Make an APDU object apdu = APDU(bytes) # pass to the process method self.vm.frame.push(applet) self.vm.frame.push(apdu) # invoke the process method self.vm._invokevirtualjava( JavaCardVirtualMethod( applet._ref.offset, 7, # process False, self.vm.cap_file, self.vm.resolver)) try: while self.vm.step(): pass except ISOException, isoe: sw = isoe.getReason() return [signed1((sw & 0xff00) >> 8), signed1(sw & 0x00ff)] except RuntimeException: self.echo("Caught RuntimeException") return d2a('\x6f\x00')
self.vm.cap_file, self.vm.resolver)) try: while self.vm.step(): pass except ISOException, isoe: sw = isoe.getReason() return [signed1((sw & 0xff00) >> 8), signed1(sw & 0x00ff)] except RuntimeException: self.echo("Caught RuntimeException") return d2a('\x6f\x00') except: self.echo("Real bad exception") raise buf = apdu._APDU__buffer[:apdu._outgoinglength] buf.extend(d2a('\x90\x00')) return buf def _cmdeselect(self): channel = self.current_channel applet = self.selected[channel] self.vm.frame.push(applet) try: deselectmtd = JavaCardVirtualMethod(applet._ref.offset, 4, False, self.vm.cap_file, self.vm.resolver) except NoSuchMethod: self.selected[channel] = None return True self.vm._invokevirtualjava(deselectmtd) while self.vm.step():
self.vm.cap_file, self.vm.resolver)) try: while self.vm.step(): pass except ISOException, isoe: sw = isoe.getReason() return [signed1((sw & 0xff00) >> 8), signed1(sw & 0x00ff)] except RuntimeException: self.echo("Caught RuntimeException") return d2a('\x6f\x00') except: self.echo("Real bad exception") raise buf = apdu._APDU__buffer[:apdu._outgoinglength] buf.extend(d2a('\x90\x00')) return buf def _cmdeselect(self): channel = self.current_channel applet = self.selected[channel] self.vm.frame.push(applet) try: deselectmtd = JavaCardVirtualMethod( applet._ref.offset, 4, False, self.vm.cap_file, self.vm.resolver) except NoSuchMethod: self.selected[channel] = None