def resolveClassRef(self, class_ref, cap_file): """ Sometimes, ClassRef need to be resolved independently from the ConstantPool. There are two known cases: - super of a ClassRef - superMethod """ CR = class_ref.class_ref # First, look in the CAPFile for index in xrange(cap_file.ConstantPool.count): cst = cap_file.ConstantPool.constant_pool[index] if cst.tag == 1: # ClassRef if class_ref.isExternal == cst.isExternal: # match if cst.isExternal: if ((cst.class_ref.package_token == CR.package_token) and (cst.class_ref.class_token == CR.class_token)): return self.resolveIndex(index, cap_file) else: if cst.class_ref == CR: return self.resolveIndex(index, cap_file) # Then look in our local storage altCP = self.__altCP.get(a2d(cap_file.Header.package.aid), []) for cst, ref in altCP: if class_ref.isExternal == cst.isExternal: if cst.isExternal: if ((cst.class_ref.package_token == CR.package_token) and (cst.class_ref.class_token == CR.class_token)): return ref else: if cst.class_ref == CR: return ref # Then finally create it ... ref = self.resolveClass(class_ref, cap_file) altCP.append((class_ref, ref,)) self.__altCP[a2d(cap_file.Header.package.aid)] = altCP return ref
def wrapper(self, index, cap_file): try: pkg = __cache[a2d(cap_file.Header.package.aid)] except KeyError: __cache[a2d(cap_file.Header.package.aid)] = {} pkg = __cache[a2d(cap_file.Header.package.aid)] try: return pkg[index] except KeyError: pkg[index] = f(self, index, cap_file) return pkg[index]
def _cmselect(self, aid): channel = self.current_channel applet = self.selected[channel] if applet is not None: if not self._cmdeselect(): self.echo("Deselect failed") return False # We should spend some time looking for an applet there ... try: potential = self.applets[a2d(aid)] except KeyError: self.echo("Applet not found", self.applets) return False self.vm.frame.push(potential) try: selectmtd = JavaCardVirtualMethod(potential._ref.offset, 6, False, self.vm.cap_file, self.vm.resolver) except NoSuchMethod: self.selected[channel] = potential self.selected[channel]._selectingApplet = True return True self.vm._invokevirtualjava(selectmtd) while self.vm.step(): pass if self.vm.frame.getValue() == True: self.selected[channel] = potential self.selected[channel]._selectingApplet = True return True else: return False
def resolveClass(self, cst, cap_file): if cst.isExternal: pkg = cap_file.Import.packages[cst.class_ref.package_token] return self._resolveExtClass(a2d(pkg.aid), cst.class_ref.class_token) else: # internal class ... return JavaCardClass(cst.class_ref, cap_file, self)
def resolveExtInterfaceMethod(self, aid, cls, token): pkg = self.refs[a2d(aid)] (clsname, mtd) = pkg.getInterfaceMethod(cls, token) mtdname = mtd['name'] if mtdname[0] == '$': # Call every variations of the function the same way mtdname = mtdname[1:mtdname[1:].find('$') + 1] return PythonVirtualMethod(mtdname, mtd['type'])
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))
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))
def resolveStaticMethod(self, cst, cap_file): if cst.isExternal: pkg = cap_file.Import.packages[cst.static_method_ref.package_token] return self._resolveExtStaticMethod( a2d(pkg.aid), cst.static_method_ref.class_token, cst.static_method_ref.token) else: return JavaCardStaticMethod(cst.static_method_ref.offset, cap_file, self)
def resolveVirtualMethod(self, cst, cap_file): if cst.isExternal: pkg = cap_file.Import.packages[cst.class_ref.package_token] return self._resolveExtVirtualMethod(a2d(pkg.aid), cst.class_ref.class_token, cst.token) else: # it is fully already resolved from the other side, we just need # the token return JavaCardVirtualMethod(cst.class_ref, cst.token, cst.isPrivate, cap_file, self)
def __init__(self, version=(3,0,1)): # load preprocessed pickle from all the exp files f = open({(2,1,2): '2.1.2.json', (2,2,2): '2.2.2.json', (3,0,1): '3.0.1.json'}[version]) struct = json.loads(f.read()) self.refs = {} for pkg in struct: self.refs[a2d(pkg['AID'])] = refCollection.impoort(pkg) # Alternative ConstantPool #: a2d(pkgAID) => (class_ref, ref,) self.__altCP = {}
def install(self, data, offset): """ data[offset:] is len||appletaid||len||installdata where installdata is the data given to the install method """ aidlen = data[offset] offset += 1 aid = data[offset: offset + aidlen] offset += aidlen length = data[offset] offset += 1 # data[offset:offset+length] is what is given to the install JavaCard # method which means: len-instanceaid-len-stuff-len-customparams # where instance AID might be empty self.vm.frame.push(data) self.vm.frame.push(offset) self.vm.frame.push(length) applet = None self.echo(len(self.vm.cap_file.Applet.applets)) for apl in self.vm.cap_file.Applet.applets: if a2d(aid) == a2d(apl.aid): applet = apl break if applet is None: self.echo("Applet %s not found in the CAP file" % a2s(aid)) return self.current_applet_aid = aid self.vm._invokestaticjava(JavaCardStaticMethod( applet.install_method_offset, self.vm.cap_file, self.vm.resolver)) try: while self.vm.step(): pass except ISOException, ie: sw = isoe.getReason() return [signed1((sw & 0xff00) >> 8), signed1(sw & 0x00ff)]
def install(self, data, offset): """ data[offset:] is len||appletaid||len||installdata where installdata is the data given to the install method """ aidlen = data[offset] offset += 1 aid = data[offset:offset + aidlen] offset += aidlen length = data[offset] offset += 1 # data[offset:offset+length] is what is given to the install JavaCard # method which means: len-instanceaid-len-stuff-len-customparams # where instance AID might be empty self.vm.frame.push(data) self.vm.frame.push(offset) self.vm.frame.push(length) applet = None self.echo(len(self.vm.cap_file.Applet.applets)) for apl in self.vm.cap_file.Applet.applets: if a2d(aid) == a2d(apl.aid): applet = apl break if applet is None: self.echo("Applet %s not found in the CAP file" % a2s(aid)) return self.current_applet_aid = aid self.vm._invokestaticjava( JavaCardStaticMethod(applet.install_method_offset, self.vm.cap_file, self.vm.resolver)) try: while self.vm.step(): pass except ISOException, ie: sw = isoe.getReason() return [signed1((sw & 0xff00) >> 8), signed1(sw & 0x00ff)]
def _cmselect(self, aid): channel = self.current_channel applet = self.selected[channel] if applet is not None: if not self._cmdeselect(): self.echo("Deselect failed") return False # We should spend some time looking for an applet there ... try: potential = self.applets[a2d(aid)] except KeyError: self.echo("Applet not found", self.applets) return False self.vm.frame.push(potential) try: selectmtd = JavaCardVirtualMethod( potential._ref.offset, 6, False, self.vm.cap_file, self.vm.resolver) except NoSuchMethod: self.selected[channel] = potential self.selected[channel]._selectingApplet = True return True self.vm._invokevirtualjava(selectmtd) while self.vm.step(): pass if self.vm.frame.getValue() == True: self.selected[channel] = potential self.selected[channel]._selectingApplet = True return True else: return False
def myisAppletActive(aid): return self.applets[a2d(aid.aid)] in self.selected
def mylookupAID(buffer, offset, length): if a2d(buffer[offset:offset + length]) in self.applets: return AID(buffer, offset, length) return None
def addExportFile(self, exp): """ Add a non-standard (not javacard) export file references """ self.refs[a2d(exp.AID)] = refCollection.from_export_file(exp)