使用其他逻辑通道与applet通信

时间:2014-09-09 10:16:01

标签: smartcard javacard apdu

我使用here中的以下代码构建.cap文件:

package helloWorldPackage;

import javacard.framework.APDU;
import javacard.framework.Applet;
import javacard.framework.ISO7816;
import javacard.framework.ISOException;
import javacard.framework.Util;

public class HelloWorldApplet extends Applet {
         private static final byte[] helloWorld = {(byte)'H',(byte)'e',(byte)'l',(byte)'l',(byte)'o',(byte)' ',(byte)'W',(byte)'o',(byte)'r',(byte)'l',(byte)'d',};

         private static final byte HW_CLA = (byte)0x80;
         private static final byte HW_INS = (byte)0x00;

         public static void install(byte[] bArray, short bOffset, byte bLength) {
             new HelloWorldApplet().register(bArray, (short) (bOffset + 1), bArray[bOffset]);
         }

         public void process(APDU apdu) {

             if (selectingApplet()) {
             return;
         }

         byte[] buffer = apdu.getBuffer();
         byte CLA = (byte) (buffer[ISO7816.OFFSET_CLA] & 0xFF);
         byte INS = (byte) (buffer[ISO7816.OFFSET_INS] & 0xFF);

         if (CLA != HW_CLA)
        {
            ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
        }

          switch ( INS ) {
             case HW_INS:
               getHelloWorld( apdu );
               break;
            default:
               ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
         }
   }

  private void getHelloWorld( APDU apdu)
  {
      byte[] buffer = apdu.getBuffer();
      short length = (short) helloWorld.length;

      Util.arrayCopyNonAtomic(helloWorld, (short)0, buffer, (short)0, (short) length);

      apdu.setOutgoingAndSend((short)0, length);
  }
}

上面的applet上传了AID = 0102030405060708091111。我成功发送了SELECT命令和8000000B命令,并接收 HelloWorld 作为回应。

OpenSC-Tool> opensc-tool -s 00a404000b0102030405060708091111 -s 800000000b
Using reader with a card: ACS CCID USB Reader 0
Sending: 00 A4 04 00 0B 01 02 03 04 05 06 07 08 09 11 11
Received (SW1=0x90, SW2=0x00)
Sending: 80 00 00 00 0B
Received (SW1=0x90, SW2=0x00):
48 65 6C 6C 6F 20 57 6F 72 6C 64 Hello World

据我所知,命令CLA中的低半字节表示通道数,对吗? 那么为什么当我想使用逻辑通道号1与applet通信时,我会收到6E00第二个APDU命令:

OpenSC-Tool> opensc-tool -s 01a404000b0102030405060708091111 -s 810000000b
Using reader with a card: ACS CCID USB Reader 0
Sending: 01 A4 04 00 0B 01 02 03 04 05 06 07 08 09 11 11
Received (SW1=0x90, SW2=0x00)
Sending: 81 00 00 00 0B
Received (SW1=0x6E, SW2=0x00)

并且:

OpenSC-Tool> opensc-tool -s 01a404000b0102030405060708091111 -s 800000000b
Using reader with a card: ACS CCID USB Reader 0
Sending: 01 A4 04 00 0B 01 02 03 04 05 06 07 08 09 11 11
Received (SW1=0x90, SW2=0x00)
Sending: 80 00 00 00 0B
Received (SW1=0x6E, SW2=0x00)

1 个答案:

答案 0 :(得分:2)

关于第一个命令序列:

<<< 01 A4 04 00 0B 01 02 03 04 05 06 07 08 09 11 11
>>> 90 00
>>> 81 00 00 00 0B
<<< 6E 00

您收到状态字0x6E00,因为这是您的applet代码的作用:

byte CLA = (byte) (buffer[ISO7816.OFFSET_CLA] & 0xFF);

if (CLA != HW_CLA)
{
    ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
}

因此,如果APDU的类字节不等于0x80,如果您使用逻辑通道1(在这种情况下为0x81)就是这种情况,则抛出{{ 1}}错误代码ISOException0x6E00)。

关于第二个命令序列:

ISO7816.SW_CLA_NOT_SUPPORTED

您在逻辑通道1上选择小程序,然后在基本通道上发送命令<<< 01 A4 04 00 0B 01 02 03 04 05 06 07 08 09 11 11 >>> 90 00 >>> 80 00 00 00 0B <<< 6E 00 。由于未在基本频道上选择您的小程序,因此没有收件人能够理解专有类中的命令。那么,您会收到状态字80 00 00 00 0B0x6E00)。