def test_gcdIteratif(self): intr = JavaCardVM(None) intr.frames.push( JavaCardFrame( [42, 56], [29, 97, 4, 28, 120, 28, 29, 73, 49, 29, 47, 30, 48, 112, 243 ])) self._run(intr) self.assertEquals(14, intr.getRetValue())
def test_callExtStaticMethod(self): intr = JavaCardVM(linkResolver()) intr.load(javatest_cap) intr.frames.push(JavaCardFrame([None, None], [17, 106, 129, 141, 0, 5, 122], 0)) try: self._run(intr) self.fail() except ISOException as ioe: self.assertEqual(0x6A81, ioe.getReason())
def test_callExtStaticMethod(self): intr = JavaCardVM(linkResolver()) intr.load(javatest_cap) intr.frames.push(JavaCardFrame([None, None], [17, 106, 129, 141, 0, 5, 122])) try: self._run(intr) self.fail() except ISOException, ioe: self.assertEquals(0x6A81, ioe.getReason())
def test_gcdRecursif(self): intr = JavaCardVM(linkResolver()) intr.load(javatest_cap) intr.frames.push( JavaCardFrame( [42, 56], [29, 97, 4, 28, 120, 29, 28, 29, 73, 141, 0, 9, 120])) self._run(intr) self.assertEquals(14, intr.getRetValue())
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 _testBranch(self, opcode, params, stack, expectedIP): vm = JavaCardVM(None) vm.frame.stack = stack f = getattr(vm, opcode) offset = f(*params) if offset is None: offset = 0 self.assertEqual(expectedIP, offset)
def testSomeIf(self): intr = JavaCardVM(None) intr.frames.push( JavaCardFrame([None, 6], [ 29, 5, 73, 97, 6, 29, 6, 69, 120, 3, 49, 30, 29, 109, 13, 30, 8, 65, 48, 30, 4, 65, 91, 49, 112, 243, 29, 120 ])) self._run(intr)
def _testLocals(self, opcode, params, init, expected): vm = JavaCardVM(None) vm.frame.locals = JavaCardLocals() for index in init: vm.frame.locals[index] = init[index] f = getattr(vm, opcode) f(*params) for index in expected: self.assertEqual(expected[index], vm.frame.locals[index])
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 testEasy(self): intr = JavaCardVM(None) intr.frames.push(TestDummyFrame([None, 4])) intr.sload_1() intr.bspush(58) intr.sadd() intr.s2b() intr.sreturn() self.assertEquals(62, intr.getRetValue())
def test_callVirtualMethod(self): intr = JavaCardVM(linkResolver()) intr.load(javatest_cap) intr.frames.push(TestDummyFrame([None])) intr.new(2) intr.dup() intr.invokespecial(3) self._run(intr) self.assertTrue(isinstance(intr.frame.stack[-1], Applet)) for i in xrange(1, 4): intr.dup() intr.sspush(i) intr.invokevirtual(7) self._run(intr) self.assertEqual(i, intr.frame.stack[-1]) intr.pop() intr.returnn()
def _teststack(self, opcode, params, init, expected): vm = JavaCardVM(None) vm.frame.stack = init f = getattr(vm, opcode) f(*params) self.assertEqual(expected, vm.frame.stack)
def _testRun(self, opcode, params, stack): vm = JavaCardVM(None) vm.frame.stack = stack f = getattr(vm, opcode) f(*params)
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')
def test_gcdRecursif(self): intr = JavaCardVM(linkResolver()) intr.load(javatest_cap) intr.frames.push(JavaCardFrame([42, 56], [29, 97, 4, 28, 120, 29, 28, 29, 73, 141, 0, 9, 120])) self._run(intr) self.assertEquals(14, intr.getRetValue())
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')
def test_gcdIteratif(self): intr = JavaCardVM(None) intr.frames.push(JavaCardFrame([42, 56], [29, 97, 4, 28, 120, 28, 29, 73, 49, 29, 47, 30, 48, 112, 243])) self._run(intr) self.assertEquals(14, intr.getRetValue())
def test_objectCreation(self): intr = JavaCardVM(linkResolver()) intr.load(javatest_cap) intr.frames.push(TestDummyFrame([None])) intr.new(2) intr.dup() intr.invokespecial(3) self._run(intr) # dig into intr.invokevirtual(4) intr.returnn()