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)]
def __init__(self, offset, cap_file, resolver): self.offset = offset sf = cap_file.StaticField if offset < sf.array_init_count * 2: offset = offset // 2 aii = sf.array_init[offset] convertfunc = { 2: lambda x: bool(x[0]), 3: lambda x: utils.signed1(x[0]), 4: lambda x: utils.signed2((x[0] << 8) + x[1]), 5: lambda x: utils.signed4((x[0] << 24) + (x[1] << 16) + (x[2] << 8) + x[3]) }[aii.type] elemsize = {2: 1, 3: 1, 4: 2, 5: 4}[aii.type] index = 0 value = [] while index < aii.count: value.append(convertfunc(aii.values[index:])) index += elemsize self.val = value elif offset < sf.reference_count * 2: self.val = None else: offset -= sf.reference_count * 2 if offset <= sf.default_value_count: self.val = None else: print("I am a non-default primitive type") raise NotImplementedError("non default primitive static field")
def __init__(self, offset, cap_file, resolver): self.offset = offset sf = cap_file.StaticField if offset < sf.array_init_count * 2: offset = offset // 2 aii = sf.array_init[offset] convertfunc = {2: lambda x: bool(x[0]), 3: lambda x: utils.signed1(x[0]), 4: lambda x: utils.signed2((x[0] << 8) + x[1]), 5: lambda x: utils.signed4((x[0] << 24) + (x[1] << 16) + (x[2] << 8) + x[3]) }[aii.type] elemsize = {2: 1, 3: 1, 4: 2, 5: 4}[aii.type] index = 0 value = [] while index < aii.count: value.append(convertfunc(aii.values[index:])) index += elemsize self.val = value elif offset < sf.reference_count * 2: self.val = None else: offset -= sf.reference_count * 2 if offset <= sf.default_value_count: self.val = None else: print "I am a non-default primitive type" raise NotImplementedError("non default primitive static field")
def _ifxx(self, branch, op): val = self.frame.pop() if {'eq': val == 0, 'ne': val != 0, 'lt': val < 0, 'le': val <= 0, 'gt': val > 0, 'ge': val >= 0}[op]: return utils.signed1(branch)
def _if_scmpxx(self, branch, op): val2 = self.frame.pop() val1 = self.frame.pop() if {'eq': val1 == val2, 'ne': val1 != val2, 'lt': val1 < val2, 'le': val1 <= val2, 'gt': val1 > val2, 'ge': val1 >= val2}[op]: return utils.signed1(branch)
def hexify(hexStr): """ Turns a string of hexadecimal nibbles into an array of numbers >>> hexify("00200003083132333400000000") [0, 32, 0, 3, 8, 49, 50, 51, 52, 0, 0, 0, 0] """ bytes = [] hexStr = ''.join(hexStr.split()) for i in range(0, len(hexStr), 2): bytes.append(signed1(int(hexStr[i:i + 2], 16))) return bytes
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 sinc(self, index, const): self.frame.locals[index] += utils.signed1(const)
def goto(self, branch): return utils.signed1(branch)
def _if_acmpxx(self, branch, op): val2 = self.frame.pop() val1 = self.frame.pop() if {'eq': val1 is val2, 'ne': val1 is not val2}[op]: return utils.signed1(branch)
def ifnonnull(self, branch): val = self.frame.pop() if val is not None: return utils.signed1(branch)
def s2b(self): val = self.frame.pop() self.frame.push(utils.signed1(val & 0xff))
def bspush(self, byte): self.frame.push(utils.signed1(byte))