【问题标题】:Communicate with applets using another logical channel使用另一个逻辑通道与小程序通信
【发布时间】:2014-09-10 00:09:58
【问题描述】:

我使用来自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);
  }
}

上面的小程序用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 与小程序通信时,我会收到 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)

【问题讨论】:

    标签: smartcard javacard apdu


    【解决方案1】:

    关于第一个命令序列:

    <<< 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,因为这是您的小程序代码所做的:

    byte CLA = (byte) (buffer[ISO7816.OFFSET_CLA] & 0xFF);
    
    if (CLA != HW_CLA)
    {
        ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
    }
    

    因此,如果 APDU 的类字节不等于 0x80,如果您使用逻辑通道 1(在这种情况下为 0x81),您会抛出带有错误代码的 ISOException 0x6E00 (ISO7816.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 上选择小程序,然后在基本通道上发送命令 80 00 00 00 0B。由于您的小程序未在基本通道上选择,因此没有接收者可以理解专有类中的命令。因此,您会收到状态字 0x6E00 (ISO7816.SW_CLA_NOT_SUPPORTED)。

    【讨论】:

    • 那么这意味着我的小程序只能在基本频道上正常工作。对吗?
    • 请问,当我通过00 A4 04 ...在卡上选择一个小程序时,我发送的下一个命令直接进入小程序?或 ISD 或其他主小程序首先接收 APDU,如果它们不是 Select Command,则将它们发送到选定的小程序?
    • 是的,该逻辑通道上的下一个命令将直接传递给该小程序(除非它是另一个应用程序选择命令)。
    • 根据您所说的“正确”,我会说您的小程序在其他渠道上也“正确”工作。您刚刚实现了您的小程序,使其在其他基本逻辑通道上的行为有所不同。
    猜你喜欢
    • 2014-07-25
    • 1970-01-01
    • 2020-11-21
    • 2020-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-09
    • 1970-01-01
    相关资源
    最近更新 更多