示例#1
0
    def testAPDUDoc(self):
        """ This is an adaptation of the piece of code on the APDU page """
        apdu = APDU([0x00, 0x20, 0x01, 0x00, 0x04, 0x31, 0x32, 0x33, 0x34, 0x00])
        buffer = apdu.getBuffer()
        cla = buffer[ISO7816.OFFSET_CLA]
        self.assertEquals(0, cla)
        ins = buffer[ISO7816.OFFSET_INS]
        self.assertEquals(0x20, ins)

        # assume this command has incoming data
        # Lc tells us the incoming apdu command length
        bytesLeft = buffer[ISO7816.OFFSET_LC]
        self.assertEquals(4, bytesLeft)

        readCount = apdu.setIncomingAndReceive()
        self.assertEquals(4, readCount)
        while  bytesLeft > 0:
            # process bytes in buffer[5] to buffer[readCount+4];
            bytesLeft -= readCount
            readCount = apdu.receiveBytes ( ISO7816.OFFSET_CDATA )

        # Note that for a short response as in the case illustrated here
        # the three APDU method calls shown : setOutgoing(),setOutgoingLength() & sendBytes()
        # could be replaced by one APDU method call : setOutgoingAndSend().

        # construct the reply APDU
        le = apdu.setOutgoing()
        apdu.setOutgoingLength( 3 )

        # build response data in apdu.buffer[ 0.. outCount-1 ];
        buffer[0] = 1; buffer[1] = 2; buffer[3] = 3
        apdu.sendBytes ( 0 , 3 )
        self.assertEquals(APDU.STATE_FULL_OUTGOING,
                          apdu.getCurrentState())
示例#2
0
    def testBuffer(self):
        apdu = APDU([0x00, 0x20, 0x01, 0x00, 0x04, 0x31, 0x32, 0x33, 0x34])
        buffer = apdu.getBuffer()
        self.assertEquals(0x00, buffer[ISO7816.OFFSET_CLA])
        self.assertEquals(0x20, buffer[ISO7816.OFFSET_INS])
        self.assertEquals(0x01, buffer[ISO7816.OFFSET_P1])
        self.assertEquals(0x00, buffer[ISO7816.OFFSET_CLA])

        buffer[0] = 1; buffer[1] = 2; buffer[2] = 3

        buff = apdu.getBuffer()
        self.assertEquals(1, buff[0])
        self.assertEquals(2, buff[1])
        self.assertEquals(3, buff[2])
示例#3
0
    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)]
示例#4
0
 def testState(self):
     apdu = APDU([0x00, 0x20, 0x01, 0x00, 0x04, 0x31, 0x32, 0x33, 0x34, 0x00])
     self.assertEquals(APDU.STATE_INITIAL, apdu.getCurrentState())
     apdu.setIncomingAndReceive()
     self.assertTrue(APDU.STATE_PARTIAL_INCOMING <= apdu.getCurrentState())
     apdu.receiveBytes(ISO7816.OFFSET_CDATA)
     self.assertEquals(APDU.STATE_FULL_INCOMING, apdu.getCurrentState())
     apdu.setOutgoing()
     self.assertEquals(APDU.STATE_OUTGOING, apdu.getCurrentState())
     apdu.setOutgoingLength(10)
     self.assertEquals(APDU.STATE_OUTGOING_LENGTH_KNOWN, apdu.getCurrentState())
     apdu.sendBytes(0, 2)
     self.assertEquals(APDU.STATE_PARTIAL_OUTGOING, apdu.getCurrentState())
     apdu.sendBytes(0, 8)
     self.assertEquals(APDU.STATE_FULL_OUTGOING, apdu.getCurrentState())