繁体   English   中英

使用另一个逻辑通道与小程序进行通信

[英]Communicate with applets using another logical channel

我从这里使用以下代码构建一个.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);
  }
}

上面的小程序以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通信时,我收到第二个APDU命令的6E00

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)

关于第一个命令序列:

<<< 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 ))就是这种情况,则会抛出ISOException错误代码0x6E00ISO7816.SW_CLA_NOT_SUPPORTED )。

关于第二个命令序列:

<<< 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

您选择逻辑通道1上的applet,然后在基本通道上发送命令80 00 00 00 0B 由于没有在基本通道上选择您的applet,因此没有收件人可以理解专有类中的命令。 因此,您将收到状态字0x6E00ISO7816.SW_CLA_NOT_SUPPORTED )。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM